-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmnet_core.c
1871 lines (1671 loc) · 47.9 KB
/
mnet_core.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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2015 lalawue
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MIT license. See LICENSE for details.
*/
#if defined(_WIN32) || defined(_WIN64)
#define MNET_OS_WIN 1
#elif defined(__APPLE__)
#define MNET_OS_MACOX 1
#elif defined(__FreeBSD__)
#define MNET_OS_FreeBSD 1
#else
#define MNET_OS_LINUX 1
#define _BSD_SOURCE
#define _DEFAULT_SOURCE
#endif
#if MNET_OS_WIN
#define _CRT_SECURE_NO_WARNINGS
#define _WIN32_WINNT _WIN32_WINNT_WIN8
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <stdint.h>
#endif // WIN
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#if (MNET_OS_MACOX || MNET_OS_FreeBSD)
#if MNET_OS_FreeBSD
#include <sys/cdefs.h>
#undef __XSI_VISIBLE
#define __XSI_VISIBLE 1
#define __BSD_VISIBLE 1
#endif
#include <sys/types.h>
#include <sys/event.h>
#endif /* MACOSX, FreeBSD */
#if MNET_OS_LINUX
#include <sys/types.h>
#include <sys/epoll.h>
#endif /* LINUX */
#if (MNET_OS_MACOX || MNET_OS_LINUX || MNET_OS_FreeBSD)
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
//#include <net/if_arp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <ctype.h>
#include <sys/time.h>
#else
#include "wepoll.h"
#endif /* MACOSX, FreeBSD, LINUX */
#include "mnet_core.h"
#if MNET_OS_WIN
#define close(a) closesocket(a)
#define getsockopt(a,b,c,d,e) getsockopt((a),(b),(c),(char*)(d),(e))
#define setsockopt(a,b,c,d,e) setsockopt((a),(b),(c),(char*)(d),(e))
#define recv(a,b,c,d) recv((SOCKET)a,(char*)b,c,d)
#define send(a,b,c,d) send((SOCKET)a,(char*)b,c,d)
#define recvfrom(a,b,c,d,e,f) recvfrom((SOCKET)a,(char*)b,c,d,e,f)
#define sendto(a,b,c,d,e,f) sendto((SOCKET)a,(char*)b,c,d,e,f)
#undef errno
#define errno WSAGetLastError()
#undef EWOULDBLOCK
#define EWOULDBLOCK WSAEWOULDBLOCK
int _evt_del(chann_t *n, int set);
#endif /* WIN */
#define MNET_SKIPLIST_MAX_LEVEL 32 /* should be enough for 2^32 elements */
#define MNET_EXT_MAX_SIZE 8 /* including reserved chann_type */
enum {
MNET_LOG_ERR = 1,
MNET_LOG_INFO,
MNET_LOG_VERBOSE,
};
enum {
MNET_SET_READ = 0,
MNET_SET_WRITE,
MNET_SET_DEL,
};
struct sk_link {
struct sk_link *prev, *next;
};
typedef struct {
int level;
int count;
struct sk_link head[MNET_SKIPLIST_MAX_LEVEL];
} skiplist_t;
typedef struct {
int64_t key;
void *value;
uint8_t level; /* for O(1) deletion */
struct sk_link link[0];
} skipnode_t;
typedef struct s_rwbuf {
int ptr;
int len;
struct s_rwbuf *next;
uint8_t *buf;
} rwb_t;
typedef struct {
rwb_t *head;
rwb_t *tail;
int count;
} rwb_head_t;
typedef struct {
int interval; /* micro seconds */
int64_t time; /* micro seconds */
void *chann; /* chann pointer */
} chann_tm_t;
struct s_chann {
int fd; /* socket fd */
chann_state_t state; /* chann state */
chann_type_t ctype; /* 'tcp', 'udp', 'broadcast' */
void *opaque; /* user defined data */
void *ext_ud; /* ext ud */
struct sockaddr_in addr; /* socket address */
socklen_t addr_len; /* socket address length */
skipnode_t *timer_node; /* timer node */
rwb_head_t rwb_send; /* fifo cached for unsend data */
chann_t *prev; /* double linked prev chann */
chann_t *next; /* double linked next chann */
chann_t *del_next; /* for deleting channs */
chann_t *dis_next; /* for disconnected channs */
int64_t bytes_send; /* bytes sended */
int64_t bytes_recv; /* bytes received */
int buf_size; /* system socket buffer size */
uint8_t active_send_event; /* notify user send data buffer empty */
uint32_t epoll_events; /* event trigger flags */
chann_msg_t msg; /* chann message body */
};
#if (MNET_OS_MACOX || MNET_OS_FreeBSD)
typedef struct kevent mevent_t;
#else
typedef struct epoll_event mevent_t;
#endif
#if MNET_OS_WIN
typedef HANDLE kq_t;
#else
typedef int kq_t;
#endif
typedef int (*sys_accept_fn)(int, struct sockaddr *, socklen_t *);
struct s_event {
int size;
int count;
mevent_t *array;
};
typedef struct {
int init;
int chann_count;
chann_t *channs; /* channs list */
chann_t *del_channs; /* for deleting channs */
chann_t *dis_channs; /* for disconnected events */
kq_t kq; /* kqueue or epoll fd */
struct s_event chg;
struct s_event evt;
mnet_ext_t ext_config[MNET_EXT_MAX_SIZE]; /* key is (CHANN_TYPE_BROADCAST, 7] */
int fd_count; /* fd count */
int fd_index; /* fd index*/
skiplist_t *tm_clock;
struct sk_link *tm_pos;
int64_t tm_current;
void *ac_context;
sys_accept_fn ac_fn;
mnet_balancer_cb ac_before;
mnet_balancer_cb ac_after;
} mnet_t;
static mnet_t g_mnet;
static inline mnet_t*
_gmnet() {
return &g_mnet;
}
/* declares
*/
static inline void mnet_log_default(chann_t *n, int level, const char *log_string) {
static const char slev[8] = { 'D', 'E', 'I', 'V'};
printf("[%c]%p: %s", slev[level], n, log_string);
}
/* basic
*/
static inline void* _mmalloc(int n) { return malloc(n); }
static inline void* _mrealloc(void*p, int n) { return realloc(p, n); }
static inline void _mfree(void *p) { free(p); }
static void* (*mnet_malloc)(int) = _mmalloc;
static void* (*mnet_realloc)(void*, int) = _mrealloc;
static void (*mnet_free)(void*) = _mfree;
static void (*mnet_log)(chann_t*, int, const char*) = mnet_log_default;
static inline void* mm_malloc(int n) {
void *p = mnet_malloc(n); memset(p, 0, n); return p;
}
static inline void* mm_realloc(void *p, size_t n) {
return mnet_realloc(p, n);
}
static inline void mm_free(void *p) {
mnet_free(p);
}
static int _log_level = MNET_LOG_INFO;
static inline void mm_log(chann_t *n, int level, const char *fmt, ...) {
if (level <= _log_level) {
char buf[192];
va_list argptr;
va_start(argptr, fmt);
vsprintf(buf, fmt, argptr);
va_end(argptr);
mnet_log(n, level, buf);
}
}
//#define mm_log(...) /* use to disable any log handling */
static inline int
_min_of(int a, int b) {
return a < b ? a : b;
}
static int _chann_msg(chann_t *n, chann_event_t event, chann_t *r, int err);
/* buf op
*/
static inline int
_rwb_count(rwb_head_t *h) { return h->count; }
static inline int
_rwb_buffered(rwb_t *b) {
return b ? (b->len - b->ptr) : 0;
}
static inline rwb_t*
_rwb_new(int len) {
rwb_t *b = (rwb_t*)mm_malloc(sizeof(rwb_t) + len);
b->buf = (uint8_t *)b + sizeof(rwb_t);
b->len = len;
return b;
}
static rwb_t*
_rwb_create_tail(rwb_head_t *h, int len) {
if (h->count <= 0) {
h->head = h->tail = _rwb_new(len);
h->count++;
} else {
h->tail->next = _rwb_new(len);
h->tail = h->tail->next;
h->count++;
}
return h->tail;
}
static void
_rwb_destroy_head(rwb_head_t *h) {
if (_rwb_buffered(h->head) <= 0) {
rwb_t *b = h->head;
h->head = b->next;
mm_free(b);
h->count -= 1;
if (h->count <= 0) {
h->head = h->tail = 0;
}
}
}
static void
_rwb_cache(rwb_head_t *h, void *buf, int buf_len) {
rwb_t *b = _rwb_create_tail(h, buf_len);
memcpy(b->buf, buf, buf_len);
}
static uint8_t*
_rwb_drain_param(rwb_head_t *h, int *len) {
rwb_t *b = h->head;
*len = _rwb_buffered(b);
return &b->buf[b->ptr];
}
static void
_rwb_drain(rwb_head_t *h, int drain_len) {
while ((h->count>0) && (drain_len>0)) {
rwb_t *b = h->head;
int len = _min_of(drain_len, _rwb_buffered(b));
drain_len -= len;
b->ptr += len;
_rwb_destroy_head(h);
}
}
static void
_rwb_destroy(rwb_head_t *h) {
while (h->count > 0) {
_rwb_destroy_head(h);
}
}
/* skip list
*/
static inline void
list_init(struct sk_link *link) {
link->prev = link->next = link;
}
static inline void
__list_add(struct sk_link *link, struct sk_link *prev, struct sk_link *next) {
link->next = next;
link->prev = prev;
next->prev = link;
prev->next = link;
}
static inline void
__list_del(struct sk_link *prev, struct sk_link *next) {
prev->next = next;
next->prev = prev;
}
static inline void
list_del(struct sk_link *link) {
__list_del(link->prev, link->next);
list_init(link);
}
static inline int
list_empty(struct sk_link *link) {
return link->next == link;
}
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr) - (size_t)(&((type *)0)->member)))
#define skiplist_foreach(pos, end) \
for (; pos != end; pos = pos->next)
#define skiplist_foreach_safe(pos, n, end) \
for (n = pos->next; pos != end; pos = n, n = pos->next)
static skipnode_t*
skipnode_create(int level, int64_t key, void *value) {
skipnode_t *node = (skipnode_t *)mm_malloc(sizeof(*node) + level * sizeof(struct sk_link));
if (node) {
node->key = key;
node->value = value;
node->level = level;
}
return node;
}
static inline void
skipnode_destroy(skipnode_t *node) {
mm_free(node);
}
static skiplist_t*
skiplist_create(void) {
skiplist_t *list = (skiplist_t *)mm_malloc(sizeof(*list));
if (list) {
list->level = 1;
list->count = 0;
size_t i = 0;
for (i = 0; i < sizeof(list->head) / sizeof(list->head[0]); i++) {
list_init(&list->head[i]);
}
}
return list;
}
static void
skiplist_destroy(skiplist_t *list) {
struct sk_link *n;
struct sk_link *pos = list->head[0].next;
skiplist_foreach_safe(pos, n, &list->head[0]) {
skipnode_t *node = list_entry(pos, skipnode_t, link[0]);
skipnode_destroy(node);
}
mm_free(list);
}
static int
skiplist_random_level(void) {
int level = 1;
const double p = 0.25;
while ((rand() & 0xffff) < 0xffff * p) {
level++;
}
return level > MNET_SKIPLIST_MAX_LEVEL ? MNET_SKIPLIST_MAX_LEVEL : level;
}
static skipnode_t *
skiplist_insert(skiplist_t *list, int64_t key, void *value) {
int level = skiplist_random_level();
if (level > list->level) {
list->level = level;
}
skipnode_t *node = skipnode_create(level, key, value);
if (node != NULL) {
int i = list->level - 1;
struct sk_link *pos = &list->head[i];
struct sk_link *end = &list->head[i];
for (; i >= 0; i--) {
pos = pos->next;
skiplist_foreach(pos, end) {
skipnode_t *nd = list_entry(pos, skipnode_t, link[i]);
if (nd->key >= key) {
end = &nd->link[i];
break;
}
}
pos = end->prev;
if (i < level) {
__list_add(&node->link[i], pos, end);
}
pos--;
end--;
}
list->count++;
}
return node;
}
static void
skiplist_remove_skipnode(skiplist_t *list, skipnode_t *node) {
int i, level = node->level;
for (i = 0; i < level; i++) {
list_del(&node->link[i]);
if (list_empty(&list->head[i])) {
list->level--;
}
}
skipnode_destroy(node);
list->count--;
}
/* timer op
*/
#define _tm_count(clock) (clock->count)
static int64_t
_tm_current() {
#ifdef MNET_OS_WIN
FILETIME ft;
int64_t t;
GetSystemTimeAsFileTime(&ft);
t = (int64_t)ft.dwHighDateTime << 32 | ft.dwLowDateTime;
return t / 10 - 11644473600000000; /* Jan 1, 1601 */
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
#endif
}
static chann_tm_t*
_tm_active(skiplist_t *clock, chann_t *n, int64_t interval) {
chann_tm_t *tm = (chann_tm_t *)mm_malloc(sizeof(chann_tm_t));
tm->interval = interval;
tm->time = _tm_current() + interval;
tm->chann = n;
n->timer_node = skiplist_insert(clock, tm->time, tm);
mm_log(n, MNET_LOG_VERBOSE, "chann create timer , %p (%p) -> %zd microsecond (%d)\n", n, tm, interval, _tm_count(clock));
return tm;
}
static inline void
_tm_update(skiplist_t *clock, chann_t *n, int64_t interval) {
skipnode_t *snode = (skipnode_t *)n->timer_node;
chann_tm_t *tm = (chann_tm_t *)snode->value;
tm->interval = interval;
tm->time = _tm_current() + tm->interval;
skiplist_remove_skipnode(clock, snode);
n->timer_node = skiplist_insert(clock, tm->time, tm);
mm_log(n, MNET_LOG_VERBOSE, "chann update timer, %p (%p) -> %zd microsecond (%d)\n", n, tm, interval, _tm_count(clock));
}
static void
_tm_kill(skiplist_t *clock, chann_t *n) {
if (n->timer_node) {
skipnode_t *snode = (skipnode_t *)n->timer_node;
if (snode->value) {
mm_free(snode->value);
}
skiplist_remove_skipnode(clock, snode);
n->timer_node = NULL;
}
}
static void
_tm_schedule(mnet_t *ss) {
skiplist_t *clock = ss->tm_clock;
if (clock->count <= 0) {
ss->tm_pos = NULL;
return;
}
int64_t current = _tm_current();
struct sk_link *pos = clock->head[0].next;
skipnode_t *snode = list_entry(pos, skipnode_t, link[0]);
chann_tm_t *tm = (chann_tm_t *)snode->value;
if (tm->time > current) {
ss->tm_pos = NULL;
} else {
ss->tm_current = current;
ss->tm_pos = pos;
}
}
static chann_msg_t*
_tm_next(mnet_t *ss) {
skiplist_t *clock = ss->tm_clock;
for (;;) {
struct sk_link *pos = ss->tm_pos;
if (pos == NULL || pos == &clock->head[0]) {
return NULL;
}
ss->tm_pos = pos->next;
skipnode_t *snode = list_entry(pos, skipnode_t, link[0]);
chann_tm_t *tm = (chann_tm_t *)snode->value;
if (tm->time <= ss->tm_current) {
chann_t *n = (chann_t *)tm->chann;
mm_log(n, MNET_LOG_VERBOSE, "chann hit timer, %p (%p) -> %zd microsecond (%d)\n", n, tm, tm->interval, _tm_count(clock));
_tm_update(clock, n, tm->interval);
if (_chann_msg(n, CHANN_EVENT_TIMER, NULL, 0)) {
return &n->msg;
} else {
continue;
}
} else {
ss->tm_pos = NULL;
return NULL;
}
}
return NULL;
}
/* socket param op
*/
static int
_set_nonblocking(int fd) {
#if MNET_OS_WIN
u_long imode = 1;
int ret = ioctlsocket(fd, FIONBIO, (u_long*)&imode);
if (ret == NO_ERROR) return 0;
#else
int flag = fcntl(fd, F_GETFL, 0);
if (flag != -1) return fcntl(fd, F_SETFL, flag | O_NONBLOCK);
#endif
return -1;
}
static int
_set_broadcast(int fd) {
int opt = 1;
return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char*)&opt, sizeof(opt));
}
static int
_set_keepalive(int fd) {
int opt = 1;
return setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&opt, sizeof(opt));
}
static int
_set_reuseaddr(int fd) {
int opt = 1;
return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));
}
static int
_set_bufsize(int fd, int buf_size) {
return (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&buf_size, sizeof(buf_size)) |
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&buf_size, sizeof(buf_size)));
}
static int
_bind(int fd, struct sockaddr_in *si) {
return bind(fd, (struct sockaddr*)si, sizeof(*si));
}
static int
_listen(int fd, int backlog) {
return listen(fd, backlog > 0 ? backlog : 3);
}
/* mnet extension internal
*/
mnet_ext_t*
_ext_config(chann_type_t ctype) {
return &_gmnet()->ext_config[ctype];
}
static int
_ext_type_fn(void *ext_ctx, chann_type_t ctype) {
return ctype;
}
static int
_ext_filter_fn(void *ext_ctx, chann_msg_t *msg) {
return 1;
}
static void
_ext_op_cb(void *ext_ctx, chann_t *n) {
}
static int
_ext_state_fn(void *ext_ctx, chann_t *n, int state) {
return state;
}
static int
_ext_stream_recv(void *ext_ctx, chann_t *n, void *buf, int len) {
int ret = (int)recv(n->fd, buf, len, 0);
if (ret<0 && errno==EWOULDBLOCK) {
ret = 0;
}
return ret;
}
static int
_ext_dgram_recv(void *ext_ctx, chann_t *n, void *buf, int len) {
int ret = (int)recvfrom(n->fd, buf, len, 0, (struct sockaddr *)&n->addr, &n->addr_len);
if (ret<0 && errno==EWOULDBLOCK) {
ret = 0;
}
return ret;
}
static int
_ext_stream_send(void *ext_ctx, chann_t *n, void *buf, int len) {
int ret = (int)send(n->fd, buf, len, 0);
if (ret<0 && errno==EWOULDBLOCK) {
ret = 0;
}
return ret;
}
static int
_ext_dgram_send(void *ext_ctx, chann_t *n, void *buf, int len) {
int ret = (int)sendto(n->fd, buf, len, 0, (struct sockaddr *)&n->addr, n->addr_len);
if (ret<0 && errno==EWOULDBLOCK) {
ret = 0;
}
return ret;
}
mnet_ext_t _ext_internal_config = {
.reserved = 0,
.ext_ctx = NULL,
.type_fn = _ext_type_fn,
.filter_fn = _ext_filter_fn,
.open_cb = _ext_op_cb,
.close_cb = _ext_op_cb,
.listen_cb = _ext_op_cb,
.accept_cb = _ext_op_cb,
.connect_cb = _ext_op_cb,
.disconnect_cb = _ext_op_cb,
.state_fn = _ext_state_fn,
.recv_fn = NULL,
.send_fn = NULL,
};
/* channel op
*/
static chann_t*
_chann_create(mnet_t *ss, chann_type_t ctype, chann_state_t state) {
chann_t *n = (chann_t*)mm_malloc(sizeof(*n));
n->ctype = ctype;
n->state = state;
n->next = ss->channs;
if (ss->channs) {
ss->channs->prev = n;
}
ss->channs = n;
ss->chann_count++;
mm_log(n, MNET_LOG_VERBOSE, "chann create, ctype:%d, count %d\n", ctype, ss->chann_count);
return n;
}
static void
_chann_destroy(mnet_t *ss, chann_t *n) {
if (n->state == CHANN_STATE_CLOSED) {
if (n->next) { n->next->prev = n->prev; }
if (n->prev) { n->prev->next = n->next; }
else { ss->channs = n->next; }
_rwb_destroy(&n->rwb_send);
_tm_kill(ss->tm_clock, n);
ss->chann_count--;
mm_log(n, MNET_LOG_VERBOSE, "chann destroy %p (%d)\n", n, ss->chann_count);
mm_free(n);
}
}
static inline char*
_chann_addr(struct sockaddr_in *addr) {
return inet_ntoa(addr->sin_addr);
}
static inline int
_chann_port(struct sockaddr_in *addr) {
return ntohs(addr->sin_port);
}
int
_chann_multiprocess_accept(int afd, struct sockaddr *addr, socklen_t *addr_len) {
mnet_t *ss = _gmnet();
int fd = 0;
if (ss->ac_before(ss->ac_context, afd) > 0) {
fd = accept(afd, addr, addr_len);
ss->ac_after(ss->ac_context, afd);
}
return fd;
}
static chann_t*
_chann_accept(mnet_t *ss, chann_t *n) {
struct sockaddr_in addr;
socklen_t addr_len = sizeof(addr);
int fd = ss->ac_fn(n->fd, (struct sockaddr*)&addr, &addr_len);
if (fd > 0 && _set_nonblocking(fd) >= 0) {
chann_t *c = _chann_create(ss, n->ctype, CHANN_STATE_CONNECTED);
c->fd = fd;
c->addr = addr;
c->addr_len = addr_len;
mm_log(n, MNET_LOG_VERBOSE, "chann accept %p fd %d, from %s, count %d\n",
c, c->fd, _chann_addr(&c->addr), ss->chann_count);
mnet_ext_t *ext = _ext_config(n->ctype);
ext->accept_cb(ext->ext_ctx, c);
return c;
}
return NULL;
}
static int
_chann_send(chann_t *n, void *buf, int len) {
mnet_ext_t *ext = _ext_config(n->ctype);
int ret = ext->send_fn(ext->ext_ctx, n, buf, len);
if (ret > 0) {
n->bytes_send += ret;
}
return ret;
}
static int
_chann_disconnect_socket(mnet_t *ss, chann_t *n) {
if (n->fd > 0 && n->state > CHANN_STATE_DISCONNECT) {
mm_log(n, MNET_LOG_VERBOSE, "chann disconnect fd:%d, %p\n", n->fd, n);
#if MNET_OS_WIN
_evt_del(n, MNET_SET_DEL);
#endif
mnet_ext_t *ext = _ext_config(n->ctype);
ext->disconnect_cb(ext->ext_ctx, n);
close(n->fd);
_rwb_destroy(&n->rwb_send);
n->fd = -1;
n->state = CHANN_STATE_DISCONNECT;
n->epoll_events = 0;
return 1;
}
return 0;
}
static void
_chann_close_socket(mnet_t *ss, chann_t *n) {
if (n->state > CHANN_STATE_CLOSED) {
mnet_ext_t *ext = _ext_config(n->ctype);
ext->close_cb(ext->ext_ctx, n);
n->del_next = ss->del_channs;
ss->del_channs = n;
n->state = CHANN_STATE_CLOSED;
n->opaque = NULL;
mm_log(n, MNET_LOG_VERBOSE, "chann close %p\n", n);
}
}
int
_chann_msg(chann_t *n, chann_event_t event, chann_t *r, int err) {
n->msg.event = event;
n->msg.err = err;
n->msg.n = n;
n->msg.r = r;
n->msg.opaque = n->opaque;
mnet_ext_t *ext = _ext_config(n->ctype);
return ext->filter_fn(ext->ext_ctx, &n->msg);
}
static int
_chann_get_err(chann_t *n) {
int err = 0;
socklen_t len = sizeof(err);
if (getsockopt(n->fd, SOL_SOCKET, SO_ERROR, (void *)&err, &len) == 0) {
return err;
}
return -9999;
}
/* return 1 when sended all cached data */
static int
_chann_sended_rwb(chann_t *n) {
rwb_head_t *prh = &n->rwb_send;
if (_rwb_count(prh) <= 0) {
return 1;
}
int ret=0, len=0;
do {
uint8_t *buf = _rwb_drain_param(prh, &len);
ret = _chann_send(n, buf, len);
if (ret > 0) {
_rwb_drain(prh, ret);
} else if (ret < 0) {
mm_log(n, MNET_LOG_ERR, "chann %p fd:%d, rwb send errno %d:%s\n",
n, n->fd, errno, strerror(errno));
_chann_disconnect_socket(_gmnet(), n);
if (_chann_msg(n, CHANN_EVENT_DISCONNECT, NULL, errno)) {
mnet_t *ss = _gmnet();
n->dis_next = ss->dis_channs;
ss->dis_channs = n;
}
}
} while (ret>0 && ret>=len && _rwb_count(prh)>0);
return _rwb_count(prh) <= 0;
}
static void
_chann_fill_addr(chann_t *n, const char *host, int port) {
memset(&n->addr, 0, sizeof(n->addr));
n->addr.sin_family = AF_INET;
n->addr.sin_port = htons(port);
n->addr.sin_addr.s_addr = host==NULL ? htonl(INADDR_ANY) : inet_addr(host);
n->addr_len = sizeof(n->addr);
}
static int
_chann_open_socket(chann_t *n, const char *host, int port, int backlog) {
if (n->state == CHANN_STATE_DISCONNECT) {
mnet_ext_t *ext = _ext_config(n->ctype);
int istcp = ext->type_fn(ext->ext_ctx, n->ctype) == CHANN_TYPE_STREAM;
int isbc = ext->type_fn(ext->ext_ctx, n->ctype) == CHANN_TYPE_BROADCAST;
int fd = socket(AF_INET, istcp ? SOCK_STREAM : SOCK_DGRAM, 0);
if (fd > 0) {
int buf_size = n->buf_size>0 ? n->buf_size : MNET_BUF_SIZE;
_chann_fill_addr(n, host, port);
if (_set_reuseaddr(fd) < 0) goto FAILED_OUT;
if (backlog && _bind(fd, &n->addr) < 0) goto FAILED_OUT;
if (backlog && istcp && _listen(fd,backlog) < 0) goto FAILED_OUT;
if (_set_nonblocking(fd) < 0) goto FAILED_OUT;
if (istcp && _set_keepalive(fd)<0) goto FAILED_OUT;
if (isbc && _set_broadcast(fd)<0) goto FAILED_OUT;
if (_set_bufsize(fd, buf_size) < 0) goto FAILED_OUT;
mm_log(n, MNET_LOG_VERBOSE, "open socket chann:%p fd:%d\n", n, fd);
return fd;
FAILED_OUT:
close(fd);
perror("chann open socket: ");
}
}
return -1;
}
/* kqueue/epoll op
*/
#if (MNET_OS_MACOX || MNET_OS_FreeBSD)
#define _KEV_CHG_ARRAY_SIZE 8
#define _KEV_EVT_ARRAY_SIZE 256
#define _KEV_FLAG_ERROR EV_ERROR
#define _KEV_FLAG_HUP EV_EOF
#define _KEV_EVENT_READ EVFILT_READ
#define _KEV_EVENT_WRITE EVFILT_WRITE
#else /* Linux, WIN */
#define _KEV_CHG_ARRAY_SIZE 4
#define _KEV_EVT_ARRAY_SIZE 256
#define _KEV_FLAG_ERROR EPOLLERR
#define _KEV_FLAG_HUP (EPOLLRDHUP | EPOLLHUP)
#define _KEV_EVENT_READ EPOLLIN
#define _KEV_EVENT_WRITE EPOLLOUT
#endif
/* kev */
static inline void*
_kev_opaque(mevent_t *kev) {
#if (MNET_OS_MACOX || MNET_OS_FreeBSD)
return kev->udata;
#else
return kev->data.ptr;
#endif
}
static inline int
_kev_flags(mevent_t *kev, int flags) {
#if (MNET_OS_MACOX || MNET_OS_FreeBSD)
return (kev->flags & flags);
#else
return (kev->events & flags);
#endif
}
static inline int
_kev_events(mevent_t *kev, int events) {
#if (MNET_OS_MACOX || MNET_OS_FreeBSD)
return (kev->filter == events);
#else
return (kev->events & events);
#endif
}
static inline int
_kev_get_flags(mevent_t *kev) {
if (kev) {
#if (MNET_OS_MACOX || MNET_OS_FreeBSD)
return kev->flags;
#else
return kev->events;
#endif
}
return -1;
}
static inline int
_kev_get_events(mevent_t *kev) {
if (kev) {
#if (MNET_OS_MACOX || MNET_OS_FreeBSD)
return kev->filter;
#else
return kev->events;
#endif
}
return -1;
}
static inline int
_kev_errno(mevent_t *kev) {
#if (MNET_OS_MACOX || MNET_OS_FreeBSD)
return kev->data;
#else
return kev->events;
#endif
}