forked from starwing/znet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zn_lua.c
776 lines (703 loc) · 22 KB
/
zn_lua.c
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#define LUA_LIB
#define LBIND_STATIC_API
#define LBIND_DEFAULT_FLAG (LBIND_TRACK|LBIND_ACCESSOR)
#include "lbind.h"
#define ZN_IMPLEMENTATION
#include "znet.h"
#include "zn_buffer.h"
#include "zn_addrinfo.h"
#define LZN_UDP_RECVSIZE 2048
LBIND_TYPE(lbT_State, "znet.State");
LBIND_TYPE(lbT_Timer, "znet.Timer");
LBIND_TYPE(lbT_Accept, "znet.Accept");
LBIND_TYPE(lbT_Tcp, "znet.Tcp");
LBIND_TYPE(lbT_Udp, "znet.Udp");
/* utils */
#define return_result(L, err) do { \
if (err == ZN_OK) lbind_returnself(L); \
else return lzn_pushresult(L, err); } while (0)
static int lzn_pushresult(lua_State *L, int err) {
lua_pushnil(L);
lua_pushstring(L, zn_strerror(err));
lua_pushinteger(L, err);
return 3;
}
static lua_Integer lzn_posrelat(lua_Integer pos, size_t len) {
if (pos >= 0) return pos;
else if (0u - (size_t)pos > len) return 0;
else return (lua_Integer)len + pos + 1;
}
static void lzn_ref(lua_State *L, int idx, int *ref) {
lua_pushvalue(L, idx);
if (*ref == LUA_NOREF)
*ref = luaL_ref(L, LUA_REGISTRYINDEX);
else
lua_rawseti(L, LUA_REGISTRYINDEX, *ref);
}
static void lzn_unref(lua_State *L, int *ref) {
luaL_unref(L, LUA_REGISTRYINDEX, *ref);
*ref = LUA_NOREF;
}
/* znet timer */
typedef struct lzn_Timer {
zn_Timer *timer;
lua_State *L;
int ontimer_ref;
int ref;
unsigned delayms;
} lzn_Timer;
static zn_Time lzn_ontimer(void *ud, zn_Timer *timer, zn_Time elapsed) {
lzn_Timer *obj = (lzn_Timer*)ud;
lua_State *L = obj->L;
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->ontimer_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->ref);
lua_pushinteger(L, (lua_Integer)elapsed);
if (lbind_pcall(L, 2, 1) != LUA_OK) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lzn_unref(L, &obj->ref);
}
else if (lua_isinteger(L, -1)) {
lua_Integer ret = lua_tointeger(L, -1);
return ret >= 0 ? (zn_Time)ret : 0;
}
else if (lua_toboolean(L, -1))
zn_starttimer(timer, obj->delayms);
else
lzn_unref(L, &obj->ref);
lua_pop(L, 1);
return 0;
}
static int Ltimer_new(lua_State *L) {
zn_State *S = (zn_State*)lbind_check(L, 1, &lbT_State);
lzn_Timer *obj;
luaL_checktype(L, 2, LUA_TFUNCTION);
obj = (lzn_Timer*)lbind_new(L, sizeof(lzn_Timer), &lbT_Timer);
obj->timer = zn_newtimer(S, lzn_ontimer, obj);
obj->L = L;
obj->ontimer_ref = LUA_NOREF;
obj->ref = LUA_NOREF;
if (obj->timer != NULL) {
lzn_ref(L, 2, &obj->ontimer_ref);
return 1;
}
return 0;
}
static int Ltimer_delete(lua_State *L) {
lzn_Timer *obj = (lzn_Timer*)lbind_test(L, 1, &lbT_Timer);
if (obj->timer != NULL) {
zn_deltimer(obj->timer);
lbind_delete(L, 1);
lzn_unref(L, &obj->ontimer_ref);
lzn_unref(L, &obj->ref);
obj->timer = NULL;
}
return 0;
}
static int Ltimer_start(lua_State *L) {
lzn_Timer *obj = (lzn_Timer*)lbind_check(L, 1, &lbT_Timer);
lua_Integer delayms = luaL_optinteger(L, 2, 0);
if (!obj->timer) return 0;
if (delayms < 0) delayms = 0;
obj->delayms = (unsigned)delayms;
if (zn_starttimer(obj->timer, obj->delayms))
lzn_ref(L, 1, &obj->ref);
lbind_returnself(L);
}
static int Ltimer_cancel(lua_State *L) {
lzn_Timer *obj = (lzn_Timer*)lbind_check(L, 1, &lbT_Timer);
if (!obj->timer) return 0;
lzn_unref(L, &obj->ref);
zn_canceltimer(obj->timer);
lbind_returnself(L);
}
static void open_timer(lua_State *L) {
luaL_Reg libs[] = {
#define ENTRY(name) { #name, Ltimer_##name }
ENTRY(new),
ENTRY(delete),
ENTRY(start),
ENTRY(cancel),
#undef ENTRY
{ NULL, NULL }
};
lbind_newmetatable(L, libs, &lbT_Timer);
}
/* znet tcp */
typedef struct lzn_Tcp {
zn_Tcp *tcp;
zn_State *S;
lua_State *L;
int onconnect_ref;
int onheader_ref;
int onpacket_ref;
int onerror_ref;
int ref;
int closing;
zn_SendBuffer send;
zn_RecvBuffer recv;
} lzn_Tcp;
static void lzn_freetcp(lua_State *L, lzn_Tcp *obj) {
zn_resetsendbuffer(&obj->send);
zn_resetrecvbuffer(&obj->recv);
lzn_unref(L, &obj->onconnect_ref);
lzn_unref(L, &obj->onheader_ref);
lzn_unref(L, &obj->onpacket_ref);
lzn_unref(L, &obj->onerror_ref);
lzn_unref(L, &obj->ref);
obj->tcp = NULL;
obj->closing = 1;
}
static void lzn_onconnect(void *ud, zn_Tcp *tcp, unsigned err) {
lzn_Tcp *obj = (lzn_Tcp*)ud;
lua_State *L = obj->L;
(void)tcp;
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->onconnect_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->ref);
lzn_unref(L, &obj->ref);
if (err != ZN_OK) {
lua_pushstring(L, err >= ZN_ERROR_COUNT ?
zn_aierror(err - ZN_ERROR_COUNT) : zn_strerror(err));
lua_pushinteger(L, err);
}
if (lbind_pcall(L, err == ZN_OK ? 1 : 3, 0) != LUA_OK) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
static void lzn_tcperror(lua_State *L, lzn_Tcp *obj, int err) {
if (obj->onerror_ref != LUA_NOREF) {
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->onerror_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->ref);
lua_pushstring(L, zn_strerror(err));
lua_pushinteger(L, err);
if (lbind_pcall(L, 3, 0) != LUA_OK) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
if (obj->tcp) {
zn_deltcp(obj->tcp);
lzn_freetcp(L, obj);
}
}
static size_t lzn_onheader(void *ud, const char *buff, size_t len) {
lzn_Tcp *obj = (lzn_Tcp*)ud;
lua_State *L = obj->L;
size_t ret = len;
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->onheader_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->ref);
lua_pushlstring(L, buff, len);
if (lbind_pcall(L, 2, 1) != LUA_OK) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lzn_unref(L, &obj->ref);
}
else if (lua_isinteger(L, -1))
ret = (size_t)lua_tointeger(L, -1);
lua_pop(L, 1);
return ret;
}
static void lzn_onpacket(void *ud, const char *buff, size_t len) {
lzn_Tcp *obj = (lzn_Tcp*)ud;
lua_State *L = obj->L;
if (obj->onpacket_ref == LUA_NOREF) return;
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->onpacket_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->ref);
lua_pushlstring(L, buff, len);
if (lbind_pcall(L, 2, 0) != LUA_OK) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
static void lzn_onsend(void *ud, zn_Tcp *tcp, unsigned err, unsigned count) {
lzn_Tcp *obj = (lzn_Tcp*)ud;
if (err == ZN_OK) {
if (!zn_sendfinish(&obj->send, count))
goto check_close;
err = zn_send(tcp,
zn_sendbuff(&obj->send),
zn_sendsize(&obj->send), lzn_onsend, obj);
}
if (err != ZN_OK) lzn_tcperror(obj->L, obj, err);
check_close:
if (obj->closing && zn_sendsize(&obj->send) == 0) {
zn_closetcp(tcp);
lzn_freetcp(obj->L, obj);
}
}
static void lzn_onrecv(void *ud, zn_Tcp *tcp, unsigned err, unsigned count) {
lzn_Tcp *obj = (lzn_Tcp*)ud;
if (err == ZN_OK) {
zn_recvfinish(&obj->recv, count);
err = zn_recv(tcp,
zn_recvbuff(&obj->recv),
zn_recvsize(&obj->recv), lzn_onrecv, obj);
}
if (err != ZN_OK) lzn_tcperror(obj->L, obj, err);
}
static lzn_Tcp *lzn_newtcp(lua_State *L, zn_State *S, zn_Tcp *tcp) {
lzn_Tcp *obj = (lzn_Tcp*)lbind_new(L, sizeof(lzn_Tcp), &lbT_Tcp);
zn_BufferCache *bc = (zn_BufferCache*)zn_getuserdata(S);
obj->tcp = tcp;
obj->S = S;
obj->L = L;
obj->onconnect_ref = LUA_NOREF;
obj->onheader_ref = LUA_NOREF;
obj->onpacket_ref = LUA_NOREF;
obj->onerror_ref = LUA_NOREF;
obj->ref = LUA_NOREF;
obj->closing = 0;
zn_initsendbuffer(&obj->send, bc);
zn_initrecvbuffer(&obj->recv, bc);
zn_recvonheader(&obj->recv, lzn_onheader, obj);
zn_recvonpacket(&obj->recv, lzn_onpacket, obj);
return obj;
}
static void lzn_ongetaddr(void *ud, unsigned err, unsigned count, zn_PeerInfo *peers) {
lzn_Tcp *obj = (lzn_Tcp*)ud;
if (err == ZN_OK)
err = zn_connect(obj->tcp, peers[0].addr, peers[0].port, lzn_onconnect, obj);
else err += ZN_ERROR_COUNT;
if (err != ZN_OK) lzn_onconnect(ud, obj->tcp, err);
}
static int Ltcp_connect(lua_State *L) {
lzn_Tcp *obj = (lzn_Tcp*)lbind_check(L, 1, &lbT_Tcp);
const char *addr = luaL_optstring(L, 2, "127.0.0.1");
const char *port = luaL_optstring(L, 3, "http");
int ret;
luaL_checktype(L, 4, LUA_TFUNCTION);
luaL_argcheck(L, port > 0, 3, "port out of range");
lzn_ref(L, 4, &obj->onconnect_ref);
lzn_ref(L, 1, &obj->ref);
ret = zn_getaddrinfo(obj->S, addr, port, ZN_TCP, lzn_ongetaddr, obj);
return_result(L, ret);
}
static int Ltcp_new(lua_State *L) {
zn_State *S = (zn_State*)lbind_check(L, 1, &lbT_State);
int top = lua_gettop(L);
zn_Tcp *tcp;
if ((tcp = zn_newtcp(S)) == NULL)
return 0;
lzn_newtcp(L, S, tcp);
if (top != 1) {
lua_replace(L, 1);
return Ltcp_connect(L);
}
return 1;
}
static int Ltcp_delete(lua_State *L) {
lzn_Tcp *obj = (lzn_Tcp*)lbind_test(L, 1, &lbT_Tcp);
if (obj && obj->tcp != NULL) {
obj->closing = 1;
lzn_unref(L, &obj->onerror_ref);
if (zn_sendsize(&obj->send) == 0) {
zn_deltcp(obj->tcp);
lzn_freetcp(L, obj);
lbind_delete(L, 1);
}
}
return 0;
}
static int Ltcp_onerror(lua_State *L) {
lzn_Tcp *obj = (lzn_Tcp*)lbind_check(L, 1, &lbT_Tcp);
if (!obj->tcp) return 0;
luaL_checktype(L, 2, LUA_TFUNCTION);
lzn_ref(L, 2, &obj->onerror_ref);
lbind_returnself(L);
}
static int Ltcp_send(lua_State *L) {
lzn_Tcp *obj = (lzn_Tcp*)lbind_check(L, 1, &lbT_Tcp);
size_t len;
const char *data = luaL_checklstring(L, 2, &len);
lua_Integer i = lzn_posrelat(luaL_optinteger(L, 3, 1), len);
lua_Integer j = lzn_posrelat(luaL_optinteger(L, 4, len), len);
int ret = ZN_OK;
len = (size_t)(i > j ? 0 : j - i + 1);
if (!obj->tcp || obj->closing) return 0;
if (!zn_sendprepare(&obj->send, data + i - 1, len))
lbind_returnself(L);
lzn_ref(L, 1, &obj->ref);
ret = zn_send(obj->tcp,
zn_sendbuff(&obj->send),
zn_sendsize(&obj->send), lzn_onsend, obj);
return_result(L, ret);
}
static int Ltcp_receive(lua_State *L) {
lzn_Tcp *obj = (lzn_Tcp*)lbind_check(L, 1, &lbT_Tcp);
int ret;
if (!obj->tcp) return 0;
luaL_checktype(L, 2, LUA_TFUNCTION);
if (lua_isfunction(L, 3))
lzn_ref(L, 3, &obj->onpacket_ref);
lzn_ref(L, 2, &obj->onheader_ref);
lzn_ref(L, 1, &obj->ref);
ret = zn_recv(obj->tcp,
zn_recvbuff(&obj->recv),
zn_recvsize(&obj->recv), lzn_onrecv, obj);
return_result(L, ret);
}
static int Ltcp_close(lua_State *L) {
lzn_Tcp *obj = (lzn_Tcp*)lbind_check(L, 1, &lbT_Tcp);
if (obj->tcp) {
obj->closing = 1;
if (zn_sendsize(&obj->send) == 0) {
int ret = zn_closetcp(obj->tcp);
lzn_freetcp(L, obj);
return_result(L, ret);
}
}
lbind_returnself(L);
}
static int Ltcp_getpeerinfo(lua_State *L) {
lzn_Tcp *obj = (lzn_Tcp*)lbind_check(L, 1, &lbT_Tcp);
zn_PeerInfo info;
zn_getpeerinfo(obj->tcp, &info);
lua_pushstring(L, info.addr);
lua_pushinteger(L, info.port);
return 2;
}
static void open_tcp(lua_State *L) {
luaL_Reg libs[] = {
{ "__gc", Ltcp_delete },
#define ENTRY(name) { #name, Ltcp_##name }
ENTRY(new),
ENTRY(delete),
ENTRY(close),
ENTRY(connect),
ENTRY(send),
ENTRY(receive),
ENTRY(getpeerinfo),
ENTRY(onerror),
#undef ENTRY
{ NULL, NULL }
};
lbind_newmetatable(L, libs, &lbT_Tcp);
}
/* znet accept */
typedef struct lzn_Accept {
zn_Accept *accept;
zn_State *S;
lua_State *L;
int onaccept_ref;
int ref;
} lzn_Accept;
static void lzn_onaccept(void *ud, zn_Accept *accept, unsigned err, zn_Tcp *tcp) {
lzn_Accept *obj = (lzn_Accept*)ud;
lua_State *L = obj->L;
if (err != ZN_OK) return;
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->onaccept_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->ref);
lzn_newtcp(L, obj->S, tcp);
if (lbind_pcall(L, 2, 1) != LUA_OK) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lzn_unref(L, &obj->ref);
}
else if (lua_toboolean(L, -1))
zn_accept(accept, lzn_onaccept, ud);
else lzn_unref(L, &obj->ref);
lua_pop(L, 1);
}
static void lzn_freeaccept(lua_State *L, lzn_Accept *obj) {
lzn_unref(L, &obj->onaccept_ref);
lzn_unref(L, &obj->ref);
obj->accept = NULL;
}
static int Laccept_new(lua_State *L) {
zn_State *S = (zn_State*)lbind_check(L, 1, &lbT_State);
lzn_Accept *obj;
luaL_checktype(L, 2, LUA_TFUNCTION);
obj = (lzn_Accept*)lbind_new(L, sizeof(lzn_Accept), &lbT_Accept);
obj->S = S;
obj->L = L;
obj->accept = zn_newaccept(S);
obj->onaccept_ref = LUA_NOREF;
obj->ref = LUA_NOREF;
if (obj->accept != NULL) {
lzn_ref(L, 2, &obj->onaccept_ref);
return 1;
}
return 0;
}
static int Laccept_delete(lua_State *L) {
lzn_Accept *obj = (lzn_Accept*)lbind_test(L, 1, &lbT_Accept);
if (obj && obj->accept != NULL) {
zn_delaccept(obj->accept);
lzn_freeaccept(L, obj);
lbind_delete(L, 1);
}
return 0;
}
static int Laccept_listen(lua_State *L) {
lzn_Accept *obj = (lzn_Accept*)lbind_check(L, 1, &lbT_Accept);
const char *addr = luaL_optstring(L, 2, "127.0.0.1");
lua_Integer port = luaL_checkinteger(L, 3);
int ret;
if (!obj->accept) return 0;
if (port < 0) luaL_argerror(L, 2, "port out of range");
if ((ret = zn_listen(obj->accept, addr, (unsigned)port)) == ZN_OK)
ret = zn_accept(obj->accept, lzn_onaccept, obj);
if (ret == ZN_OK) lzn_ref(L, 1, &obj->ref);
return_result(L, ret);
}
static int Laccept_close(lua_State *L) {
lzn_Accept *obj = (lzn_Accept*)lbind_check(L, 1, &lbT_Accept);
int ret;
if (!obj->accept) return 0;
ret = zn_closeaccept(obj->accept);
lzn_freeaccept(L, obj);
return_result(L, ret);
}
static void open_accept(lua_State *L) {
luaL_Reg libs[] = {
{ "__gc", Laccept_delete },
#define ENTRY(name) { #name, Laccept_##name }
ENTRY(new),
ENTRY(delete),
ENTRY(listen),
ENTRY(close),
#undef ENTRY
{ NULL, NULL }
};
lbind_newmetatable(L, libs, &lbT_Accept);
}
/* znet udp */
typedef struct lzn_Udp {
zn_Udp *udp;
lua_State *L;
int onrecvfrom_ref;
int ref;
zn_Buffer recv;
} lzn_Udp;
static void lzn_onrecvfrom(void *ud, zn_Udp *udp, unsigned err, unsigned count, const char *addr, unsigned port) {
lzn_Udp *obj = (lzn_Udp*)ud;
lua_State *L = obj->L;
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->onrecvfrom_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, obj->ref);
if (err == ZN_OK) {
lua_pushlstring(L, zn_buffer(&obj->recv), count);
zn_bufflen(&obj->recv) = 0;
lua_pushstring(L, addr);
lua_pushinteger(L, port);
}
else {
lua_pushnil(L);
lua_pushstring(L, zn_strerror(err));
lua_pushinteger(L, err);
}
if (lbind_pcall(L, 4, 1) != LUA_OK) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lzn_unref(L, &obj->ref);
}
else if (lua_toboolean(L, -1)) {
int ret = zn_recvfrom(udp,
zn_prepbuffsize(&obj->recv, LZN_UDP_RECVSIZE),
LZN_UDP_RECVSIZE, lzn_onrecvfrom, obj);
if (ret != ZN_OK)
lzn_onrecvfrom(ud, udp, ret, 0, NULL, 0);
}
else lzn_unref(L, &obj->ref);
lua_pop(L, 1);
}
static int Ludp_new(lua_State *L) {
zn_State *S = (zn_State*)lbind_check(L, 1, &lbT_State);
const char *addr = luaL_optstring(L, 2, "127.0.0.1");
lua_Integer port = luaL_optinteger(L, 3, 0);
lzn_Udp *obj;
luaL_checktype(L, 2, LUA_TFUNCTION);
if (port < 0) luaL_argerror(L, 2, "port out of range");
obj = (lzn_Udp*)lbind_new(L, sizeof(lzn_Udp), &lbT_Udp);
obj->udp = zn_newudp(S, addr, (unsigned)port);
obj->L = L;
obj->onrecvfrom_ref = LUA_NOREF;
obj->ref = LUA_NOREF;
return 1;
}
static int Ludp_delete(lua_State *L) {
lzn_Udp *obj = (lzn_Udp*)lbind_test(L, 1, &lbT_Udp);
if (obj && obj->udp != NULL) {
zn_deludp(obj->udp);
lbind_delete(L, 1);
lzn_unref(L, &obj->onrecvfrom_ref);
lzn_unref(L, &obj->ref);
obj->udp = NULL;
}
return 0;
}
static int Ludp_sendto(lua_State *L) {
lzn_Udp *obj = (lzn_Udp*)lbind_check(L, 1, &lbT_Udp);
const char *addr = luaL_checkstring(L, 2);
lua_Integer port = luaL_checkinteger(L, 3);
size_t len;
const char *data = luaL_checklstring(L, 4, &len);
lua_Integer i = lzn_posrelat(luaL_optinteger(L, 5, 1), len);
lua_Integer j = lzn_posrelat(luaL_optinteger(L, 6, len), len);
len = (size_t)(i > j ? 0 : j - i + 1);
int ret = zn_sendto(obj->udp, data + i - 1, len, addr, (unsigned)port);
return_result(L, ret);
}
static int Ludp_receivefrom(lua_State *L) {
lzn_Udp *obj = (lzn_Udp*)lbind_check(L, 1, &lbT_Udp);
int ret;
luaL_checktype(L, 2, LUA_TFUNCTION);
lzn_ref(L, 2, &obj->onrecvfrom_ref);
ret = zn_recvfrom(obj->udp,
zn_prepbuffsize(&obj->recv, LZN_UDP_RECVSIZE),
LZN_UDP_RECVSIZE, lzn_onrecvfrom, obj);
if (ret == ZN_OK) lzn_ref(L, 1, &obj->ref);
return_result(L, ret);
}
static void open_udp(lua_State *L) {
luaL_Reg libs[] = {
{ "__gc", Ludp_delete },
#define ENTRY(name) { #name, Ludp_##name }
ENTRY(new),
ENTRY(delete),
ENTRY(sendto),
ENTRY(receivefrom),
#undef ENTRY
{ NULL, NULL }
};
if (lbind_newmetatable(L, libs, &lbT_Udp))
lbind_setaccessors(L, 0, LBIND_INDEX|LBIND_NEWINDEX);
}
/* znet state */
typedef struct lzn_AddrInfo {
lua_State *L;
int ref;
int ref_func;
} lzn_AddrInfo;
static void lzn_onaddrinfo(void *ud, unsigned err, unsigned count, zn_PeerInfo *peers) {
lzn_AddrInfo *info = (lzn_AddrInfo*)ud;
lua_State *L = info->L;
lua_rawgeti(L, LUA_REGISTRYINDEX, info->ref_func);
if (err != ZN_OK) {
lua_pushnil(L);
if (err == ZN_ERROR)
lua_pushliteral(L, "Operation canceled");
else
lua_pushstring(L, zn_aierror(err));
}
else {
unsigned i;
lua_createtable(L, count, 0);
for (i = 1; i <= count; ++i) {
lua_pushstring(L, peers[i-1].addr);
lua_rawseti(L, -2, i);
}
if (count != 0)
lua_pushinteger(L, peers[0].port);
}
if (lbind_pcall(L, 2, 0) != LUA_OK) {
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
lzn_unref(L, &info->ref_func);
lzn_unref(L, &info->ref);
}
static int lzn_checkgaiflags(lua_State *L, int idx) {
const char *s = luaL_optstring(L, idx, "");
int flags = 0;
for (; *s != '\0'; ++s) {
switch (*s) {
case '4': flags |= ZN_IPV4; break;
case '6': flags |= ZN_IPV6; break;
case 't': if ((flags&0xFF)==0) flags = ZN_TCP; break;
case 'u': if ((flags&0xFF)==0) flags = ZN_UDP; break;
case 'a': if ((flags&0xFF)==0) flags = ZN_ACCEPT; break;
}
}
return flags;
}
static int Lstate_getaddrinfo(lua_State *L) {
zn_State *S = (zn_State*)lbind_check(L, 1, &lbT_State);
lzn_AddrInfo *data;
const char *node = luaL_optstring(L, 2, NULL);
const char *svr = luaL_optstring(L, 3, NULL);
int flags = 0;
if (!lua_isfunction(L, 4))
flags = lzn_checkgaiflags(L, 4);
else {
lua_pushnil(L);
lua_insert(L, 4);
}
luaL_checktype(L, 5, LUA_TFUNCTION);
if (node == NULL && svr == NULL)
luaL_error(L, "node or service required");
data = lua_newuserdata(L, sizeof(lzn_AddrInfo));
data->L = L;
data->ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pushvalue(L, 5);
data->ref_func = luaL_ref(L, LUA_REGISTRYINDEX);
zn_getaddrinfo(S, node, svr, flags, lzn_onaddrinfo, data);
lbind_returnself(L);
}
static int Lstate_new(lua_State *L) {
zn_State *S = zn_newstate();
if (S == NULL)
return 0;
lbind_wrap(L, S, &lbT_State);
zn_setuserdata(S, zn_newbuffcache(NULL, NULL));
return 1;
}
static int Lstate_delete(lua_State *L) {
zn_State *S = (zn_State*)lbind_test(L, 1, &lbT_State);
if (S != NULL) {
lbind_delete(L, 1);
zn_closeaddrinfo(S);
zn_delbuffcache((zn_BufferCache*)zn_getuserdata(S));
zn_close(S);
}
return 0;
}
static int Lstate_run(lua_State *L) {
zn_State *S = (zn_State*)lbind_check(L, 1, &lbT_State);
const char *s = luaL_optstring(L, 2, "loop");
int ret;
if (*s == 'c') /* check */
ret = zn_run(S, ZN_RUN_CHECK);
else if (*s == 'o')
ret = zn_run(S, ZN_RUN_ONCE);
else
ret = zn_run(S, ZN_RUN_LOOP);
return_result(L, ret);
}
static int lzn_deinitialize(lua_State *L) {
(void)L;
zn_deinitialize();
return 0;
}
LUALIB_API int luaopen_znet(lua_State *L) {
luaL_Reg libs[] = {
{ "timer", Ltimer_new },
{ "accept", Laccept_new },
{ "tcp", Ltcp_new },
{ "udp", Ludp_new },
#define ENTRY(name) { #name, Lstate_##name }
ENTRY(new),
ENTRY(delete),
ENTRY(run),
ENTRY(getaddrinfo),
#undef ENTRY
{ NULL, NULL }
};
lbind_install(L, NULL);
open_timer(L);
open_accept(L);
open_tcp(L);
open_udp(L);
if (lbind_newmetatable(L, libs, &lbT_State)) {
lbind_setaccessors(L, 0, LBIND_INDEX|LBIND_NEWINDEX);
/* initialize and register deinitialize */
zn_initialize();
lua_newtable(L);
lua_pushcfunction(L, lzn_deinitialize);
lua_setfield(L, -2, "__gc");
lua_setmetatable(L, -2);
}
lua_pushstring(L, zn_engine());
lua_setfield(L, -2, "engine");
return 1;
}
/* win32cc: flags+='-mdll -s -O3 -DLUA_BUILD_AS_DLL'
* win32cc: libs+='-lws2_32 -llua53' output='znet.dll'
* maccc: flags+='-bundle -O3 -undefined dynamic_lookup -pthread'
* maccc: output='znet.so' */