-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathev.c
20214 lines (17190 loc) · 524 KB
/
ev.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
/**
* MIT License
*
* Copyright (c) 2021 qgymib
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#define EV_AMALGAMATE_BUILD
#define EV_BUILDING_DLL
#define _GNU_SOURCE
#include "ev.h" /* @AMALGAMATE: SKIP */
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/defs.h
// SIZE: 9518
// SHA-256: 5fcf4b2560acaebc5baa12693e2f96c50efb663304241d9b263658ce8938e70c
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/defs.h"
#ifndef __EV_DEFINES_INTERNAL_H__
#define __EV_DEFINES_INTERNAL_H__
#if defined(_WIN32)
# include <WS2tcpip.h>
#else
# include <arpa/inet.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN
# define EV_IPC_FRAME_HDR_MAGIC (0x48465645)
#elif __BYTE_ORDER == __BIG_ENDIAN
# define EV_IPC_FRAME_HDR_MAGIC (0x45564648)
#else
# error unknown endian
#endif
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define EV_MIN(a, b) ((a) < (b) ? (a) : (b))
#define EV_JOIN(a, b) EV_JOIN_2(a, b)
#define EV_JOIN_2(a, b) a##b
#define ACCESS_ONCE(TYPE, var) (*(volatile TYPE*) &(var))
/**
* @brief exchange value of \p v1 and \p v2.
* @note \p v1 and \p v2 must have the same type.
* @param[in] TYPE Type of \p v1 and \p v2.
* @param[in,out] v1 value1
* @param[in,out] v2 value2
*/
#define EXCHANGE_VALUE(TYPE, v1, v2) \
do {\
TYPE _tmp = v1;\
v1 = v2;\
v2 = _tmp;\
} while(0)
/**
* @def EV_COUNT_ARG
* @brief Count the number of arguments in macro
*/
#ifdef _MSC_VER // Microsoft compilers
# define EV_COUNT_ARG(...) _EV_INTERNAL_EXPAND_ARGS_PRIVATE(_EV_INTERNAL_ARGS_AUGMENTER(__VA_ARGS__))
/**@cond DOXYGEN_INTERNAL*/
# define _EV_INTERNAL_ARGS_AUGMENTER(...) unused, __VA_ARGS__
# define _EV_INTERNAL_EXPAND_ARGS_PRIVATE(...) EV_EXPAND(_EV_INTERNAL_GET_ARG_COUNT_PRIVATE(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
# define _EV_INTERNAL_GET_ARG_COUNT_PRIVATE(_1_, _2_, _3_, _4_, _5_, _6_, _7_, _8_, _9_, _10_, _11_, _12_, _13_, _14_, _15_, _16_, count, ...) count
/**@endcond*/
#else // Non-Microsoft compilers
# define EV_COUNT_ARG(...) _EV_INTERNAL_GET_ARG_COUNT_PRIVATE(0, ## __VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
/**@cond DOXYGEN_INTERNAL*/
# define _EV_INTERNAL_GET_ARG_COUNT_PRIVATE(_0, _1_, _2_, _3_, _4_, _5_, _6_, _7_, _8_, _9_, _10_, _11_, _12_, _13_, _14_, _15_, _16_, count, ...) count
/**@endcond*/
#endif
/**
* @def EV_BARG
* @brief Check if any parameter exists.
*/
#if defined(_MSC_VER)
# define EV_BARG(...) \
TEST_INTERNAL_BARG3(TEST_INTERNAL_BARG4(__VA_ARGS__))
# define TEST_INTERNAL_BARG4(...) \
unused, __VA_ARGS__
# define TEST_INTERNAL_BARG3(...) \
EV_EXPAND(TEST_INTERNAL_BARG(__VA_ARGS__, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 0) \
)
# define TEST_INTERNAL_BARG(\
_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, \
count, ...) count
#else
# define EV_BARG(...) \
TEST_INTERNAL_BARG(0, ## __VA_ARGS__, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 0)
# define TEST_INTERNAL_BARG(_0, \
_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, \
count, ...) count
#endif
#define ENSURE_LAYOUT(TYPE_A, FIELD_A_1, FIELD_A_2, TYPE_B, FIELD_B_1, FIELD_B_2) \
assert(sizeof(TYPE_A) == sizeof(TYPE_B));\
assert(offsetof(TYPE_A, FIELD_A_1) == offsetof(TYPE_B, FIELD_B_1));\
assert(sizeof(((TYPE_A*)0)->FIELD_A_1) == sizeof(((TYPE_B*)0)->FIELD_B_1));\
assert(offsetof(TYPE_A, FIELD_A_2) == offsetof(TYPE_B, FIELD_B_2));\
assert(sizeof(((TYPE_A*)0)->FIELD_A_2) == sizeof(((TYPE_B*)0)->FIELD_B_2))
#define EV_ABORT(fmt, ...) \
do {\
fprintf(stderr, "%s:%d:" fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__);\
abort();\
} while (0)
#if defined(__GNUC__) || defined(__clang__)
# define EV_NORETURN __attribute__ ((__noreturn__))
#elif defined(_MSC_VER)
# define EV_NORETURN __declspec(noreturn)
#else
# define EV_NORETURN
#endif
/**
* @brief The maximum length for a path.
* In Windows API, the Ansi version of `MAX_PATH` is defined as 260.
* The unicode version does not define macro as `MAX_PATH`, but NTFS does
* support up to 32768 characters in length, but only when using the Unicode
* APIs.
*
* @see https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
* @see https://docs.microsoft.com/en-us/cpp/c-runtime-library/path-field-limits?view=msvc-170
*/
#if defined(_WIN32)
# define WIN32_UNICODE_PATH_MAX 32768
#endif
#define EV_ERRNO_POSIX_MAP(xx) \
xx(EV_EPERM, EPERM, "Operation not permitted") \
xx(EV_ENOENT, ENOENT, "No such file or directory") \
xx(EV_EIO, EIO, "Host is unreachable") \
xx(EV_E2BIG, E2BIG, "Argument list too long") \
xx(EV_EBADF, EBADF, "Bad file descriptor") \
xx(EV_EAGAIN, EAGAIN, "Resource temporarily unavailable") \
xx(EV_ENOMEM, ENOMEM, "Not enough space/cannot allocate memory") \
xx(EV_EACCES, EACCES, "Permission denied") \
xx(EV_EFAULT, EFAULT, "Bad address") \
xx(EV_EBUSY, EBUSY, "Device or resource busy") \
xx(EV_EEXIST, EEXIST, "File exists") \
xx(EV_EXDEV, EXDEV, "Improper link") \
xx(EV_ENOTDIR, ENOTDIR, "Not a directory") \
xx(EV_EISDIR, EISDIR, "Is a directory") \
xx(EV_EINVAL, EINVAL, "Invalid argument") \
xx(EV_ENFILE, ENFILE, "Too many open files in system") \
xx(EV_EMFILE, EMFILE, "Too many open files") \
xx(EV_ENOSPC, ENOSPC, "No space left on device") \
xx(EV_EROFS, EROFS, "Read-only filesystem") \
xx(EV_EPIPE, EPIPE, "Broken pipe") \
xx(EV_ENAMETOOLONG, ENAMETOOLONG, "Filename too long") \
xx(EV_ENOSYS, ENOSYS, "Function not implemented") \
xx(EV_ENOTEMPTY, ENOTEMPTY, "Directory not empty") \
xx(EV_EADDRINUSE, EADDRINUSE, "Address already in use") \
xx(EV_EADDRNOTAVAIL, EADDRNOTAVAIL, "Address not available") \
xx(EV_EAFNOSUPPORT, EAFNOSUPPORT, "Address family not supported") \
xx(EV_EALREADY, EALREADY, "Connection already in progress") \
xx(EV_ECANCELED, ECANCELED, "Operation canceled") \
xx(EV_ECONNABORTED, ECONNABORTED, "Connection aborted") \
xx(EV_ECONNREFUSED, ECONNREFUSED, "Connection refused") \
xx(EV_ECONNRESET, ECONNRESET, "Connection reset") \
xx(EV_EHOSTUNREACH, EHOSTUNREACH, "Host is unreachable") \
xx(EV_EINPROGRESS, EINPROGRESS, "Operation in progress") \
xx(EV_EISCONN, EISCONN, "Socket is connected") \
xx(EV_ELOOP, ELOOP, "Too many levels of symbolic links") \
xx(EV_EMSGSIZE, EMSGSIZE, "Message too long") \
xx(EV_ENETUNREACH, ENETUNREACH, "Network unreachable") \
xx(EV_ENOBUFS, ENOBUFS, "No buffer space available") \
xx(EV_ENOTCONN, ENOTCONN, "The socket is not connected") \
xx(EV_ENOTSOCK, ENOTSOCK, "Not a socket") \
xx(EV_ENOTSUP, ENOTSUP, "Operation not supported") \
xx(EV_EPROTO, EPROTO, "Protocol error") \
xx(EV_EPROTONOSUPPORT, EPROTONOSUPPORT, "Protocol not supported") \
xx(EV_ETIMEDOUT, ETIMEDOUT, "Operation timed out")
#ifdef __cplusplus
}
#endif
#endif
// #line 7 "ev.c"
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/assert_internal.h
// SIZE: 1706
// SHA-256: 427a0c0d9101908de67717a2facc7388994953797b06d53b1683dda2f3284567
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/assert_internal.h"
#ifndef __EV_ASSERT_INTERNAL_H__
#define __EV_ASSERT_INTERNAL_H__
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Assertion macro.
*
* If \p exp is false, this macro prints an error message to standard error and
* terminates the program by calling abort().
*
* User can optionally add custom message to this macro just like printf(). In
* this case, use this macro as `EV_ASSERT(exp, fmt, ...)`.
*/
#define EV_ASSERT(exp, ...) EV_JOIN(_EV_ASSERT_, EV_BARG(__VA_ARGS__))(exp, ##__VA_ARGS__)
/**
* @brief Helper assertion macro.
*
* #EV_ASSERT() will expand to this if no print format exist.
*/
#define _EV_ASSERT_0(exp, ...) \
do {\
int _assert_ret = (exp);\
if (_assert_ret) {\
break;\
}\
ev__assertion_failure(#exp, __FILE__, __LINE__, NULL);\
} while (0)
/**
* @brief Helper assertion macro.
*
* #EV_ASSERT() will expand to this if print format exist.
*/
#define _EV_ASSERT_1(exp, fmt, ...) \
do {\
int _assert_ret = (exp);\
if (_assert_ret) {\
break;\
}\
ev__assertion_failure(#exp, __FILE__, __LINE__, fmt, ##__VA_ARGS__);\
} while (0)
/**
* @brief Assertion failure function.
* @warning Do NOT use this function directly. Use #EV_ASSERT() instead.
* @param[in] exp The expression that failed.
* @param[in] file The file that \p exp located.
* @param[in] line The line that \p exp located.
* @param[in] fmt Custom print format. Set to NULL if no user print format.
* @param[in] ... Variable list for \p fmt.
*/
EV_LOCAL void ev__assertion_failure(const char* exp, const char* file, int line, const char* fmt, ...);
#ifdef __cplusplus
}
#endif
#endif
// #line 8 "ev.c"
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/async_internal.h
// SIZE: 657
// SHA-256: 9e8f86f5b191bbc637a6bc61995673e4f813fd5c329873e4c84ee9dc653edc78
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/async_internal.h"
#ifndef __EV_ASYNC_INTERNAL_H__
#define __EV_ASYNC_INTERNAL_H__
#ifdef __cplusplus
extern "C" {
#endif
struct ev_async
{
ev_handle_t base; /**< Base object */
ev_async_cb activate_cb; /**< Activate callback */
void *activate_arg; /**< Activate argument. */
ev_async_cb close_cb; /**< Close callback */
void* close_arg;
EV_ASYNC_BACKEND backend; /**< Platform related fields */
};
/**
* @brief Force destroy #ev_async_t.
* @param[in] handle A initialized #ev_async_t handler.
*/
EV_LOCAL void ev__async_exit_force(ev_async_t *handle);
#ifdef __cplusplus
}
#endif
#endif
// #line 9 "ev.c"
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/atomic_internal.h
// SIZE: 221
// SHA-256: 10bdb611512d3b12be054a8ae00e7872a507320a03ae77917fb33d2998241a6b
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/atomic_internal.h"
#ifndef __EV_ATOMIC_INTERNAL_H__
#define __EV_ATOMIC_INTERNAL_H__
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Cleanup atomic context.
*/
EV_LOCAL void ev__atomic_exit(void);
#ifdef __cplusplus
}
#endif
#endif
// #line 10 "ev.c"
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/handle_internal.h
// SIZE: 4036
// SHA-256: 41b14a8f0d8cbb8cc9907aa2d0c27af34b45f0fd7c46e356be64dffcff190d78
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/handle_internal.h"
#ifndef __EV_HANDLE_INTERNAL_H__
#define __EV_HANDLE_INTERNAL_H__
#ifdef __cplusplus
extern "C" {
#endif
typedef enum ev_handle_flag
{
/* Used by all handles. Bit 0-7. */
EV_HANDLE_CLOSING = 0x01 << 0x00, /**< 1. Handle is going to close */
EV_HANDLE_CLOSED = 0x01 << 0x01, /**< 2. Handle is closed */
EV_HANDLE_ACTIVE = 0x01 << 0x02, /**< 4. Handle is busy */
/* #EV_ROLE_EV_TCP */
EV_HANDLE_TCP_LISTING = 0x01 << 0x08, /**< 256. This is a listen socket and is listening */
EV_HANDLE_TCP_ACCEPTING = 0x01 << 0x09, /**< 512. This is a socket waiting for accept */
EV_HANDLE_TCP_STREAMING = 0x01 << 0x0A, /**< 1024. This is a socket waiting for read or write */
EV_HANDLE_TCP_CONNECTING = 0x01 << 0x0B, /**< 2048. This is a connect and waiting for connect complete */
EV_HABDLE_TCP_BOUND = 0x01 << 0x0C, /**< 4096. Socket is bond to address */
/* #EV_ROLE_EV_UDP */
EV_HANDLE_UDP_IPV6 = 0x01 << 0x08, /**< 256. This socket have IPv6 ability */
EV_HANDLE_UDP_CONNECTED = 0x01 << 0x09, /**< 512. This socket is connected */
EV_HANDLE_UDP_BOUND = 0x01 << 0x0A, /**< 1024. Socket is bond to address */
EV_HANDLE_UDP_BYPASS_IOCP = 0x01 << 0x0B, /**< 2048. FILE_SKIP_SET_EVENT_ON_HANDLE | FILE_SKIP_COMPLETION_PORT_ON_SUCCESS */
/* #EV_ROLE_EV_PIPE */
EV_HANDLE_PIPE_IPC = 0x01 << 0x08, /**< 256. This pipe is support IPC */
EV_HANDLE_PIPE_STREAMING = 0x01 << 0x09, /**< 512. This pipe is initialized by #ev_stream_t */
} ev_handle_flag_t;
/**
* @brief Initialize a handle.
*
* A initialized handle will be linked with \p loop. By default the \p handle
* is in #ev_loop_t::handles::idle_list. If the \p handle is active (The event
* counter is non-zero), the handle is moved into #ev_loop_t::handles::active_list.
*
* @note Once a handle is initialized, it must call #ev__handle_exit() when no
* longer needed.
* @param[in] loop The loop own the handle
* @param[out] handle A pointer to the structure
* @param[in] role Who we are
*/
EV_LOCAL void ev__handle_init(ev_loop_t* loop, ev_handle_t* handle, ev_role_t role);
/**
* @brief Close the handle
* @note The handle will not closed until close_cb was called, which was given
* by #ev__handle_init()
* @note #ev__handle_exit() never reset active_events counter for you. You always
* need to balance active_events counter yourself.
* @param[in] handle handler
* @param[in] close_cb Close callback. If non-null, the \p close_cb will be
* called in next event loop. If null, the handle will be closed synchronously.
*/
EV_LOCAL void ev__handle_exit(ev_handle_t* handle, ev_handle_cb close_cb);
/**
* @brief Active handle.
* @param[in] handle Handler.
*/
EV_LOCAL void ev__handle_active(ev_handle_t* handle);
/**
* @brief De-active handle.
* @param[in] handle Handler.
*/
EV_LOCAL void ev__handle_deactive(ev_handle_t* handle);
/**
* @brief Check if the handle is in active state
* @param[in] handle handler
* @return bool
*/
EV_LOCAL int ev__handle_is_active(ev_handle_t* handle);
/**
* @brief Check if the handle is in closing or closed state
* @param[in] handle handler
* @return bool
*/
EV_LOCAL int ev__handle_is_closing(ev_handle_t* handle);
/**
* @brief Queue a task.
* This task will be execute in next loop.
* @param[in] handle handler.
* @param[in] callback Task callback
* @return #ev_errno_t
*/
EV_LOCAL int ev__backlog_submit(ev_handle_t* handle, ev_handle_cb callback);
/**
* @brief Process backlog events.
* @param[in] loop Event loop.
* @return Active count.
*/
EV_LOCAL size_t ev__process_backlog(ev_loop_t* loop);
/**
* @brief Process endgame events.
* @param[in] loop Event loop.
* @return Active count.
*/
EV_LOCAL size_t ev__process_endgame(ev_loop_t* loop);
#ifdef __cplusplus
}
#endif
#endif
// #line 11 "ev.c"
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/loop_internal.h
// SIZE: 3692
// SHA-256: 7fe057f7482c9dfdd6f873d3c1a03209f4d21189d5ce97f09a1c106c071521ac
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/loop_internal.h"
#ifndef __EV_LOOP_INTERNAL_H__
#define __EV_LOOP_INTERNAL_H__
#ifdef __cplusplus
extern "C" {
#endif
typedef enum ev_ipc_frame_flag
{
EV_IPC_FRAME_FLAG_INFORMATION = 1,
} ev_ipc_frame_flag_t;
/**
* @brief Event loop type.
*/
struct ev_loop
{
uint64_t hwtime; /**< A fast clock time in milliseconds */
struct
{
ev_list_t idle_list; /**< (#ev_handle::node) All idle handles */
ev_list_t active_list; /**< (#ev_handle::node) All active handles */
} handles; /**< table for handles */
ev_list_t backlog_queue; /**< Backlog queue */
ev_list_t endgame_queue; /**< Close queue */
/**
* @brief Timer context
*/
struct
{
ev_map_t heap; /**< #ev_timer_t::node. Timer heap */
} timer;
struct
{
struct ev_threadpool *pool; /**< Thread pool */
ev_list_node_t node; /**< node for #ev_threadpool_t::loop_table */
ev_mutex_t *mutex; /**< Work queue lock */
ev_list_t work_queue; /**< Work queue */
} threadpool;
struct
{
unsigned b_stop : 1; /**< Flag: need to stop */
} mask;
EV_LOOP_BACKEND backend; /**< Platform related implementation */
};
/**
* @brief Get event loop for the handle.
* @param[in] handle handler
* @return Event loop
*/
EV_LOCAL ev_loop_t *ev__handle_loop(ev_handle_t *handle);
/**
* @brief Check IPC frame header
* @param[in] buffer Buffer to check
* @param[in] size Buffer size
* @return bool
*/
EV_LOCAL int ev__ipc_check_frame_hdr(const void *buffer, size_t size);
/**
* @brief Initialize IPC frame header
* @param[out] hdr Frame header to initialize
* @param[in] flags Control flags
* @param[in] exsz Extra information size
* @param[in] dtsz Data size
*/
EV_LOCAL void ev__ipc_init_frame_hdr(ev_ipc_frame_hdr_t *hdr, uint8_t flags,
uint16_t exsz, uint32_t dtsz);
/**
* @brief Update loop time
* @param[in] loop loop handler
*/
EV_LOCAL void ev__loop_update_time(ev_loop_t *loop);
/**
* @brief Get minimal length of specific \p addr type.
* @param[in] addr A valid sockaddr buffer
* @return A valid minimal length, or (socklen_t)-1 if error.
*/
EV_LOCAL socklen_t ev__get_addr_len(const struct sockaddr *addr);
/**
* @brief Initialize #ev_write_t
* @param[out] req A write request to be initialized
* @param[in] bufs Buffer list
* @param[in] nbuf Buffer list size, can not larger than #EV_IOV_MAX.
* @return #ev_errno_t
*/
EV_LOCAL int ev__write_init(ev_write_t *req, ev_buf_t *bufs, size_t nbuf);
/**
* @brief Cleanup write request
* @param[in] req Write request
*/
EV_LOCAL void ev__write_exit(ev_write_t *req);
/**
* @brief Initialize #ev_read_t
* @param[out] req A read request to be initialized
* @param[in] bufs Buffer list
* @param[in] nbuf Buffer list size, can not larger than #EV_IOV_MAX.
* @return #ev_errno_t
*/
EV_LOCAL int ev__read_init(ev_read_t *req, ev_buf_t *bufs, size_t nbuf);
/**
* @brief Cleanup read request
* @param[in] req read request
*/
EV_LOCAL void ev__read_exit(ev_read_t *req);
/**
* @brief Initialize backend
* @param[in] loop loop handler
* @return #ev_errno_t
*/
EV_LOCAL int ev__loop_init_backend(ev_loop_t *loop);
/**
* @brief Destroy backend
* @param[in] loop loop handler
*/
EV_LOCAL void ev__loop_exit_backend(ev_loop_t *loop);
/**
* @brief Wait for IO event and process
* @param[in] loop loop handler
* @param[in] timeout timeout in milliseconds
*/
EV_LOCAL void ev__poll(ev_loop_t *loop, uint32_t timeout);
#ifdef __cplusplus
}
#endif
#endif
// #line 12 "ev.c"
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/fs_internal.h
// SIZE: 3915
// SHA-256: 80ed2860face86c8fbc31856c0d06d938427607abdd98c3da42be92d98b41d5e
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/fs_internal.h"
#ifndef __EV_FILESYSTEM_INTERNAL_H__
#define __EV_FILESYSTEM_INTERNAL_H__
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Readdir callback.
* @param[in] info Dirent information.
* @param[in] arg User defined data.
* @return non-zero to stop.
*/
typedef int (*ev_fs_readdir_cb)(ev_dirent_t* info, void* arg);
/**
* @brief Close file handle.
* @return #ev_errno_t
*/
EV_LOCAL int ev__fs_close(ev_os_file_t file);
/**
* @brief Open file.
* @param[out] file File handle.
* @param[in] path File path.
* @param[in] flags Open flags.
* @param[in] mode Creation mode.
* @return #ev_errno_t
*/
EV_LOCAL int ev__fs_open(ev_os_file_t* file, const char* path, int flags,
int mode);
/**
* @brief Same as [seek(2)](https://linux.die.net/man/2/seek)
* @param[in] file File handle.
* @param[in] whence Directive.
* @param[in] offset Offset.
* @return #ev_errno_t
*/
EV_LOCAL int64_t ev__fs_seek(ev_os_file_t file, int whence, int64_t offset);
/**
* @brief Same as [readv(2)](https://linux.die.net/man/2/readv)
* @note For windows users, the \p file can NOT open with FILE_FLAG_OVERLAPPED.
* @param[in] file File handle.
* @param[in] bufs Buffer list
* @param[in] nbuf Buffer number
* @return #ev_errno_t
*/
EV_LOCAL ssize_t ev__fs_readv(ev_os_file_t file, ev_buf_t* bufs, size_t nbuf);
/**
* @brief Same as [preadv(2)](https://linux.die.net/man/2/preadv)
* @note For windows users, the \p file can NOT open with FILE_FLAG_OVERLAPPED.
* @param[in] file File handle.
* @param[in] bufs Buffer list
* @param[in] nbuf Buffer number
* @param[in] offset Offset.
* @return #ev_errno_t
*/
EV_LOCAL ssize_t ev__fs_preadv(ev_os_file_t file, ev_buf_t* bufs, size_t nbuf,
int64_t offset);
/**
* @brief Same as [writev(2)](https://linux.die.net/man/2/writev)
* @note For windows users, the \p file can NOT open with FILE_FLAG_OVERLAPPED.
* @param[in] file File handle.
* @param[in] bufs Buffer list
* @param[in] nbuf Buffer number
* @return #ev_errno_t
*/
EV_LOCAL ssize_t ev__fs_writev(ev_os_file_t file, ev_buf_t* bufs, size_t nbuf);
/**
* @brief Same as [pwritev(2)](https://linux.die.net/man/2/pwritev)
* @note For windows users, the \p file can NOT open with FILE_FLAG_OVERLAPPED.
* @param[in] file File handle.
* @param[in] bufs Buffer list
* @param[in] nbuf Buffer number
* @param[in] offset Offset.
* @return #ev_errno_t
*/
EV_LOCAL ssize_t ev__fs_pwritev(ev_os_file_t file, ev_buf_t* bufs, size_t nbuf,
int64_t offset);
/**
* @brief Same as [fstat(2)](https://linux.die.net/man/2/fstat)
* @param[in] file File handle.
* @param[out] statbuf File information.
* @return #ev_errno_t
*/
EV_LOCAL int ev__fs_fstat(ev_os_file_t file, ev_fs_stat_t* statbuf);
/**
* @brief Same as [readdir(3)](https://man7.org/linux/man-pages/man3/readdir.3.html)
* @param[in] path Directory path. The path can be end with or without '/'.
* @param[in] cb Dirent callback.
* @param[in] arg User defined data.
* @return #ev_errno_t.
*/
EV_LOCAL int ev__fs_readdir(const char* path, ev_fs_readdir_cb cb, void* arg);
/**
* @brief Same as [mkdir(2)](https://man7.org/linux/man-pages/man2/mkdir.2.html),
* and make parent directories as needed.
* @param[in] path Directory path.
* @param[in] mode The mode for the new directory.
* @return #ev_errno_t
*/
EV_LOCAL int ev__fs_mkdir(const char* path, int mode);
/**
* @brief Remove file or directory.
* @param[in] path File path.
* @param[in] recursive Recursive if it is a directory.
* @return #ev_errno_t
*/
EV_LOCAL int ev__fs_remove(const char* path, int recursive);
#ifdef __cplusplus
}
#endif
#endif
// #line 13 "ev.c"
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/misc_internal.h
// SIZE: 767
// SHA-256: d868e98713600d57cf502123aaffa79eb35ea0d27edc2b49be60d238602e0e49
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/misc_internal.h"
#ifndef __EV_MISC_INTERNAL_H__
#define __EV_MISC_INTERNAL_H__
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Translate system error into #ev_errno_t
* @param[in] syserr System error
* @return #ev_errno_t
*/
EV_LOCAL int ev__translate_sys_error(int syserr);
/**
* @brief Translate posix error into #ev_errno_t
* @param[in] syserr Posix error.
* @return #ev_errno_t
*/
EV_LOCAL int ev__translate_posix_sys_error(int syserr);
/**
* @brief Fill \p buf with random data.
* @param[out] buf The buffer to fill.
* @param[in] len The number of bytes to fill.
* @return #ev_errno_t
*/
EV_LOCAL int ev__random(void* buf, size_t len);
EV_LOCAL void ev__backend_shutdown(void);
#ifdef __cplusplus
}
#endif
#endif
// #line 14 "ev.c"
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/pipe_internal.h
// SIZE: 3341
// SHA-256: 667e85a7dfe0d5005204802b13581dea97d718d58eaa23117b184787c2688fa3
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/pipe_internal.h"
#ifndef __EV_PIPE_INTERNAL_H__
#define __EV_PIPE_INTERNAL_H__
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Write request token for pipe.
*/
typedef struct ev_pipe_write_req
{
ev_write_t base; /**< Base object */
ev_pipe_write_cb ucb; /**< User callback */
void *ucb_arg; /**< User defined argument. */
struct
{
ev_role_t role; /**< The type of handle to send */
union {
ev_os_socket_t os_socket; /**< A EV handle instance */
} u;
} handle;
EV_PIPE_WRITE_BACKEND backend; /**< Backend */
} ev_pipe_write_req_t;
struct ev_pipe
{
ev_handle_t base; /**< Base object */
ev_pipe_cb close_cb; /**< User close callback */
void *close_arg; /**< Close argument. */
ev_os_pipe_t pipfd; /**< Pipe handle */
EV_PIPE_BACKEND backend; /**< Platform related implementation */
};
/**
* @brief Initialize #ev_pipe_read_req_t
* @param[out] req A read request to be initialized
* @param[in] bufs Buffer list
* @param[in] nbuf Buffer list size, can not larger than #EV_IOV_MAX.
* @param[in] cb Read complete callback
* @return #ev_errno_t
*/
EV_LOCAL int ev__pipe_read_init(ev_pipe_read_req_t *req, ev_buf_t *bufs,
size_t nbuf, ev_pipe_read_cb cb);
/**
* @brief Initialize #ev_pipe_write_req_t
* @param[out] req A write request to be initialized
* @param[in] bufs Buffer list
* @param[in] nbuf Buffer list size, can not larger than #EV_IOV_MAX.
* @param[in] cb Write complete callback
* @param[in] arg User defined argument.
* @return #ev_errno_t
*/
EV_LOCAL int ev__pipe_write_init(ev_pipe_write_req_t *req, ev_buf_t *bufs,
size_t nbuf, ev_pipe_write_cb cb, void* arg);
/**
* @brief Initialize #ev_pipe_write_req_t
*
* The extend version of #ev__pipe_write_init(). You should use this function if
* any of following condition is meet:
*
* + The value of \p nbuf is larger than #EV_IOV_MAX.<br>
* In this case you should pass \p iov_bufs as storage, the minimum value of
* \p iov_size can be calculated by #EV_IOV_BUF_SIZE(). \p take the
* ownership of \p iov_bufs, so you cannot modify or free \p iov_bufs until \p
* callback is called.
*
* + Need to transfer a handle to peer.<br>
* In this case you should set the type of handle via \p handle_role and
* pass the address of the handle via \p handle_addr. \p req does not take the
* ownership of the handle, but the handle should not be closed or destroy until
* \p callback is called.
*
* @param[out] req A write request to be initialized
* @param[in] cb Write complete callback
* @param[in] arg User defined argument.
* @param[in] bufs Buffer list
* @param[in] nbuf Buffer list size
* @param[in] handle_role The type of handle to send
* @param[in] handle_addr The address of handle to send
* @return #ev_errno_t
*/
EV_LOCAL int ev__pipe_write_init_ext(ev_pipe_write_req_t *req,
ev_pipe_write_cb cb, void *arg,
ev_buf_t *bufs, size_t nbuf,
ev_role_t handle_role, void *handle_addr);
#ifdef __cplusplus
}
#endif
#endif
// #line 15 "ev.c"
////////////////////////////////////////////////////////////////////////////////
// FILE: ev/ringbuffer.h
// SIZE: 7166
// SHA-256: 8617563810073a2580dc1cdfaed47c9e38b94c937c9d7a23ad174aeeccca8075
////////////////////////////////////////////////////////////////////////////////
// #line 1 "ev/ringbuffer.h"
#ifndef __EV_RINGBUFFER_INTERNAL_H__
#define __EV_RINGBUFFER_INTERNAL_H__
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Ring buffer flags
* @see ring_buffer_reserve
* @see ring_buffer_commit
*/
typedef enum ring_buffer_flag
{
RINGBUFFER_FLAG_OVERWRITE = 0x01 << 0x00, /**< Overwrite data if no enough free space */
RINGBUFFER_FLAG_DISCARD = 0x01 << 0x01, /**< Discard operation */
RINGBUFFER_FLAG_ABANDON = 0x01 << 0x02, /**< Abandon token */
}ring_buffer_flag_t;
typedef enum ring_buffer_node_state
{
RINGBUFFER_STAT_WRITING, /** Writing */
RINGBUFFER_STAT_COMMITTED, /** Committed */
RINGBUFFER_STAT_READING, /** Reading */
}ring_buffer_node_state_t;
/**
* @brief Ring buffer data token
*/
typedef struct ring_buffer_token
{
union
{
size_t size; /**< Data length */
void* _align; /**< Padding field used for make sure address align */
}size;
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4200)
#endif
uint8_t data[]; /**< Data body */
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
}ring_buffer_token_t;
typedef struct ring_buffer_node
{
struct
{
size_t off_next; /**< Next position in chain. NOT `NULL` in any condition. */
size_t off_prev; /**< Previous position in chain. NOT `NULL` in any condition. */
}chain_pos;
struct
{
size_t off_newer; /**< Newer node. `0` if not exists. */
size_t off_older; /**< Older node. `NULL` if not exists. */
}chain_time;
ring_buffer_node_state_t state; /**< Node status */
ring_buffer_token_t token; /**< User data */
}ring_buffer_node_t;
/**
* @brief Ring buffer counter
*/
typedef struct ring_buffer_counter
{
size_t committed; /**< The number of committed nodes */
size_t writing; /**< The number of writing nodes */
size_t reading; /**< The number of reading nodes */
}ring_buffer_counter_t;
/**
* @brief Ring buffer
*/
typedef struct ring_buffer
{
struct
{
size_t capacity; /**< Available length. */
}config;