-
Notifications
You must be signed in to change notification settings - Fork 2
/
SocketWin.cpp
373 lines (315 loc) · 6.53 KB
/
SocketWin.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
#include "SocketWin.h"
#if EASY_WIN
EASY_NS_BEGIN
LPVOID lpMsgBuf = 0;
#ifdef errno
#undef errno
#endif
#define errno WSAGetLastError()
// Note: the \r\n is removed
// LANG_NEUTRAL is for local language.
#define strerror(x) \
(FormatMessage(\
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, \
NULL, \
x, \
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), \
(LPTSTR)&lpMsgBuf, \
0, NULL), ((char*)lpMsgBuf)[strlen((const char*)lpMsgBuf) - 1] = 0, lpMsgBuf)
int SocketWin::__inited = 0;
SocketWin::SocketWin()
: sock(-1)
{
if (__inited++ == 0) {
WSADATA wsdata;
WSAStartup(MAKEWORD(2, 2), &wsdata);
}
}
SocketWin::~SocketWin()
{
close();
if (--__inited == 0) {
WSACleanup();
}
}
bool SocketWin::create(int protocol)
{
close();
if (protocol == -1) {
protocol = this->protocol;
}
if (protocol == 4 || protocol == 6) {
this->protocol = protocol;
int p = protocol == 4 ? PF_INET : PF_INET6;
sock = ::socket(p, socket_type, 0);
if (sock != -1) {
// default: unblock, reuse addr
if (set_unblock) {
unblock();
}
if (set_reuseAddr) {
reuseAddr(true);
}
setSendBufferSize(65535);
return true;
} else {
EASY_LOG("SocketWin::create error: %s", strerror(errno));
return false;
}
} else {
EASY_LOG("SocketWin::create error: invalid protocol %d", protocol);
return false;
}
}
void SocketWin::close(bool hasError)
{
if (sock != -1) {
closesocket(sock);
sock = -1;
SocketBase::close(hasError);
}
}
bool SocketWin::send(const char *buf, size_t len)
{
if (sock == -1) {
setError(0, SocketInvalid);
EASY_LOG("SocketWin::send error: invalid socket");
return false;
}
size_t ret = ::send(sock, buf, len, 0);
if (ret == -1) {
// unblock
int error = errno;
if (error == WSAEWOULDBLOCK) {
EASY_LOG("SocketWin::send warning: send buffer is full.");
} else {
setError(error);
EASY_LOG("SocketWin::send error: %s", strerror(error));
}
return false;
}
return ret == len;
}
int SocketWin::recv(char *buf, size_t len)
{
if (sock == -1) {
setError(0, SocketInvalid);
EASY_LOG("SocketWin::recv error: invalid socket");
return -1;
}
size_t ret = ::recv(sock, buf, len, 0);
if (ret == -1) {
int error = errno;
if (error == WSAEWOULDBLOCK) {
ret = -2;
} else {
setError(error);
EASY_LOG("SocketWin::recv error: %s", strerror(error));
}
}
return ret;
}
void SocketWin::shutdown()
{
::shutdown(sock, SD_BOTH);
}
bool SocketWin::bind(int port, const char *ip)
{
if (sock == -1) {
setError(0, SocketInvalid);
EASY_LOG("SocketWin::bind error: invalid socket");
return false;
}
SockAddr *p = getSockAddr(port, ip);
int ret = ::bind(sock, p->pointer(), p->length());
if (ret != -1) {
return true;
} else {
int error = errno;
setError(error);
EASY_LOG("SocketWin::bind error: %s", strerror(error));
return false;
}
}
bool SocketWin::bind(addrinfo *addrInfo)
{
if (addr) {
addr->release();
}
addr = new SockAddr(addrInfo);
return bind(0, 0);
}
bool SocketWin::listen()
{
if (sock == -1) {
setError(0, SocketInvalid);
EASY_LOG("SocketWin::listen error: invalid socket");
return false;
}
int ret = ::listen(sock, 5);
if (ret != -1) {
if (del) {
del->onListening();
}
return true;
} else {
int error = errno;
setError(error);
EASY_LOG("SocketWin::listen error: %s", strerror(error));
return false;
}
}
bool SocketWin::accept(SockAddr *p)
{
if (sock == -1) {
setError(0, SocketInvalid);
EASY_LOG("SocketWin::accept error: invalid socket");
return false;
}
SockAddr *temp = 0;
if (!p) {
temp = new SockAddr(protocol);
p = temp;
}
socklen_t len = p->length();
int s = ::accept(sock, p->pointer(), &len);
bool ret = true;
if (s != -1) {
SocketWin *conn = new SocketWin();
conn->sock = s;
conn->setSockAddr(p);
if (del) {
del->onConnection(conn);
} else {
delete conn;
conn = 0;
}
} else {
// unblock
int error = errno;
if (error == WSAEWOULDBLOCK) {
// nop
} else {
setError(error);
EASY_LOG("SocketLinux::accept error: %s", strerror(error));
ret = false;
}
}
if (temp) {
temp->release();
}
return ret;
}
bool SocketWin::connect(int port, const char *ip)
{
if (sock == -1) {
setError(0, SocketInvalid);
EASY_LOG("SocketWin::connect error: invalid socket");
return false;
}
SockAddr *p = getSockAddr(port, ip);
int ret = ::connect(sock, p->pointer(), p->length());
if (ret == 0) {
if (del) {
del->onConnect();
}
return true;
} else {
// unblock
int error = errno;
if (ret == -1 && error == WSAEWOULDBLOCK) {
return true;
} else {
setError(error);
EASY_LOG("SocketLinux::connect error: %s", strerror(error));
return false;
}
}
}
bool SocketWin::connect(addrinfo *addrInfo)
{
if (addr) {
addr->release();
}
addr = new SockAddr(addrInfo);
return connect(0, 0);
}
int SocketWin::sendto(const char *buf, size_t len, int port, const char *ip)
{
if (sock == -1) {
setError(0, SocketInvalid);
EASY_LOG("SocketWin::sendto error: invalid socket");
return false;
}
SockAddr *p = getSockAddr(port, ip);
int ret = ::sendto(sock, buf, len, 0, p->pointer(), p->length());
if (ret == -1) {
// unblock
int error = errno;
if (error == WSAEWOULDBLOCK) {
EASY_LOG("SocketWin::sendto warning: send buffer is full.");
} else {
setError(error);
EASY_LOG("SocketWin::sendto error: %s", strerror(error));
}
return false;
}
return ret == len;
}
int SocketWin::sendto(const char *buf, size_t len, addrinfo *addrInfo)
{
if (addr) {
addr->release();
}
addr = new SockAddr(addrInfo);
return sendto(buf, len, 0, 0);
}
int SocketWin::recvfrom(char *buf, size_t len, SockAddr **pp)
{
if (sock == -1) {
setError(0, SocketInvalid);
EASY_LOG("SocketWin::recvfrom error: invalid socket");
return -1;
}
SockAddr *temp = 0;
SockAddr *p = 0;
if (pp) {
p = *pp;
}
if (!p) {
temp = new SockAddr(protocol);
p = temp;
*pp = p;
}
int _len = p->length();
int ret = ::recvfrom(sock, buf, len, 0, p->pointer(), &_len);
if (ret == -1) {
int error = errno;
if (error == WSAEWOULDBLOCK) {
ret = -2;
} else {
setError(error);
EASY_LOG("SocketWin::recvfrom error: %s", strerror(error));
}
}
if (temp) {
temp->release();
}
return ret;
}
const char* SocketWin::formatError(int error)
{
return (const char*)strerror(error);
}
bool SocketWin::unblock()
{
unsigned long ul = 1;
int ret = ioctlsocket(sock, FIONBIO, (unsigned long*)&ul);
if (ret == -1) {
EASY_LOG("SocketWin::unblock.fcntl error: %s", strerror(errno));
return false;
}
return true;
}
EASY_NS_END
#endif