-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocketHelperClasses.cpp
More file actions
122 lines (114 loc) · 5.27 KB
/
SocketHelperClasses.cpp
File metadata and controls
122 lines (114 loc) · 5.27 KB
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
#include "SocketManager.h"
void Socket::Delete(Socket *obj) {
EnterCriticalSection(&obj->SockCritSec);
{
// Close the socket if it hasn't already been closed
if (obj->s != INVALID_SOCKET && (obj->state == CONNECTED || obj->state == FAILURE)) {
LOG("closing socket\n");
obj->Close(obj->state == FAILURE);
}
}
LeaveCriticalSection(&obj->SockCritSec);
ListElt<Socket>::Delete(obj);
}
void Socket::DeleteOrDisconnect(Socket *obj, CriticalMap<UUID, Socket*> &critMap) {
bool needDelete;
EnterCriticalSection(&obj->SockCritSec);
{
// Close the socket if it hasn't already been closed
if (obj->state < DISCONNECTING) {
switch (obj->state){
case CLOSING : {
if (obj->client->ShouldReuseSocket()) {
LOG("disconnecting socket\n");
obj->Disconnect(critMap);
return;
}
/** NOBREAK **/
}
case CONNECTED :{
LOG("closing socket\n");
obj->Close(false);
break;
}
case FAILURE : /** NOBREAK **/
case LISTENING :{
LOG("closing socket\n");
obj->Close(true);
break;
}
default:
break;
}
}
if (obj->state != RETRY_CONNECTION) {
EnterCriticalSection(&critMap.critSec);
{
critMap.map.erase(obj->id);
}
LeaveCriticalSection(&critMap.critSec);
}
needDelete = obj->state == CLOSED || obj->state == RETRY_CONNECTION;
}
LeaveCriticalSection(&obj->SockCritSec);
if (needDelete)
ListElt<Socket>::Delete(obj);
}
void Socket::Disconnect(CriticalMap<UUID, Socket*> &critMap) {
int err;
// ----------------------------- enqueue disconnect operation
Buffer *disconnectobj = Buffer::Create(client->inUseBufferList, Buffer::Operation::Disconnect);
if (!SocketManager::DisconnectEx(s, // hSocket : A handle to a connected, connection-oriented socket.
&(disconnectobj->ol), // lpOverlapped : A pointer to an OVERLAPPED structure. If the socket handle has been opened as overlapped, specifying this parameter results in an overlapped (asynchronous) I/O operation.
TF_REUSE_SOCKET, // dwFlags : A set of flags that customizes processing of the function call. TF_REUSE_SOCKET -> Prepares the socket handle to be reused. When the DisconnectEx request completes, the socket handle can be passed to the AcceptEx or ConnectEx function.
0 // reserved : Reserved. Must be zero. If nonzero, WSAEINVAL is returned.
)) {
if ((err = WSAGetLastError()) != WSA_IO_PENDING) {
LOG_ERROR("DisconnectEx failed: %d\n", err);
state = Socket::SocketState::FAILURE;
LeaveCriticalSection(&SockCritSec);
return Socket::DeleteOrDisconnect(this, critMap);
}
}
state = Socket::SocketState::DISCONNECTING;
LeaveCriticalSection(&SockCritSec);
LOG("DisconnectEx ok\n");
}
void Socket::Close(bool forceClose) {
int err;
if (!forceClose) {
// ----------------------------- shutdown connexion
if (shutdown(s, SD_SEND) == SOCKET_ERROR) {
err = WSAGetLastError();
if (err == WSAEINPROGRESS) {
return;
}
LOG_ERROR("shutdown failed / error %d\n", err);
forceClose = true;
}
}
if (forceClose) {
// ------------------------- change socket option to trigger abortive close
linger sl = { 1, //l_onoff : non-zero value enables linger option in kernel.
0 }; //l_linger : timeout interval in seconds.
if (setsockopt(s, //s : A descriptor that identifies a socket.
SOL_SOCKET, //level : The level at which the option is defined.
SO_LINGER, //optname : The socket option for which the value is to be set. The optname parameter must be a socket option defined within the specified level, or behavior is undefined.
reinterpret_cast<char*>(&sl), //optval : A pointer to the buffer in which the value for the requested option is specified.
sizeof(sl) //optlen : The size, in bytes, of the buffer pointed to by the optval parameter.
) == SOCKET_ERROR ) {
err = WSAGetLastError();
LOG_ERROR("setsockopt failed / error %d\n", err);
}
}
// ----------------------------- graceful or abortive close depending on if shutdown failed
if (closesocket(s) == SOCKET_ERROR) {
err = WSAGetLastError();
if (err == WSAEINPROGRESS) {
return;
}
LOG_ERROR("closesocket failed / error %d\n", err);
}
s = INVALID_SOCKET;
state = CLOSED;
}