forked from starwing/znet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
znet_iocp.h
692 lines (595 loc) · 19.6 KB
/
znet_iocp.h
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
#if !defined(znet_h) && !defined(ZN_USE_IOCP)
# include "znet.h"
#endif
#if defined(_WIN32) && defined(ZN_USE_IOCP) && !defined(znet_iocp_h)
#define znet_iocp_h
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <Windows.h>
# include <WinSock2.h>
# include <MSWSock.h>
# ifdef _MSC_VER
# pragma warning(disable: 4996) /* deprecated stuffs */
# pragma warning(disable: 4127) /* for do {} while(0) */
# pragma comment(lib, "ws2_32")
# endif /* _MSC_VER */
typedef enum zn_RequestType {
ZN_TACCEPT,
ZN_TRECV,
ZN_TSEND,
ZN_TCONNECT,
ZN_TRECVFROM,
ZN_TSENDTO
} zn_RequestType;
typedef struct zn_Request {
OVERLAPPED overlapped; /* must be first */
zn_RequestType type;
} zn_Request;
typedef struct zn_Post {
zn_PostHandler *handler;
void *ud;
} zn_Post;
struct zn_State {
ZN_STATE_FIELDS
HANDLE iocp;
};
/* utils */
static int znU_set_nodelay(SOCKET socket) {
BOOL bEnable = 1;
return setsockopt(socket, IPPROTO_TCP, TCP_NODELAY,
(const char*)&bEnable, sizeof(bEnable)) == 0;
}
static int znU_update_acceptinfo(SOCKET server, SOCKET client) {
return setsockopt(client, SOL_SOCKET,
SO_UPDATE_ACCEPT_CONTEXT,
(const char*)&server, sizeof(server)) == 0;
}
static int znU_set_reuseaddr(SOCKET socket) {
BOOL bReuseAddr = TRUE;
return setsockopt(socket, SOL_SOCKET, SO_REUSEADDR,
(const char*)&bReuseAddr, sizeof(BOOL)) == 0;
}
/* tcp */
struct zn_Tcp {
znL_entry(zn_Tcp);
zn_State *S;
void *connect_ud; zn_ConnectHandler *connect_handler;
void *send_ud; zn_SendHandler *send_handler;
void *recv_ud; zn_RecvHandler *recv_handler;
zn_Request connect_request;
zn_Request send_request;
zn_Request recv_request;
SOCKET socket;
zn_PeerInfo info;
WSABUF sendBuffer;
WSABUF recvBuffer;
};
static int zn_getextension(SOCKET socket, GUID* gid, void *fn) {
DWORD dwSize = 0;
return WSAIoctl(socket,
SIO_GET_EXTENSION_FUNCTION_POINTER,
gid, sizeof(*gid),
fn, sizeof(void(PASCAL*)(void)),
&dwSize, NULL, NULL) == 0;
}
static int zn_inittcp(zn_Tcp *tcp) {
SOCKET socket;
SOCKADDR_IN localAddr;
if ((socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,
NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
return ZN_ESOCKET;
memset(&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
if (bind(socket, (struct sockaddr *)&localAddr,
sizeof(localAddr)) != 0) {
closesocket(socket);
return ZN_EBIND;
}
if (CreateIoCompletionPort((HANDLE)socket, tcp->S->iocp,
(ULONG_PTR)tcp, 1) == NULL)
{
closesocket(socket);
return ZN_EPOLL;
}
tcp->socket = socket;
return ZN_OK;
}
static void zn_setinfo(zn_Tcp *tcp, const char *addr, unsigned port) {
strcpy(tcp->info.addr, addr);
tcp->info.port = port;
}
ZN_API zn_Tcp* zn_newtcp(zn_State *S) {
ZN_GETOBJECT(S, zn_Tcp, tcp);
tcp->socket = INVALID_SOCKET;
tcp->connect_request.type = ZN_TCONNECT;
tcp->send_request.type = ZN_TSEND;
tcp->recv_request.type = ZN_TRECV;
return tcp;
}
ZN_API void zn_deltcp(zn_Tcp *tcp) {
zn_closetcp(tcp);
/* if a request is on-the-go, then the IOCP will give callback, we
* delete object on that callback.
* same as zn_delaccept() and zn_deludp() */
if (tcp->recv_handler == NULL
&& tcp->send_handler == NULL
&& tcp->connect_handler == NULL)
ZN_PUTOBJECT(tcp);
}
ZN_API int zn_closetcp(zn_Tcp *tcp) {
int ret = ZN_OK;
if (tcp->socket != INVALID_SOCKET) {
if (closesocket(tcp->socket) != 0)
ret = ZN_ERROR;
tcp->socket = INVALID_SOCKET;
}
return ret;
}
ZN_API void zn_getpeerinfo(zn_Tcp *tcp, zn_PeerInfo *info) {
*info = tcp->info;
}
ZN_API int zn_connect(zn_Tcp *tcp, const char *addr, unsigned port, zn_ConnectHandler *cb, void *ud) {
static LPFN_CONNECTEX lpConnectEx = NULL;
static GUID gid = WSAID_CONNECTEX;
DWORD dwLength = 0;
SOCKADDR_IN remoteAddr;
char buf[1];
int err;
if (tcp->socket != INVALID_SOCKET) return ZN_ESTATE;
if (tcp->connect_handler != NULL) return ZN_EBUSY;
if (cb == NULL) return ZN_EPARAM;
if ((err = zn_inittcp(tcp)) != ZN_OK)
return err;
if (!lpConnectEx && !zn_getextension(tcp->socket, &gid, &lpConnectEx))
return ZN_ECONNECT;
memset(&remoteAddr, 0, sizeof(remoteAddr));
remoteAddr.sin_family = AF_INET;
remoteAddr.sin_addr.s_addr = inet_addr(addr);
remoteAddr.sin_port = htons((unsigned short)port);
zn_setinfo(tcp, addr, port);
if (!lpConnectEx(tcp->socket,
(struct sockaddr *)&remoteAddr, sizeof(remoteAddr),
buf, 0, &dwLength, &tcp->connect_request.overlapped)
&& WSAGetLastError() != ERROR_IO_PENDING)
{
closesocket(tcp->socket);
tcp->socket = INVALID_SOCKET;
return ZN_ECONNECT;
}
++tcp->S->waitings;
tcp->connect_handler = cb;
tcp->connect_ud = ud;
return ZN_OK;
}
ZN_API int zn_send(zn_Tcp *tcp, const char *buff, unsigned len, zn_SendHandler *cb, void *ud) {
DWORD dwTemp1=0;
if (tcp->socket == INVALID_SOCKET) return ZN_ESTATE;
if (tcp->send_handler != NULL) return ZN_EBUSY;
if (cb == NULL || len == 0) return ZN_EPARAM;
tcp->sendBuffer.buf = (char*)buff;
tcp->sendBuffer.len = len;
if (WSASend(tcp->socket, &tcp->sendBuffer, 1,
&dwTemp1, 0, &tcp->send_request.overlapped, NULL) != 0
&& WSAGetLastError() != WSA_IO_PENDING)
{
tcp->sendBuffer.buf = NULL;
tcp->sendBuffer.len = 0;
zn_closetcp(tcp);
return ZN_ERROR;
}
++tcp->S->waitings;
tcp->send_handler = cb;
tcp->send_ud = ud;
return ZN_OK;
}
ZN_API int zn_recv(zn_Tcp *tcp, char *buff, unsigned len, zn_RecvHandler *cb, void *ud) {
DWORD dwRecv = 0;
DWORD dwFlag = 0;
if (tcp->socket == INVALID_SOCKET) return ZN_ESTATE;
if (tcp->recv_handler != NULL) return ZN_EBUSY;
if (cb == NULL || len == 0) return ZN_EPARAM;
tcp->recvBuffer.buf = buff;
tcp->recvBuffer.len = len;
if (WSARecv(tcp->socket, &tcp->recvBuffer, 1,
&dwRecv, &dwFlag, &tcp->recv_request.overlapped, NULL) != 0
&& WSAGetLastError() != WSA_IO_PENDING)
{
tcp->recvBuffer.buf = NULL;
tcp->recvBuffer.len = 0;
zn_closetcp(tcp);
return ZN_ERROR;
}
++tcp->S->waitings;
tcp->recv_handler = cb;
tcp->recv_ud = ud;
return ZN_OK;
}
static void zn_onconnect(zn_Tcp *tcp, BOOL bSuccess) {
zn_ConnectHandler *cb = tcp->connect_handler;
assert(tcp->connect_handler);
tcp->connect_handler = NULL;
if (tcp->socket == INVALID_SOCKET) {
/* cb(tcp->connect_ud, tcp, ZN_ECLOSED); */
ZN_PUTOBJECT(tcp);
return;
}
if (!bSuccess) zn_closetcp(tcp);
else znU_set_nodelay(tcp->socket);
cb(tcp->connect_ud, tcp, bSuccess ? ZN_OK : ZN_ERROR);
}
static void zn_onsend(zn_Tcp *tcp, BOOL bSuccess, DWORD dwBytes) {
zn_SendHandler *cb = tcp->send_handler;
assert(tcp->send_handler);
tcp->send_handler = NULL;
if (tcp->socket == INVALID_SOCKET) {
assert(tcp->socket == INVALID_SOCKET);
/* cb(tcp->send_ud, tcp, ZN_ECLOSED, dwBytes); */
if (tcp->recv_handler == NULL)
ZN_PUTOBJECT(tcp);
return;
}
if (!bSuccess) zn_closetcp(tcp);
cb(tcp->send_ud, tcp, bSuccess ? ZN_OK : ZN_ERROR, dwBytes);
}
static void zn_onrecv(zn_Tcp *tcp, BOOL bSuccess, DWORD dwBytes) {
zn_RecvHandler *cb = tcp->recv_handler;
assert(tcp->recv_handler);
tcp->recv_handler = NULL;
if (tcp->socket == INVALID_SOCKET) {
/* cb(tcp->recv_ud, tcp, ZN_ECLOSED, dwBytes); */
if (tcp->send_handler == NULL)
ZN_PUTOBJECT(tcp);
return;
}
if (dwBytes == 0 || tcp->socket == INVALID_SOCKET) {
zn_closetcp(tcp);
cb(tcp->recv_ud, tcp, ZN_ECLOSED, dwBytes);
}
else if (!bSuccess) {
zn_closetcp(tcp);
cb(tcp->recv_ud, tcp, ZN_EHANGUP, dwBytes);
}
else
cb(tcp->recv_ud, tcp, ZN_OK, dwBytes);
}
/* accept */
struct zn_Accept {
znL_entry(zn_Accept);
zn_State *S;
void *accept_ud; zn_AcceptHandler *accept_handler;
zn_Request accept_request;
SOCKET socket;
SOCKET client;
DWORD recv_length;
char recv_buffer[(sizeof(SOCKADDR_IN)+16)*2];
};
ZN_API zn_Accept* zn_newaccept(zn_State *S) {
ZN_GETOBJECT(S, zn_Accept, accept);
accept->socket = INVALID_SOCKET;
accept->client = INVALID_SOCKET;
accept->accept_request.type = ZN_TACCEPT;
return accept;
}
ZN_API void zn_delaccept(zn_Accept *accept) {
zn_closeaccept(accept);
if (accept->accept_handler == NULL)
ZN_PUTOBJECT(accept);
}
ZN_API int zn_closeaccept(zn_Accept *accept) {
int ret = ZN_OK;
if (accept->socket != INVALID_SOCKET) {
if (closesocket(accept->socket) != 0)
ret = ZN_ERROR;
accept->socket = INVALID_SOCKET;
}
if (accept->client != INVALID_SOCKET) {
if (closesocket(accept->client) != 0)
ret = ZN_ERROR;
accept->client = INVALID_SOCKET;
}
return ret;
}
ZN_API int zn_listen(zn_Accept *accept, const char *addr, unsigned port) {
SOCKADDR_IN sockAddr;
SOCKET socket;
if (accept->socket != INVALID_SOCKET) return ZN_ESTATE;
if (accept->accept_handler != NULL) return ZN_EBUSY;
if ((socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,
NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
return ZN_ESOCKET;
znU_set_reuseaddr(accept->socket);
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr(addr);
sockAddr.sin_port = htons((unsigned short)port);
if (bind(socket, (struct sockaddr *)&sockAddr,
sizeof(sockAddr)) != 0)
{
closesocket(socket);
return ZN_EBIND;
}
if (listen(socket, SOMAXCONN) != 0) {
closesocket(socket);
return ZN_ERROR;
}
if (CreateIoCompletionPort((HANDLE)socket, accept->S->iocp,
(ULONG_PTR)accept, 1) == NULL)
{
closesocket(socket);
return ZN_EPOLL;
}
accept->socket = socket;
return ZN_OK;
}
ZN_API int zn_accept(zn_Accept *accept, zn_AcceptHandler *cb, void *ud) {
static GUID gid = WSAID_ACCEPTEX;
static LPFN_ACCEPTEX lpAcceptEx = NULL;
SOCKET socket;
if (accept->socket == INVALID_SOCKET) return ZN_ESTATE;
if (cb == NULL) return ZN_EPARAM;
if (!lpAcceptEx && !zn_getextension(accept->socket, &gid, &lpAcceptEx))
return ZN_ERROR;
if ((socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,
NULL, 0, WSA_FLAG_OVERLAPPED)) == INVALID_SOCKET)
return ZN_ESOCKET;
if (!lpAcceptEx(accept->socket, socket,
accept->recv_buffer, 0,
sizeof(SOCKADDR_IN)+16, sizeof(SOCKADDR_IN)+16,
&accept->recv_length, &accept->accept_request.overlapped)
&& WSAGetLastError() != ERROR_IO_PENDING)
{
closesocket(socket);
return ZN_ERROR;
}
++accept->S->waitings;
accept->accept_handler = cb;
accept->accept_ud = ud;
accept->client = socket;
return ZN_OK;
}
static void zn_onaccept(zn_Accept *accept, BOOL bSuccess) {
static LPFN_GETACCEPTEXSOCKADDRS lpGetAcceptExSockaddrs = NULL;
static GUID gid = WSAID_GETACCEPTEXSOCKADDRS;
zn_AcceptHandler *cb = accept->accept_handler;
zn_Tcp *tcp;
struct sockaddr *paddr1 = NULL, *paddr2 = NULL;
int tmp1 = 0, tmp2 = 0;
accept->accept_handler = NULL;
if (accept->socket == INVALID_SOCKET) {
ZN_PUTOBJECT(accept);
return;
}
if (!bSuccess) {
/* zn_closeaccept(accept); */
cb(accept->accept_ud, accept, ZN_ERROR, NULL);
return;
}
znU_update_acceptinfo(accept->socket, accept->client);
znU_set_nodelay(accept->client);
if (!lpGetAcceptExSockaddrs && !zn_getextension(accept->client,
&gid, &lpGetAcceptExSockaddrs))
return;
lpGetAcceptExSockaddrs(accept->recv_buffer,
accept->recv_length,
sizeof(SOCKADDR_IN)+16,
sizeof(SOCKADDR_IN)+16,
&paddr1, &tmp1, &paddr2, &tmp2);
tcp = zn_newtcp(accept->S);
tcp->socket = accept->client;
accept->client = INVALID_SOCKET;
if (CreateIoCompletionPort((HANDLE)tcp->socket,
tcp->S->iocp, (ULONG_PTR)tcp, 1) == NULL)
{
closesocket(tcp->socket);
ZN_PUTOBJECT(tcp);
return;
}
zn_setinfo(tcp,
inet_ntoa(((struct sockaddr_in*)paddr2)->sin_addr),
ntohs(((struct sockaddr_in*)paddr2)->sin_port));
cb(accept->accept_ud, accept, ZN_OK, tcp);
}
/* udp */
struct zn_Udp {
znL_entry(zn_Udp);
zn_State *S;
void *recv_ud; zn_RecvFromHandler *recv_handler;
zn_Request recv_request;
SOCKET socket;
WSABUF recvBuffer;
SOCKADDR_IN recvFrom;
INT recvFromLen;
};
static int zn_initudp(zn_Udp *udp, const char *addr, unsigned port) {
SOCKET socket;
SOCKADDR_IN sockAddr;
memset(&sockAddr, 0, sizeof(sockAddr));
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = inet_addr(addr);
sockAddr.sin_port = htons((unsigned short)port);
socket = WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0,
WSA_FLAG_OVERLAPPED);
if (socket == INVALID_SOCKET)
return ZN_ESOCKET;
if (bind(socket, (struct sockaddr*)&sockAddr, sizeof(SOCKADDR_IN)) != 0)
return ZN_EBIND;
if (CreateIoCompletionPort((HANDLE)socket, udp->S->iocp,
(ULONG_PTR)udp, 1) == NULL)
{
closesocket(socket);
return ZN_EPOLL;
}
udp->socket = socket;
return ZN_OK;
}
ZN_API zn_Udp* zn_newudp(zn_State *S, const char *addr, unsigned port) {
ZN_GETOBJECT(S, zn_Udp, udp);
udp->socket = INVALID_SOCKET;
udp->recv_request.type = ZN_TRECVFROM;
if (!zn_initudp(udp, addr, port)) {
ZN_PUTOBJECT(udp);
return NULL;
}
return udp;
}
ZN_API void zn_deludp(zn_Udp *udp) {
closesocket(udp->socket);
if (udp->recv_handler == NULL)
ZN_PUTOBJECT(udp);
}
ZN_API int zn_sendto(zn_Udp *udp, const char *buff, unsigned len, const char *addr, unsigned port) {
SOCKADDR_IN dst;
if (udp->socket == INVALID_SOCKET) return ZN_ESTATE;
if (len == 0 || len >1200) return ZN_EPARAM;
memset(&dst, 0, sizeof(SOCKADDR_IN));
dst.sin_family = AF_INET;
dst.sin_addr.s_addr = inet_addr(addr);
dst.sin_port = htons((unsigned short)port);
sendto(udp->socket, buff, len, 0, (struct sockaddr*)&dst, sizeof(dst));
return ZN_OK;
}
ZN_API int zn_recvfrom(zn_Udp *udp, char *buff, unsigned len, zn_RecvFromHandler *cb, void *ud) {
DWORD dwRecv = 0;
DWORD dwFlag = 0;
if (udp->socket == INVALID_SOCKET) return ZN_ESTATE;
if (udp->recv_handler) return ZN_EBUSY;
if (len == 0 || cb == NULL) return ZN_EPARAM;
udp->recvBuffer.buf = buff;
udp->recvBuffer.len = len;
memset(&udp->recvFrom, 0, sizeof(udp->recvFrom));
udp->recvFromLen = sizeof(udp->recvFrom);
if ((WSARecvFrom(udp->socket, &udp->recvBuffer, 1, &dwRecv, &dwFlag,
(struct sockaddr*)&udp->recvFrom, &udp->recvFromLen,
&udp->recv_request.overlapped, NULL) != 0)
&& WSAGetLastError() != WSA_IO_PENDING)
{
udp->recvBuffer.buf = NULL;
udp->recvBuffer.len = 0;
return ZN_ERROR;
}
++udp->S->waitings;
udp->recv_handler = cb;
udp->recv_ud = ud;
return ZN_OK;
}
static void zn_onrecvfrom(zn_Udp *udp, BOOL bSuccess, DWORD dwBytes) {
zn_RecvFromHandler *cb = udp->recv_handler;
assert(udp->recv_handler);
udp->recv_handler = NULL;
if (udp->socket == INVALID_SOCKET) {
ZN_PUTOBJECT(udp);
return;
}
if (bSuccess && dwBytes > 0)
cb(udp->recv_ud, udp, ZN_OK, dwBytes,
inet_ntoa(((struct sockaddr_in*)&udp->recvFrom)->sin_addr),
ntohs(udp->recvFrom.sin_port));
else cb(udp->recv_ud, udp, ZN_ERROR, dwBytes,
"0.0.0.0", 0);
}
/* poll */
static BOOL zn_initialized = FALSE;
static LARGE_INTEGER counterFreq;
static LARGE_INTEGER startTime;
ZN_API const char *zn_engine(void) { return "IOCP"; }
ZN_API void zn_initialize(void) {
if (!zn_initialized) {
WORD version = MAKEWORD(2, 2);
WSADATA d;
if (WSAStartup(version, &d) != 0) {
abort();
}
zn_initialized = TRUE;
}
}
ZN_API void zn_deinitialize(void) {
if (zn_initialized) {
WSACleanup();
zn_initialized = FALSE;
}
}
ZN_API zn_Time zn_time(void) {
LARGE_INTEGER current;
QueryPerformanceCounter(¤t);
return (zn_Time)((current.QuadPart - startTime.QuadPart) * 1000
/ counterFreq.LowPart);
}
ZN_API int zn_post(zn_State *S, zn_PostHandler *cb, void *ud) {
zn_Post *ps = (zn_Post*)malloc(sizeof(zn_Post));
if (ps == NULL) return 0;
ps->handler = cb;
ps->ud = ud;
PostQueuedCompletionStatus(S->iocp, 0, 0, (LPOVERLAPPED)ps);
return 1;
}
static int znS_init(zn_State *S) {
if (counterFreq.QuadPart == 0) {
QueryPerformanceFrequency(&counterFreq);
QueryPerformanceCounter(&startTime);
assert(counterFreq.HighPart == 0);
}
S->iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
NULL, (ULONG_PTR)0, 1);
return S->iocp != NULL;
}
static void znS_close(zn_State *S) {
/* use IOCP to wait all closed but not deleted object,
* and delete them. */
while (S->waitings != 0) {
assert(S->waitings > 0);
zn_run(S, ZN_RUN_ONCE);
}
CloseHandle(S->iocp);
}
static int znS_poll(zn_State *S, int checkonly) {
DWORD dwBytes = 0;
ULONG_PTR upComKey = (ULONG_PTR)0;
LPOVERLAPPED pOverlapped = NULL;
DWORD timeout = 0;
zn_Time current;
zn_Request *req;
BOOL bRet;
S->status = ZN_STATUS_IN_RUN;
znT_updatetimers(S, current = zn_time());
if (!checkonly) {
zn_Time ms = znT_gettimeout(S, current);
timeout = ms >= INFINITE ? INFINITE : (DWORD)ms;
}
bRet = GetQueuedCompletionStatus(S->iocp,
&dwBytes, &upComKey, &pOverlapped, timeout);
znT_updatetimers(S, zn_time());
if (!bRet && !pOverlapped) /* time out */
goto out;
if (upComKey == 0) {
zn_Post *ps = (zn_Post*)pOverlapped;
if (ps->handler)
ps->handler(ps->ud, S);
free(ps);
goto out;
}
req = (zn_Request*)pOverlapped;
--S->waitings;
switch (req->type) {
case ZN_TACCEPT: zn_onaccept((zn_Accept*)upComKey, bRet); break;
case ZN_TRECV: zn_onrecv((zn_Tcp*)upComKey, bRet, dwBytes); break;
case ZN_TSEND: zn_onsend((zn_Tcp*)upComKey, bRet, dwBytes); break;
case ZN_TCONNECT: zn_onconnect((zn_Tcp*)upComKey, bRet); break;
case ZN_TRECVFROM: zn_onrecvfrom((zn_Udp*)upComKey, bRet, dwBytes); break;
case ZN_TSENDTO: /* do not have this operation */
default: ;
}
out:
if (S->status == ZN_STATUS_CLOSING_IN_RUN) {
S->status = ZN_STATUS_READY; /* trigger real close */
zn_close(S);
return 0;
}
S->status = ZN_STATUS_READY;
return znT_hastimers(S) || S->waitings != 0;
}
#endif /* ZN_USE_IOCP */
/* win32cc: flags+='-s -O3 -mdll -DZN_IMPLEMENTATION -xc'
* win32cc: libs+='-lws2_32' output='znet.dll' */
/* unixcc: flags+='-s -O3 -shared -fPIC -DZN_IMPLEMENTATION -xc'
* unixcc: libs+='-lpthread' output='znet.so' */