Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
556 lines (487 loc) · 10.5 KB

File metadata and controls

556 lines (487 loc) · 10.5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include "Socket.h"
#ifdef POSIX
#include <sys/types.h>
#include <sys/socket.h>
#include <poll.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <fcntl.h>
#endif
namespace Socket
{
bool Socket::Startup() noexcept
{
#ifdef WIN32
unsigned short version = MAKEWORD(2, 2);
::WSADATA wsaData = {0};
return 0 == ::WSAStartup(version, &wsaData);
#elif POSIX
return true;
#else
#error "Undefined platform"
#endif
}
bool Socket::Cleanup() noexcept
{
#ifdef WIN32
return 0 == ::WSACleanup();
#elif POSIX
return true;
#else
#error "Undefined platform"
#endif
}
int Socket::getLastError() noexcept
{
#ifdef WIN32
return ::WSAGetLastError();
#elif POSIX
return errno;
#else
#error "Undefined platform"
#endif
}
Socket::Socket() noexcept : socket_handle(~0) {}
Socket::Socket(const System::native_socket_type fd) noexcept : socket_handle(fd) {}
Socket::Socket(const Socket &obj) noexcept : socket_handle(obj.socket_handle) {}
Socket::Socket(Socket &&obj) noexcept
: socket_handle(obj.socket_handle)
{
obj.socket_handle = ~0;
}
bool Socket::open() noexcept {
this->close();
this->socket_handle = ::socket(AF_INET, SOCK_STREAM, 0);
return this->is_open();
}
bool Socket::close() noexcept
{
if (this->is_open() )
{
#ifdef WIN32
const int result = ::closesocket(this->socket_handle);
#elif POSIX
const int result = ::close(this->socket_handle);
#else
#error "Undefined platform"
#endif
if (0 == result) {
this->socket_handle = ~0;
return true;
}
}
return false;
}
bool Socket::is_open() const noexcept
{
#ifdef WIN32
return INVALID_SOCKET != this->socket_handle;
#elif POSIX
return ~0 != this->socket_handle;
#else
#error "Undefined platform"
#endif
}
System::native_socket_type Socket::get_handle() const noexcept {
return this->socket_handle;
}
bool Socket::bind(const int port) const noexcept {
// GCC bug with global namespace: https://bbs.archlinux.org/viewtopic.php?id=53751
auto const net_port = htons(static_cast<uint16_t>(port));
auto const net_addr = ::htonl(INADDR_ANY);
const ::sockaddr_in sock_addr {
AF_INET,
net_port,
{ net_addr },
{ 0 } // sin_zero
};
return 0 == ::bind(
this->socket_handle,
reinterpret_cast<const ::sockaddr *>(&sock_addr),
sizeof(::sockaddr_in)
);
}
bool Socket::listen() const noexcept {
return 0 == ::listen(this->socket_handle, SOMAXCONN);
}
Socket Socket::accept() const noexcept
{
#ifdef WIN32
System::native_socket_type client_socket = ::accept(
this->socket_handle,
static_cast<sockaddr *>(nullptr),
static_cast<int *>(nullptr)
);
#elif POSIX
System::native_socket_type client_socket = ::accept(
this->socket_handle,
static_cast<sockaddr *>(nullptr),
static_cast<socklen_t *>(nullptr)
);
#else
#error "Undefined platform"
#endif
return Socket(client_socket);
}
Socket Socket::nonblock_accept() const noexcept
{
System::native_socket_type client_socket = ~0;
#ifdef WIN32
WSAPOLLFD event {
this->socket_handle,
POLLRDNORM,
0
};
if (1 == ::WSAPoll(&event, 1, ~0) && event.revents & POLLRDNORM) {
client_socket = ::accept(
this->socket_handle,
static_cast<sockaddr *>(nullptr),
static_cast<int *>(nullptr)
);
}
#elif POSIX
struct ::pollfd event {
this->socket_handle,
POLLIN,
0
};
if (1 == ::poll(&event, 1, ~0) && event.revents & POLLIN) {
client_socket = ::accept(
this->socket_handle,
static_cast<sockaddr *>(nullptr),
static_cast<socklen_t *>(nullptr)
);
}
#else
#error "Undefined platform"
#endif
return Socket(client_socket);
}
Socket Socket::nonblock_accept(const std::chrono::milliseconds &timeout) const noexcept
{
System::native_socket_type client_socket = ~0;
#ifdef WIN32
WSAPOLLFD event {
this->socket_handle,
POLLRDNORM,
0
};
if (1 == ::WSAPoll(&event, 1, int(timeout.count()) ) && event.revents & POLLRDNORM) {
client_socket = ::accept(
this->socket_handle,
static_cast<sockaddr *>(nullptr),
static_cast<int *>(nullptr)
);
}
#elif POSIX
struct ::pollfd event {
this->socket_handle,
POLLIN,
0
};
if (1 == ::poll(&event, 1, int(timeout.count()) ) && event.revents & POLLIN) {
client_socket = ::accept(
this->socket_handle,
static_cast<sockaddr *>(nullptr),
static_cast<socklen_t *>(nullptr)
);
}
#else
#error "Undefined platform"
#endif
return Socket(client_socket);
}
bool Socket::shutdown() const noexcept
{
if (is_open() )
{
#ifdef WIN32
return 0 == ::shutdown(this->socket_handle, SD_BOTH);
#elif POSIX
return 0 == ::shutdown(this->socket_handle, SHUT_RDWR);
#else
#error "Undefined platform"
#endif
}
return false;
}
bool Socket::nonblock(const bool isNonBlock) const noexcept
{
#ifdef WIN32
unsigned long value = isNonBlock;
return 0 == ::ioctlsocket(
this->socket_handle,
FIONBIO,
&value
);
#elif POSIX
return ~0 != ::fcntl(
this->socket_handle,
F_SETFL,
isNonBlock
? O_NONBLOCK
: O_SYNC
);
#else
#error "Undefined platform"
#endif
}
/*
bool Socket::is_nonblock() const noexcept
{
#ifdef WIN32
#elif POSIX
const int flags = ::fcntl(socket_handle, F_GETFL, 0);
return (flags != ~0) && (flags & O_NONBLOCK);
#else
#error "Undefined platform"
#endif
}
*/
bool Socket::tcp_nodelay(const bool nodelay) const noexcept
{
#ifdef WIN32
int flags = nodelay ? 1 : 0;
return 0 == ::setsockopt(
this->socket_handle,
IPPROTO_TCP,
TCP_NODELAY,
reinterpret_cast<char *>(&flags),
sizeof(flags)
);
#elif POSIX
int flags = nodelay ? 1 : 0;
return 0 == ::setsockopt(
this->socket_handle,
IPPROTO_TCP,
TCP_NODELAY,
&flags,
sizeof(flags)
);
#else
#error "Undefined platform"
#endif
}
long Socket::recv(
std::vector<std::string::value_type> &buf
) const noexcept {
return this->recv(buf.data(), buf.size() );
}
long Socket::recv(
void *buf,
const size_t length
) const noexcept {
#ifdef WIN32
return ::recv(
this->socket_handle,
reinterpret_cast<char *>(buf),
static_cast<const int>(length),
0
);
#elif POSIX
return ::recv(this->socket_handle, buf, length, 0);
#else
#error "Undefined platform"
#endif
}
long Socket::nonblock_recv(
std::vector<std::string::value_type> &buf,
const std::chrono::milliseconds &timeout
) const noexcept {
return this->nonblock_recv(
buf.data(),
buf.size(),
timeout
);
}
long Socket::nonblock_recv(
void *buf,
const size_t length,
const std::chrono::milliseconds &timeout
) const noexcept {
long recv_len = ~0;
#ifdef WIN32
WSAPOLLFD event {
this->socket_handle,
POLLRDNORM,
0
};
if (1 == ::WSAPoll(&event, 1, int(timeout.count()) ) && event.revents & POLLRDNORM) {
recv_len = ::recv(
this->socket_handle,
reinterpret_cast<char *>(buf),
static_cast<const int>(length),
0
);
}
#elif POSIX
struct ::pollfd event {
this->socket_handle,
POLLIN,
0
};
if (1 == ::poll(&event, 1, int(timeout.count()) ) && event.revents & POLLIN) {
recv_len = ::recv(this->socket_handle, buf, length, 0);
}
#else
#error "Undefined platform"
#endif
return recv_len;
}
bool Socket::nonblock_recv_sync(
const std::chrono::milliseconds &timeout
) const noexcept {
#ifdef WIN32
WSAPOLLFD event {
this->socket_handle,
POLLIN,
0
};
return ::WSAPoll(&event, 1, int(timeout.count()) ) == 1;
#elif POSIX
struct ::pollfd event {
this->socket_handle,
POLLIN,
0
};
return ::poll(&event, 1, int(timeout.count()) ) == 1;
#else
#error "Undefined platform"
#endif
}
static long send_all(
const System::native_socket_type socket_handle,
const void *data,
const size_t length
) noexcept {
size_t total = 0;
while (total < length) {
const long send_size = ::send(
socket_handle,
reinterpret_cast<const char *>(data) + total,
length - total,
0
);
if (send_size < 0) {
return send_size;
}
total += size_t(send_size);
}
return static_cast<long>(total);
}
long Socket::send(const std::string &buf) const noexcept {
return this->send(buf.data(), buf.length() );
}
long Socket::send(const void *buf, const size_t length) const noexcept {
return send_all(this->socket_handle, buf, length);
}
static long nonblock_send_all(
const System::native_socket_type socket_handle,
const void *data,
const size_t length,
const std::chrono::milliseconds &timeout
) noexcept {
size_t total = 0;
#ifdef WIN32
WSAPOLLFD event = {
socket_handle,
POLLOUT,
0
};
while (total < length) {
if (1 == ::WSAPoll(&event, 1, int(timeout.count()) ) && event.revents & POLLOUT) {
const long send_size = ::send(
socket_handle,
reinterpret_cast<const char *>(data) + total,
static_cast<const int>(length - total),
0
);
if (send_size < 0) {
return send_size;
}
total += size_t(send_size);
} else {
return -1;
}
}
#elif POSIX
struct ::pollfd event = {
socket_handle,
POLLOUT,
0
};
while (total < length) {
if (1 == ::poll(&event, 1, int(timeout.count()) ) && event.revents & POLLOUT) {
const long send_size = ::send(
socket_handle,
reinterpret_cast<const uint8_t *>(data) + total,
length - total,
0
);
if (send_size < 0) {
return send_size;
}
total += size_t(send_size);
} else {
return -1;
}
}
#else
#error "Undefined platform"
#endif
return long(total);
}
long Socket::nonblock_send(
const std::string &buf,
const std::chrono::milliseconds &timeout
) const noexcept {
return this->nonblock_send(
buf.data(),
buf.length(),
timeout
);
}
long Socket::nonblock_send(
const void *buf,
const size_t length,
const std::chrono::milliseconds &timeout
) const noexcept {
return nonblock_send_all(
this->socket_handle,
buf,
length,
timeout
);
}
void Socket::nonblock_send_sync() const noexcept
{
#ifdef WIN32
WSAPOLLFD event {
this->socket_handle,
POLLOUT,
0
};
::WSAPoll(&event, 1, ~0);
#elif POSIX
struct ::pollfd event {
this->socket_handle,
POLLOUT,
0
};
::poll(&event, 1, ~0);
#else
#error "Undefined platform"
#endif
}
Socket &Socket::operator=(const Socket &obj) noexcept {
this->socket_handle = obj.socket_handle;
return *this;
}
bool Socket::operator ==(const Socket &obj) const noexcept {
return this->socket_handle == obj.socket_handle;
}
bool Socket::operator !=(const Socket &obj) const noexcept {
return this->socket_handle != obj.socket_handle;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.