-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCConnectionSocket.cpp
531 lines (465 loc) · 15.6 KB
/
CConnectionSocket.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
Copyright [2010] [Richard Bross]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// CConnectionSocket.cpp file
//
// Created: Richard A. Bross
//
#if defined(_WIN32)
#include "stdafx.h"
#endif
#include "XPlat.h"
#include "CBaseSocket.h"
#include "CConnectionSocket.h"
#include <syslog.h>
#include "CProfileTimer.h"
#include <poll.h>
// Constructor
CConnectionSocket::CConnectionSocket(CBaseSocket *pOwner, struct sockaddr *pAddr) : CBaseSocket()
{
memset(&hThread, 0, sizeof(hThread));
cOwner = pOwner;
memset(&sAddr, 0, sizeof(sAddr));
memset(&sUAddr, 0, sizeof(sUAddr));
if (pAddr)
memcpy(&sAddr, pAddr, sizeof(sAddr));
eRequestClose = CLOSE_ACTIVE;
if (cOwner)
iAF = pOwner->GetSocketType();
}
CConnectionSocket::~CConnectionSocket()
{
TerminateConnection();
}
// Requests that the handleconenction thread close
// NOTES:
// We used to just close the socket. However, data waiting in the socket buffer was lost.
// We still want the thread to exit on an invalid socket, since the other side may close the connection.
// Now we set a flag and let the HandleConnection thread get remaining data.
// If a user is calling this and they are stupid enough to call SendDataOut at the same time,
// tough luck, the connection will be closed after 50 ms max.
void CConnectionSocket::TerminateConnection(ECLOSE_TYPE eRequest)
{
// Ask the connection thread to close
CProfileTimer::SetTimestamp("CCS-CLOSE");
eRequestClose = eRequest;
if (hThread)
{
#if defined(_WIN32)
::WaitForSingleObject(hThread, INFINITE);
::CloseHandle(hThread); // _beginthreadex doesn't auto close the handle
#else
// Make sure thread finishes
int iRet;
if ((iRet = ::pthread_join(hThread, NULL)) != 0)
{ // EDEADLK (35) will occur if some bozo calls this routine from the thread itself
::openlog("connectionsocket", LOG_PID, LOG_LOCAL0);
char strLogEntry[1024];
::sprintf(strLogEntry, "pthread_join error could cause memory leak. Error number: %d", iRet);
::syslog(LOG_CRIT, "%s", strLogEntry);
::closelog();
}
else
{ // Only on successs
::memset(&hThread, 0, sizeof(hThread));
}
#endif
};
// Close the socket
CloseSocket();
};
// Starts thread to service connection. Returns thread handle.
XPLAT_HANDLE CConnectionSocket::ServiceConnection(SOCKET sNewSocket)
{
// Timestamp
tConnected = time(NULL);
// Already servicing a connection?
if (hThread)
{
if (CXPlatThread::IsThreadActive(hThread))
return(hThread);
else
{
#if defined(_WIN32)
::CloseHandle(hThread);
#endif
::memset(&hThread, 0, sizeof(hThread));
};
};
// Assign socket
sSocket = sNewSocket;
#if defined(_WIN32)
// Begin the servicing thread
unsigned pThread;
hThread = (HANDLE) ::_beginthreadex(NULL,
0,
ServiceConnectionThread,
(void *) this,
1,
&pThread);
#else
int iReturn;
iReturn = ::pthread_create(&hThread, pthread_attr_default, (START_ROUTINE) ServiceConnectionThread, (void *) this);
#endif
return(hThread);
};
// Send data
BOOL CConnectionSocket::SendDataOut(LPSTR lpData, int nPacketSize, int nTotalLength)
{
int nDataSent = 0;
int nReturn = 0;
#if defined(__linux)
int nFlags = 0x4000; // MSG_NOSIGNAL
#else
int nFlags = 0;
#endif
// fd_set writesets;
struct pollfd fds[1];
// int nReturnSet = SOCKET_ERROR;
#if defined(__linux)
// In Linux, doing a close on a socket in a thread doesn't
// seem to always unblock select or listen in another
// thread. So for Linux, we'll timeout after .5 seconds and loop
// struct timeval sTv;
// 3 ms. This is the time that we wait for the output buffer to have some space. Probably almost never have to wait.
// long lUSec = 3000;
long lUSec = 3;
#endif
// Make sure pointer isn't NULL
if (!lpData)
return FALSE;
// Send data
// FD_ZERO(&writesets);
fds[0].fd = sSocket;
fds[0].events = POLLOUT | POLLWRBAND; // POLLOUT == POLLWRNORM
while(TRUE)
{
cCriticalSocket.Lock();
if (sSocket == SOCKET_ERROR)
{
cCriticalSocket.Unlock();
return(FALSE);
};
// FD_SET(sSocket, &writesets);
cCriticalSocket.Unlock();
// Anything waiting to be sent?
#if defined(__linux)
// sTv.tv_sec = 0;
// sTv.tv_usec = lUSec;
try
{
// nReturnSet = ::select(sSocket + 1, NULL, &writesets, NULL, &sTv);
nReturn = ::poll(fds, 1, lUSec);
}
catch(...)
{
nReturn = SOCKET_ERROR;
}
#else
nReturnSet = ::select((int) sSocket + 1, NULL, (XPLAT_FD_SET_PTR) & writesets, (XPLAT_FD_SET_PTR) NULL, NULL);
#endif
if (nReturn == SOCKET_ERROR)
{
CloseSocket();
return(FALSE);
};
cCriticalSocket.Lock();
if (sSocket == SOCKET_ERROR)
{
cCriticalSocket.Unlock();
return(FALSE);
};
// nReturn = FD_ISSET(sSocket, &writesets);
cCriticalSocket.Unlock();
if (!nReturn && (fds[0].revents & POLLHUP || fds[0].revents & POLLERR))
return(FALSE);
if (nReturn)
{ // Other end closed
// If not one of the "ok to write" events, loop. Is this possible at this point
if (!fds[0].revents & POLLWRBAND && !fds[0].revents & POLLOUT)
continue;
// Send the whole packet to the client if the packet is big enough
cCriticalSocket.Lock();
if (sSocket == SOCKET_ERROR)
{
cCriticalSocket.Unlock();
return(FALSE);
};
if (nDataSent + nPacketSize >= nTotalLength)
{
try
{
nReturn = ::send(sSocket, (LPSTR) lpData + nDataSent, nTotalLength - nDataSent, nFlags);
}
catch(...)
{
nReturn = SOCKET_ERROR;
}
cCriticalSocket.Unlock();
if (nReturn == SOCKET_ERROR)
{
nReturn = XPLAT_WSAGETLASTERR;
if (nReturn != XEWOULDBLOCK && // would block
nReturn != XEINPROGRESS) // in progress
{ // error return
CloseSocket();
return(FALSE);
}
};
nDataSent += (nTotalLength - nDataSent);
break;
}
else
{
try
{
nReturn = ::send(sSocket, lpData + nDataSent, nPacketSize, nFlags);
}
catch(...)
{
nReturn = SOCKET_ERROR;
}
cCriticalSocket.Unlock();
if (nReturn == SOCKET_ERROR)
{
nReturn = XPLAT_WSAGETLASTERR;
if (nReturn != XEWOULDBLOCK && // would block
nReturn != XEINPROGRESS) // in progress
{ // error return
CloseSocket();
return(FALSE);
}
};
nDataSent += nPacketSize;
}
// It was all sent
if (nDataSent >= nTotalLength)
break;
// We only have to worry about this for partial data
CProfileTimer::SetTimestamp("CCS-SEND");
}
else
{ // If nothing to write, it's been 6ms since the last write or 50 ms since we were asked to close
if (eRequestClose == CLOSE_IMMEDIATE || // Close right now
(eRequestClose == CLOSE_WAIT && // Wait for data. 1ms (6 with the select call) since last send or 50ms for any send
(CProfileTimer::GetElapsedMS("CCS-SEND", "NowSend", true) > 1 || CProfileTimer::GetElapsedMS("CCS-CLOSE", "NowSend", false) > 50)))
{
break;
}
}
// Clean up
cCriticalSocket.Lock();
if (sSocket == SOCKET_ERROR)
{
cCriticalSocket.Unlock();
return(FALSE);
};
// FD_ZERO(&writesets);
cCriticalSocket.Unlock();
}
return(nDataSent == nTotalLength);
}
// Receive the data from socket, external buffer
int CConnectionSocket::ReceiveData(void *lpBuf, int nBufLen)
{
int iReturn;
#if defined(__linux)
int nFlags = 0x4000; // MSG_NOSIGNAL
#else
int nFlags = 0;
#endif
// Get any data sent over the connection
cCriticalSocket.Lock();
try
{
iReturn = ::recv(sSocket, (LPSTR) lpBuf, nBufLen, nFlags);
}
catch(...)
{
iReturn = SOCKET_ERROR;
}
cCriticalSocket.Unlock();
return(iReturn);
}
// Receive the data from socket, internal buffer
int CConnectionSocket::ReceiveData()
{
return(ReceiveData(ucBuffer, CS_BUFFER_SIZE));
}
// Called by ServiceConnectionThread to actually do the work
int CConnectionSocket::HandleConnection()
{
// fd_set readsets;
struct pollfd fds[1];
int nReturn;
int nRevLength;
int nPacket = 2048;
unsigned char *ucHS;
#if defined(__linux)
// Linux "select()" does not unblock when the socket is closed. So we set a timeout.
// struct timeval sTv;
// long lUSec = 3000; // If the socket gets closed, this will shut it down within 3ms
long lUSec = 3; // If the socket gets closed, this will shut it down within 3ms
#endif
try
{
// Send handshake, if derived class wants us to
if (GetHandshake(&ucHS, nReturn))
SendDataOut((char *) ucHS, nPacket, nReturn);
// Clear sets
// FD_ZERO(&readsets);
fds[0].fd = sSocket;
fds[0].events = POLLIN | POLLRDBAND; // POLLRDNORM == POLLIN. POLLRDHUP is only available if _GNU_SOURCE is defined before headers
// Read the data
while(TRUE)
{
cCriticalSocket.Lock(); // Lock
// Someone close it?
if (sSocket == SOCKET_ERROR)
{
cCriticalSocket.Unlock(); // if Unlock
Disconnected();
return(0);
};
// Init sets
// FD_SET(sSocket, &readsets);
// cCriticalSocket.Unlock(); // Unlock
// Blocking call
#if defined(__linux)
// Reset every time because the Linux "select()" modifies the value
// sTv.tv_sec = 0;
// sTv.tv_usec = lUSec;
try
{
// nReturn = ::select(sSocket + 1, &readsets, NULL, NULL, &sTv);
nReturn = ::poll(fds, 1, lUSec);
}
catch(...)
{
nReturn = SOCKET_ERROR;
}
#else
nReturn = ::select((int) sSocket + 1, (XPLAT_FD_SET_PTR) & readsets, NULL, (XPLAT_FD_SET_PTR) NULL, NULL);
#endif
// Someone close it or an error
// cCriticalSocket.Lock();
cCriticalSocket.Unlock();
if (nReturn == SOCKET_ERROR)
{
if (sSocket != SOCKET_ERROR)
CloseSocket();
Disconnected();
return(0);
};
// nReturn = FD_ISSET(sSocket, &readsets);
// cCriticalSocket.Unlock();
if (!nReturn && (fds[0].revents & POLLHUP || fds[0].revents & POLLERR))
{
Disconnected();
return(0);
}
// We have data
if (nReturn)
{ // If not one of the "ok to read" events, loop. Is this possible at this point
if (!fds[0].revents & POLLRDBAND && !fds[0].revents & POLLIN)
continue;
nRevLength = ReceiveData();
if (nRevLength == SOCKET_ERROR || nRevLength == 0)
{
nReturn = XPLAT_WSAGETLASTERR;
if (nReturn != XEWOULDBLOCK && // would block
nReturn != XEINPROGRESS) // in progress
{ // error return, probably closed and ended nornally
if (sSocket != SOCKET_ERROR)
CloseSocket();
Disconnected();
return(0);
}
}
// This should be impossible
// if (nRevLength > CConnectionSocket::CS_BUFFER_SIZE)
// {
// Disconnected();
// return(0);
// };
// Echo data
if (nRevLength > 0)
{ // Make sure our socket isn't closed. If we're in our destructor, the derived class
// is already destroyed and ProcessData will be calling a pure virtual function.
// if (sSocket == SOCKET_ERROR)
// {
// Disconnected();
// return(0);
// };
// Process the data
ProcessData(ucBuffer, nRevLength);
CProfileTimer::SetTimestamp("CCS-RECV");
}
}
else
{ // If nothing to read, it's been 6ms since the last read or 50 ms since we were asked to close
if (eRequestClose == CLOSE_IMMEDIATE || // Close right now
(eRequestClose == CLOSE_WAIT && // Wait for data. 1ms (6 with the select call) since last receive or 50ms for any receive
(CProfileTimer::GetElapsedMS("CCS-RECV", "NowRecv", true) > 1 || CProfileTimer::GetElapsedMS("CCS-CLOSE", "NowRecv", false) > 50)))
{
Disconnected();
return(0);
}
}
// // clear up
// cCriticalSocket.Lock();
// if (sSocket == SOCKET_ERROR)
// {
// Disconnected();
// return(0);
// };
// FD_ZERO(&readsets);
};
}
catch(...)
{ // If we're not locked, no harm. But if we are it prevents a deadlock.
cCriticalSocket.Unlock();
Disconnected();
return(-1);
}
Disconnected();
return(0);
};
// This thread waits for data
unsigned CConnectionSocket::ServiceConnectionThread(void* pVoid)
{
CConnectionSocket *cConnectionSocket;
unsigned uReturn;
// Assign our "this" pointer
if (!pVoid)
return((unsigned) - 1);
cConnectionSocket = (CConnectionSocket *) pVoid;
uReturn = cConnectionSocket->HandleConnection();
return(uReturn);
}
// Return the time the server was started
time_t CConnectionSocket::GetTimeConnected()
{
return(tConnected);
};
// Connected?
BOOL CConnectionSocket::IsConnected()
{
if ((int) sSocket == SOCKET_ERROR || !hThread)
return(FALSE);
return(CXPlatThread::IsThreadActive(hThread));
};
// Return the socket address
struct sockaddr_in CConnectionSocket::GetSocketAddress()
{
return(sAddr);
};