-
Notifications
You must be signed in to change notification settings - Fork 0
/
port.cpp
490 lines (412 loc) · 10.2 KB
/
port.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// port.cpp
// FireFly server 'port' data type
#include "port.h"
#include "database.h"
#include "string.h"
#ifndef SIGIGN
#define SIGIGN SIG_IGN
#endif
#include <errno.h>
#include <netinet/tcp.h>
fd_set port_t::m_fdsHandles;
fd_set port_t::m_fdsWriteHandles;
portp port_t::m_pCurrent = NULL;
static int m_hasWriters = 0;
/* Broken Pipe signal handler
void CDECL port_t::SigBrokenPipe( int sig )
{
if( m_pCurrent )
{
printf( "[%d] broken pipe, closed", (int)m_pCurrent->m_fdSocket );
m_pCurrent->Close();
}
else
{
printf( "[unk] broken pipe, ignored" );
}
}
*/
RESULT port_t::Initialise()
{
#ifdef WIN32
// Start windows sockets
WSADATA wsaData;
WORD nRequest = MAKEWORD( 1, 0 );
if( WSAStartup( nRequest, &wsaData ) != 0 )
{
printf( "Failed to initialise windows sockets" );
return E_SOCKET;
}
// Check that the required version is supported
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 0 )
{
printf( "WinSock version 1.0 required" );
WSACleanup();
return E_SOCKET;
}
#else
// Install a signal handler for Broken Pipe signals
//signal( SIGPIPE, SigBrokenPipe );
signal( SIGPIPE, SIGIGN );
#endif
FD_ZERO( &m_fdsHandles );
FD_ZERO( &m_fdsWriteHandles );
m_hasWriters = 0;
return E_SUCCESS;
}
RESULT port_t::Shutdown()
{
#ifdef WIN32
WSACleanup();
#endif
return E_SUCCESS;
}
RESULT port_t::Select( time_t nTimeout )
{
struct timeval tvTime = { (long) nTimeout, 0 };
fd_set fdsRead = m_fdsHandles;
//fd_set fdsWrite = m_fdsWriteHandles;
// fd_set fdsExcept = m_fdsHandles;
// Must select on fdsRead and fdsWrite, otherwise writing large
// amounts of data will stall until the next read!
// int nReady = select( FD_SETSIZE, &fdsRead, &fdsWrite, &fdsExcept, &tvTime );
// For Win32, any non-null handle set specified must have at least
// one handle in it according to the documentation. Stupid restriction.
int nReady;
if (m_hasWriters)
{
nReady = select( FD_SETSIZE, &fdsRead, &m_fdsWriteHandles, NULL, &tvTime );
//printf( "** has %d writers.\n", m_hasWriters );
}
else
{
nReady = select( FD_SETSIZE, &fdsRead, NULL, NULL, &tvTime );
}
// Clear all the write handles every time we wake up
// This is the only safe way to clean them up, because if we ever
// miss one (eg. the thread writing to a socket is killed) we will
// use up 100% cpu polling that socket until we're restarted.
// So instead Write() must put the socket back into the set
// every time it returns with write data outstanding.
FD_ZERO( &m_fdsWriteHandles );
m_hasWriters = 0;
if( nReady < 1 ) return E_FALSE;
return E_SUCCESS;
}
RESULT port_t::Listen( int32 nPort )
{
// Ensure port number is valid
if( nPort < 1 ) return E_RANGE;
// Create a new socket
m_fdSocket = socket( AF_INET, SOCK_STREAM, 0 );
if( m_fdSocket == INVALID_SOCKET ) return E_SOCKET;
// Enable address re-use (so we can use any-address)
int nOption = 1;
if( setsockopt( m_fdSocket, SOL_SOCKET, SO_REUSEADDR,
(char*)&nOption, sizeof(nOption) ) == -1 )
{
Close();
return E_SOCKET;
}
// Bind to any-address on specified port
struct sockaddr_in addrListen;
addrListen.sin_family = AF_INET;
addrListen.sin_addr.s_addr = INADDR_ANY;
addrListen.sin_port = htons((unsigned short)nPort);
if( bind( m_fdSocket, (struct sockaddr*)&addrListen,
sizeof(addrListen) ) == -1 )
{
Close();
return E_SOCKET;
}
// Set connection queue size
if( listen( m_fdSocket, 8 ) == -1 )
{
Close();
return E_SOCKET;
}
// Set up the socket options
RESULT nResult = SetupSocket();
if( ISERROR(nResult) ) return nResult;
return E_SUCCESS;
}
RESULT port_t::Accept( portp& ptResult )
{
SOCKET fdNew;
// Accept the new connection
m_pCurrent = this;
fdNew = accept( m_fdSocket, (struct sockaddr*) 0, 0 );
m_pCurrent = NULL;
// Check the return value
if( fdNew == INVALID_SOCKET )
{
#ifdef WIN32
if( WSAGetLastError() != WSAEWOULDBLOCK )
#else
if( errno != EAGAIN && errno != EINTR )
#endif
{
// Something has gone horribly wrong
Close();
return E_CLOSED;
}
// Write would block - try again later
return E_FALSE;
}
// Create the new socket
ptResult = new port_t;
if( !ptResult )
{
#ifdef WIN32
closesocket( fdNew );
#else
close( fdNew );
#endif
return E_MEMORY;
}
ptResult->m_fdSocket = fdNew;
// Set up the socket options
RESULT nResult = ptResult->SetupSocket();
if( ISERROR(nResult) ) return nResult;
// Set no-delay mode (TODO: should do internal buffering or use TCP_CORK)
int noDelay = 1;
if (setsockopt( fdNew, IPPROTO_TCP, TCP_NODELAY, (char*)&noDelay, sizeof(noDelay) ) == -1) {
Close();
return E_SOCKET;
}
return E_SUCCESS;
}
RESULT port_t::Connect( stringp sAddress, int32 nPort )
{
return E_NOT_IMPLEMENTED;
}
RESULT port_t::Read( int32 nSize, stringp& sResult )
{
RESULT nResult = ReadData();
if( ISERROR(nResult) ) return nResult;
// If not reading data, succeed unless port is closed and empty
if( nSize < 1 )
{
if( IsClosed() && !m_sReadBuf.size() )
return E_CLOSED;
return E_SUCCESS;
}
// Check if sufficient data is available
if( (int32)m_sReadBuf.size() >= nSize )
{
// Create the result string
sResult = new string_t( m_sReadBuf.c_str(), nSize );
if( !sResult ) return E_MEMORY;
// Remove the read data from the input buffer
m_sReadBuf = m_sReadBuf.c_str() + nSize;
return E_SUCCESS;
}
if( IsClosed() )
return E_CLOSED;
return E_FALSE;
}
RESULT port_t::ReadLine( stringp& sResult )
{
RESULT nResult = ReadData();
if( ISERROR(nResult) ) return nResult;
// Check if sufficient data is available
long nEnd = m_sReadBuf.find_first_of( "\n\r" );
if( nEnd != std::basic_string<char>::npos )
{
// Create the result string
sResult = new string_t( m_sReadBuf.c_str(), nEnd );
if( !sResult ) return E_MEMORY;
// Handle any type of line terminator
if( nEnd + 1 < (int32)m_sReadBuf.length() )
{
if( (m_sReadBuf[nEnd] == '\n' && m_sReadBuf[nEnd+1] == '\r') ||
(m_sReadBuf[nEnd] == '\r' && m_sReadBuf[nEnd+1] == '\n') ) nEnd++;
}
// Remove the read data from the input buffer
// Use a temp string because basic_string uses memcpy not memmove
std::basic_string<char> strTemp = m_sReadBuf.c_str() + nEnd + 1;
m_sReadBuf = strTemp;
return E_SUCCESS;
}
if( IsClosed() )
return E_CLOSED;
return E_FALSE;
}
RESULT port_t::Write( stringp sValue )
{
if( IsClosed() )
return E_CLOSED;
int32 nLen = sValue->Length();
if( nLen < 1 ) return E_SUCCESS;
// Calculate the length remaining to write
size_t nWritten, nWrite = nLen - m_nWritten;
ASSERT( nWrite > 0 );
const char* szData = sValue->CString() + m_nWritten;
m_pCurrent = this;
#ifdef WIN32
if( m_nPort == FILE_PORT_FLAG )
{
// Write to the file
nWritten = WRITE( m_fdSocket, szData, nWrite );
}
else
{
// Write to the socket
nWritten = send( m_fdSocket, szData, nWrite, 0 );
}
#else
// Write the data to the port
nWritten = write( m_fdSocket, szData, nWrite );
#endif
m_pCurrent = NULL;
// Check the return value
if( nWritten == (size_t)-1 )
{
#ifdef WIN32
if( WSAGetLastError() != WSAEWOULDBLOCK )
#else
if( errno != EAGAIN && errno != EINTR )
#endif
{
// Something has gone horribly wrong
Close();
return E_CLOSED;
}
// Write would block - try again later
nWritten = 0;
}
#ifdef WIN32
// Send the data immediately
//_commit( m_fdSocket );
#endif
if( m_nWritten + (long)nWritten < nLen )
{
// Could not write all the data
m_nWritten += nWritten;
// Add the socket to the write handle set
// Must do this every time we leave outstanding data
FD_SET( m_fdSocket, &m_fdsWriteHandles );
m_hasWriters++;
//printf( "** added writer, now has %d writers.\n", m_hasWriters );
return E_FALSE;
}
// All data successfully written
m_nWritten = 0;
return E_SUCCESS;
}
RESULT port_t::Close()
{
m_pCurrent = NULL; // prevent potential infinite loop
if( m_fdSocket != INVALID_SOCKET )
{
#ifdef WIN32
if( m_nPort == FILE_PORT_FLAG )
{
// Close the file
CLOSE( m_fdSocket );
}
else
{
// Close the socket
FD_CLR( m_fdSocket, &m_fdsHandles );
FD_CLR( m_fdSocket, &m_fdsWriteHandles );
closesocket( m_fdSocket );
}
#else
FD_CLR( m_fdSocket, &m_fdsHandles );
FD_CLR( m_fdSocket, &m_fdsWriteHandles );
close( m_fdSocket );
#endif
}
// Mark the port as closed
m_fdSocket = INVALID_SOCKET;
return E_SUCCESS;
}
// Private implementation
RESULT port_t::ReadData()
{
size_t nRead;
// Check the socket for new data
m_pCurrent = this;
if( m_fdSocket != INVALID_SOCKET )
{
#ifdef WIN32
if( m_nPort == FILE_PORT_FLAG )
{
// Read from the file
nRead = READ( m_fdSocket, (char*)m_pReadChunk, READ_CHUNK );
}
else
{
// Read from the socket
nRead = recv( m_fdSocket, (char*)m_pReadChunk, READ_CHUNK, 0);
}
#else
// Read any new data from the port
nRead = read( m_fdSocket, (char*)m_pReadChunk, READ_CHUNK );
#endif
// Check the return value
if( nRead == 0 ) Close();
else if( nRead == (size_t)-1 )
{
#ifdef WIN32
if( WSAGetLastError() != WSAEWOULDBLOCK )
#else
if( errno != EAGAIN && errno != EINTR )
#endif
{
// Something has gone horribly wrong
Close();
return E_CLOSED;
}
// Read would block - try again later
nRead = 0;
}
if( nRead ) m_sReadBuf.append( (char*)m_pReadChunk, nRead );
}
m_pCurrent = NULL;
return E_SUCCESS;
}
RESULT port_t::SetupSocket()
{
// Make sure out-of-band data stays that way
int nOption = 0;
if( setsockopt( m_fdSocket, SOL_SOCKET, SO_OOBINLINE,
(char*)&nOption, sizeof(nOption) ) == -1 )
{
Close();
return E_SOCKET;
}
// Set non-blocking flag so we can poll/select
#ifdef WIN32
{
unsigned long nValue = 1;
if( ioctlsocket( m_fdSocket, FIONBIO, &nValue ) != 0 )
{
Close();
return E_SOCKET;
}
}
#else
if( fcntl( m_fdSocket, F_SETFL, O_NONBLOCK ) == -1 )
{
Close();
return E_SOCKET;
}
#endif
// Add the socket to the handle set
FD_SET( m_fdSocket, &m_fdsHandles );
return E_SUCCESS;
}
// Persistent storage management
void port_t::Persist()
{
// Write the reference count
g_oDatabase.WriteLong( m_nRef );
}
void port_t::Restore()
{
// Read the reference count
m_nRef = g_oDatabase.ReadLong();
}