-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathswoole.h
2647 lines (2290 loc) Β· 67.2 KB
/
swoole.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
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
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
| Twosee <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifndef SWOOLE_H_
#define SWOOLE_H_
#if defined(HAVE_CONFIG_H) && !defined(COMPILE_DL_SWOOLE)
#include "config.h"
#elif defined(PHP_ATOM_INC) || defined(ZEND_SIGNALS)
#include "php_config.h"
#endif
#ifdef __cplusplus
#define SW_EXTERN_C_BEGIN extern "C" {
#define SW_EXTERN_C_END }
#else
#define SW_EXTERN_C_BEGIN
#define SW_EXTERN_C_END
#endif
SW_EXTERN_C_BEGIN
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/*--- C standard library ---*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <poll.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/mman.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#if defined(HAVE_CPU_AFFINITY)
#ifdef __FreeBSD__
#include <sys/types.h>
#include <sys/cpuset.h>
#include <pthread_np.h>
typedef cpuset_t cpu_set_t;
#else
#include <sched.h>
#endif
#endif
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach_time.h>
#include <sys/sysctl.h>
#define ORWL_NANO (+1.0E-9)
#define ORWL_GIGA UINT64_C(1000000000)
static double orwl_timebase = 0.0;
static uint64_t orwl_timestart = 0;
#ifndef HAVE_CLOCK_GETTIME
int clock_gettime(clock_id_t which_clock, struct timespec *t);
#endif
#endif
#ifndef ulong
#define ulong unsigned long
#endif
typedef unsigned long ulong_t;
#ifndef PRIu64
#define PRIu64 "llu"
#endif
#ifndef PRIx64
#define PRIx64 "llx"
#endif
#if defined(__GNUC__)
#if __GNUC__ >= 3
#define sw_inline inline __attribute__((always_inline))
#else
#define sw_inline inline
#endif
#elif defined(_MSC_VER)
#define sw_inline __forceinline
#else
#define sw_inline inline
#endif
#if defined(__GNUC__) && __GNUC__ >= 4
#define SW_API __attribute__ ((visibility("default")))
#else
#define SW_API
#endif
#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
#define MAP_ANONYMOUS MAP_ANON
#endif
#if defined(MAP_HUGETLB) || defined(MAP_ALIGNED_SUPER)
#define MAP_HUGE_PAGE 1
#endif
#ifndef SOCK_NONBLOCK
#define SOCK_NONBLOCK O_NONBLOCK
#endif
#ifndef CLOCK_REALTIME
#define CLOCK_REALTIME 0
#endif
#if !defined(__GNUC__) || __GNUC__ < 3
#define __builtin_expect(x, expected_value) (x)
#endif
#define sw_likely(x) __builtin_expect(!!(x), 1)
#define sw_unlikely(x) __builtin_expect(!!(x), 0)
#define SW_START_LINE "-------------------------START----------------------------"
#define SW_END_LINE "--------------------------END-----------------------------"
#define SW_ECHO_RED "\e[31m%s\e[0m"
#define SW_ECHO_GREEN "\e[32m%s\e[0m"
#define SW_ECHO_YELLOW "\e[33m%s\e[0m"
#define SW_ECHO_BLUE "\e[34m%s\e[0m"
#define SW_ECHO_MAGENTA "\e[35m%s\e[0m"
#define SW_ECHO_CYAN "\e[36m%s\e[0m"
#define SW_ECHO_WHITE "\e[37m%s\e[0m"
#define SW_COLOR_RED 1
#define SW_COLOR_GREEN 2
#define SW_COLOR_YELLOW 3
#define SW_COLOR_BLUE 4
#define SW_COLOR_MAGENTA 5
#define SW_COLOR_CYAN 6
#define SW_COLOR_WHITE 7
#define SW_SPACE ' '
#define SW_CRLF "\r\n"
#define SW_CRLF_LEN 2
#define SW_ASCII_CODE_0 64
#define SW_ASCII_CODE_Z 106
/*----------------------------------------------------------------------------*/
#include "swoole_config.h"
#include "swoole_version.h"
#include "atomic.h"
#include "buffer.h"
#include "hashmap.h"
#include "heap.h"
#include "ring_queue.h"
#include "error.h"
#define SW_MAX(A, B) ((A) > (B) ? (A) : (B))
#define SW_MIN(A, B) ((A) < (B) ? (A) : (B))
#ifndef MAX
#define MAX(A, B) SW_MAX(A, B)
#endif
#ifndef MIN
#define MIN(A, B) SW_MIN(A, B)
#endif
#ifdef SW_DEBUG
#define SW_ASSERT(e) assert(e)
#define SW_ASSERT_1BYTE(v) do { \
size_t i = 0, n = 0; \
for (; i < sizeof(v); i++) { \
n += ((v >> i) & 1) ? 1 : 0; \
} \
assert(n == 1); \
} while (0)
#else
#define SW_ASSERT(e)
#define SW_ASSERT_1BYTE(v)
#endif
#define SW_START_SLEEP usleep(100000) //sleep 1s,wait fork and pthread_create
/*-----------------------------------Memory------------------------------------*/
// Evaluates to the number of elements in 'array'
#define SW_ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
#define SW_DEFAULT_ALIGNMENT sizeof(unsigned long)
#define SW_MEM_ALIGNED_SIZE(size) \
SW_MEM_ALIGNED_SIZE_EX(size, SW_DEFAULT_ALIGNMENT)
#define SW_MEM_ALIGNED_SIZE_EX(size, alignment) \
(((size) + ((alignment) - 1LL)) & ~((alignment) - 1LL))
#ifdef SW_USE_EMALLOC
#define sw_malloc emalloc
#define sw_free efree
#define sw_calloc ecalloc
#define sw_realloc erealloc
#else
#ifdef SW_USE_JEMALLOC
#include <jemalloc/jemalloc.h>
#define sw_malloc je_malloc
#define sw_free je_free
#define sw_calloc je_calloc
#define sw_realloc je_realloc
#else
#define sw_malloc malloc
#define sw_free free
#define sw_calloc calloc
#define sw_realloc realloc
#endif
#endif
static sw_inline int sw_mem_equal(const void *v1, size_t s1, const void *v2, size_t s2)
{
return s1 == s2 && memcmp(v1, v2, s2) == 0;
}
/*----------------------------------String-------------------------------------*/
#define SW_STRS(s) s, sizeof(s)
#define SW_STRL(s) s, sizeof(s)-1
#if defined(SW_USE_JEMALLOC) || defined(SW_USE_TCMALLOC)
#define sw_strdup swoole_strdup
#define sw_strndup swoole_strndup
#else
#define sw_strdup strdup
#define sw_strndup strndup
#endif
#define SW_Z_BEST_SPEED 1
/** always return less than size, zero termination */
size_t sw_snprintf(char *buf, size_t size, const char *format, ...);
size_t sw_vsnprintf(char *buf, size_t size, const char *format, va_list args);
static sw_inline char* swoole_strdup(const char *s)
{
size_t l = strlen(s) + 1;
char *p = (char *) sw_malloc(l);
if (sw_likely(p))
{
memcpy(p, s, l);
}
return p;
}
static sw_inline char* swoole_strndup(const char *s, size_t n)
{
char *p = (char *) sw_malloc(n + 1);
if (sw_likely(p))
{
strncpy(p, s, n)[n] = '\0';
}
return p;
}
/* string equal */
static sw_inline unsigned int swoole_streq(const char *str1, size_t len1, const char *str2, size_t len2)
{
return (len1 == len2) && (strncmp(str1, str2, len1) == 0);
}
static sw_inline unsigned int swoole_strcaseeq(const char *str1, size_t len1, const char *str2, size_t len2)
{
return (len1 == len2) && (strncasecmp(str1, str2, len1) == 0);
}
static sw_inline unsigned int swoole_strct(const char *pstr, size_t plen, const char *sstr, size_t slen)
{
return (plen >= slen) && (strncmp(pstr, sstr, slen) == 0);
}
static sw_inline unsigned int swoole_strcasect(const char *pstr, size_t plen, const char *sstr, size_t slen)
{
return (plen >= slen) && (strncasecmp(pstr, sstr, slen) == 0);
}
#define SW_STREQ(str, len, const_str) swoole_streq(str, len, SW_STRL(const_str))
#define SW_STRCASEEQ(str, len, const_str) swoole_strcaseeq(str, len, SW_STRL(const_str))
/* string contain */
#define SW_STRCT(str, len, const_sub_str) swoole_strct(str, len, SW_STRL(const_sub_str))
#define SW_STRCASECT(str, len, const_sub_str) swoole_strcasect(str, len, SW_STRL(const_sub_str))
/*--------------------------------Constants------------------------------------*/
enum swResult_code
{
SW_OK = 0,
SW_ERR = -1,
};
enum swReturn_code
{
SW_CONTINUE = 1,
SW_WAIT = 2,
SW_CLOSE = 3,
SW_ERROR = 4,
SW_READY = 5,
};
enum swFd_type
{
SW_FD_SESSION, //server stream session
SW_FD_STREAM_SERVER, //server stream port
SW_FD_DGRAM_SERVER, //server dgram port
SW_FD_PIPE,
SW_FD_STREAM,
SW_FD_AIO,
/**
* Coroutine Socket
*/
SW_FD_CORO_SOCKET,
/**
* socket poll fd [coroutine::socket_poll]
*/
SW_FD_CORO_POLL,
/**
* event waiter
*/
SW_FD_CORO_EVENT,
/**
* signalfd
*/
SW_FD_SIGNAL,
SW_FD_DNS_RESOLVER,
/**
* SW_FD_USER or SW_FD_USER+n: for custom event
*/
SW_FD_USER = 16,
SW_FD_STREAM_CLIENT,
SW_FD_DGRAM_CLIENT,
};
enum swBool_type
{
SW_TRUE = 1,
SW_FALSE = 0,
};
enum swEvent_type
{
SW_EVENT_NULL = 0,
SW_EVENT_DEAULT = 1u << 8,
SW_EVENT_READ = 1u << 9,
SW_EVENT_WRITE = 1u << 10,
SW_EVENT_RDWR = SW_EVENT_READ | SW_EVENT_WRITE,
SW_EVENT_ERROR = 1u << 11,
SW_EVENT_ONCE = 1u << 12,
};
enum swGlobal_hook_type
{
SW_GLOBAL_HOOK_BEFORE_SERVER_START,
SW_GLOBAL_HOOK_BEFORE_CLIENT_START,
SW_GLOBAL_HOOK_BEFORE_WORKER_START,
SW_GLOBAL_HOOK_ON_CORO_START,
SW_GLOBAL_HOOK_ON_CORO_STOP,
SW_GLOBAL_HOOK_ON_REACTOR_CREATE,
};
enum swFork_type
{
SW_FORK_SPAWN = 0,
SW_FORK_EXEC = 1 << 1,
SW_FORK_DAEMON = 1 << 2,
SW_FORK_PRECHECK = 1 << 3,
};
//-------------------------------------------------------------------------------
enum swServer_mode
{
SW_MODE_BASE = 1,
SW_MODE_PROCESS = 2,
};
//-------------------------------------------------------------------------------
enum swSocket_type
{
SW_SOCK_TCP = 1,
SW_SOCK_UDP = 2,
SW_SOCK_TCP6 = 3,
SW_SOCK_UDP6 = 4,
SW_SOCK_UNIX_STREAM = 5, //unix sock stream
SW_SOCK_UNIX_DGRAM = 6, //unix sock dgram
};
#define SW_SOCK_SSL (1u << 9)
//-------------------------------------------------------------------------------
enum swLog_level
{
SW_LOG_DEBUG = 0,
SW_LOG_TRACE,
SW_LOG_INFO,
SW_LOG_NOTICE,
SW_LOG_WARNING,
SW_LOG_ERROR,
SW_LOG_NONE,
};
//-------------------------------------------------------------------------------
enum swWorker_status
{
SW_WORKER_BUSY = 1,
SW_WORKER_IDLE = 2,
};
//-------------------------------------------------------------------------------
#define swInfo(str,...) \
if (SW_LOG_INFO >= SwooleG.log_level) { \
size_t _sw_error_len = sw_snprintf(sw_error,SW_ERROR_MSG_SIZE,str,##__VA_ARGS__); \
SwooleG.write_log(SW_LOG_INFO, sw_error, _sw_error_len); \
}
#define swNotice(str,...) \
if (SW_LOG_NOTICE >= SwooleG.log_level) { \
size_t _sw_error_len = sw_snprintf(sw_error,SW_ERROR_MSG_SIZE,str,##__VA_ARGS__); \
SwooleG.write_log(SW_LOG_NOTICE, sw_error, _sw_error_len); \
}
#define swSysNotice(str,...) \
do{ \
SwooleG.error = errno; \
if (SW_LOG_ERROR >= SwooleG.log_level) { \
size_t _sw_error_len = sw_snprintf(sw_error,SW_ERROR_MSG_SIZE,"%s(:%d): " str ", Error: %s[%d]",__func__,__LINE__,##__VA_ARGS__,swoole_strerror(errno),errno); \
SwooleG.write_log(SW_LOG_NOTICE, sw_error, _sw_error_len); \
} \
} while(0)
#define swWarn(str,...) \
do{ \
if (SW_LOG_WARNING >= SwooleG.log_level) { \
size_t _sw_error_len = sw_snprintf(sw_error,SW_ERROR_MSG_SIZE,"%s: " str,__func__,##__VA_ARGS__); \
SwooleG.write_log(SW_LOG_WARNING, sw_error, _sw_error_len); \
} \
} while(0)
#define swSysWarn(str,...) \
do{ \
SwooleG.error = errno; \
if (SW_LOG_ERROR >= SwooleG.log_level) { \
size_t _sw_error_len = sw_snprintf(sw_error,SW_ERROR_MSG_SIZE,"%s(:%d): " str ", Error: %s[%d]",__func__,__LINE__,##__VA_ARGS__,swoole_strerror(errno),errno); \
SwooleG.write_log(SW_LOG_WARNING, sw_error, _sw_error_len); \
} \
} while(0)
#define swError(str,...) \
do{ \
size_t _sw_error_len = sw_snprintf(sw_error, SW_ERROR_MSG_SIZE, str, ##__VA_ARGS__); \
SwooleG.write_log(SW_LOG_ERROR, sw_error, _sw_error_len); \
exit(1); \
} while(0)
#define swSysError(str,...) \
do{ \
size_t _sw_error_len = sw_snprintf(sw_error,SW_ERROR_MSG_SIZE,"%s(:%d): " str ", Error: %s[%d]",__func__,__LINE__,##__VA_ARGS__,swoole_strerror(errno),errno); \
SwooleG.write_log(SW_LOG_ERROR, sw_error, _sw_error_len); \
exit(1); \
} while(0)
#define swFatalError(code, str,...) \
do { \
SwooleG.fatal_error(code, str, ##__VA_ARGS__); \
abort(); \
} while (0)
#define swoole_error_log(level, __errno, str, ...) \
do{ \
SwooleG.error = __errno; \
if (level >= SwooleG.log_level){ \
size_t _sw_error_len = sw_snprintf(sw_error, SW_ERROR_MSG_SIZE, "%s (ERRNO %d): " str,__func__,__errno,##__VA_ARGS__); \
SwooleG.write_log(level, sw_error, _sw_error_len); \
} \
} while(0)
#ifdef SW_DEBUG
#define swDebug(str,...) \
if (SW_LOG_DEBUG >= SwooleG.log_level) { \
size_t _sw_error_len = sw_snprintf(sw_error, SW_ERROR_MSG_SIZE, "%s(:%d): " str, __func__, __LINE__, ##__VA_ARGS__); \
SwooleG.write_log(SW_LOG_DEBUG, sw_error, _sw_error_len); \
}
#define swHexDump(data, length) \
do { \
const char *__data = (data); \
size_t __length = (length); \
swDebug("+----------+------------+-----------+-----------+------------+------------------+"); \
for (size_t of = 0; of < __length; of += 16) \
{ \
char hex[16 * 3 + 1]; \
char str[16 + 1]; \
size_t i, hof = 0, sof = 0; \
for (i = of; i < of + 16 && i < __length; i++) \
{ \
hof += sprintf(hex + hof, "%02x ", (__data)[i] & 0xff); \
sof += sprintf(str + sof, "%c", isprint((int) (__data)[i]) ? (__data)[i] : '.'); \
} \
swDebug("| %08x | %-48s| %-16s |", of, hex, str); \
} \
swDebug("+----------+------------+-----------+-----------+------------+------------------+"); \
} while (0)
#else
#define swDebug(str,...)
#define swHexDump(data, length)
#endif
enum swTrace_type
{
/**
* Server
*/
SW_TRACE_SERVER = 1u << 1,
SW_TRACE_CLIENT = 1u << 2,
SW_TRACE_BUFFER = 1u << 3,
SW_TRACE_CONN = 1u << 4,
SW_TRACE_EVENT = 1u << 5,
SW_TRACE_WORKER = 1u << 6,
SW_TRACE_MEMORY = 1u << 7,
SW_TRACE_REACTOR = 1u << 8,
SW_TRACE_PHP = 1u << 9,
SW_TRACE_HTTP = 1u << 10,
SW_TRACE_HTTP2 = 1u << 11,
SW_TRACE_EOF_PROTOCOL = 1u << 12,
SW_TRACE_LENGTH_PROTOCOL = 1u << 13,
SW_TRACE_CLOSE = 1u << 14,
SW_TRACE_WEBSOCEKT = 1u << 15,
/**
* Client
*/
SW_TRACE_REDIS_CLIENT = 1u << 16,
SW_TRACE_MYSQL_CLIENT = 1u << 17,
SW_TRACE_HTTP_CLIENT = 1u << 18,
SW_TRACE_AIO = 1u << 19,
SW_TRACE_SSL = 1u << 20,
SW_TRACE_NORMAL = 1u << 21,
/**
* Coroutine
*/
SW_TRACE_CHANNEL = 1u << 22,
SW_TRACE_TIMER = 1u << 23,
SW_TRACE_SOCKET = 1u << 24,
SW_TRACE_COROUTINE = 1u << 25,
SW_TRACE_CONTEXT = 1u << 26,
SW_TRACE_CO_HTTP_SERVER = 1u << 27,
SW_TRACE_ALL = 0xffffffff
};
#ifdef SW_LOG_TRACE_OPEN
#define swTraceLog(what,str,...) \
if (SW_LOG_TRACE >= SwooleG.log_level && (what & SwooleG.trace_flags)) {\
size_t _sw_error_len = sw_snprintf(sw_error,SW_ERROR_MSG_SIZE,"%s(:%d): " str, __func__, __LINE__, ##__VA_ARGS__);\
SwooleG.write_log(SW_LOG_TRACE, sw_error, _sw_error_len);\
}
#else
#define swTraceLog(what,str,...)
#endif
#define swTrace(str,...) swTraceLog(SW_TRACE_NORMAL, str, ##__VA_ARGS__)
#define swYield() sched_yield() //or usleep(1)
#define SW_MAX_FDTYPE 32 //32 kinds of event
//------------------------------Base--------------------------------
#ifndef uchar
typedef unsigned char uchar;
#endif
#ifdef SW_USE_OPENSSL
#include <openssl/ssl.h>
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
#define SW_SUPPORT_DTLS
#endif
#endif
typedef void (*swDestructor)(void *data);
typedef void (*swCallback)(void *data);
typedef struct
{
uint32_t id;
uint32_t fd :24;
uint32_t reactor_id :8;
} swSession;
typedef struct _swString
{
size_t length;
size_t size;
off_t offset;
char *str;
} swString;
typedef struct
{
union
{
struct sockaddr ss;
struct sockaddr_in inet_v4;
struct sockaddr_in6 inet_v6;
struct sockaddr_un un;
} addr;
socklen_t len;
} swSocketAddress;
typedef struct _swSocket
{
int fd;
enum swFd_type fdtype;
enum swSocket_type socket_type;
int events;
uchar removed :1;
uchar nonblock :1;
uchar cloexec :1;
uchar direct_send :1;
#ifdef SW_USE_OPENSSL
uchar ssl_send :1;
uchar ssl_want_read :1;
uchar ssl_want_write :1;
uchar ssl_renegotiation :1;
uchar ssl_handshake_buffer_set :1;
uchar ssl_quiet_shutdown :1;
#ifdef SW_SUPPORT_DTLS
uchar dtls :1;
#endif
#endif
uchar dontwait :1;
uchar close_wait :1;
uchar send_wait :1;
uchar tcp_nopush :1;
uchar tcp_nodelay :1;
uchar skip_recv :1;
uchar recv_wait :1;
uchar event_hup :1;
/**
* memory buffer size;
*/
uint32_t buffer_size;
uint32_t chunk_size;
void *object;
#ifdef SW_USE_OPENSSL
SSL *ssl;
uint32_t ssl_state;
#endif
swSocketAddress info;
struct _swBuffer *out_buffer;
struct _swBuffer *in_buffer;
swString *recv_buffer;
#ifdef SW_DEBUG
size_t total_recv_bytes;
size_t total_send_bytes;
#endif
} swSocket;
typedef struct _swTask_sendfile
{
char *filename;
uint16_t name_len;
int fd;
size_t length;
off_t offset;
} swTask_sendfile;
typedef struct _swConnection
{
/**
* file descript
*/
int fd;
/**
* session id
*/
uint32_t session_id;
/**
* socket type, SW_SOCK_TCP or SW_SOCK_UDP
*/
enum swSocket_type socket_type;
//--------------------------------------------------------------
/**
* is active
* system fd must be 0. en: signalfd, listen socket
*/
uint8_t active;
#ifdef SW_USE_OPENSSL
uint8_t ssl;
uint8_t ssl_ready;
#endif
//--------------------------------------------------------------
uint8_t overflow;
uint8_t high_watermark;
//--------------------------------------------------------------
uint8_t http_upgrade;
#ifdef SW_USE_HTTP2
uint8_t http2_stream;
#endif
#ifdef SW_HAVE_ZLIB
uint8_t websocket_compression;
#endif
//--------------------------------------------------------------
/**
* server is actively close the connection
*/
uint8_t close_actively;
uint8_t closed;
uint8_t close_queued;
uint8_t closing;
uint8_t close_reset;
uint8_t peer_closed;
/**
* protected connection, cannot be closed by heartbeat thread.
*/
uint8_t protect;
//--------------------------------------------------------------
uint8_t close_notify;
uint8_t close_force;
//--------------------------------------------------------------
/**
* ReactorThread id
*/
uint16_t reactor_id;
/**
* close error code
*/
uint16_t close_errno;
/**
* from which socket fd
*/
sw_atomic_t server_fd;
sw_atomic_t queued_bytes;
uint16_t waiting_time;
struct _swTimer_node *timer;
/**
* socket address
*/
swSocketAddress info;
/**
* link any thing, for kernel, do not use with application.
*/
void *object;
/**
* socket info
*/
swSocket *socket;
/**
* connect time(seconds)
*/
time_t connect_time;
/**
* received time with last data
*/
time_t last_time;
#ifdef SW_BUFFER_RECV_TIME
/**
* received time(microseconds) with last data
*/
double last_time_usec;
#endif
/**
* bind uid
*/
uint32_t uid;
/**
* upgarde websocket
*/
uint8_t websocket_status;
/**
* unfinished data frame
*/
swString *websocket_buffer;
#ifdef SW_USE_OPENSSL
swString *ssl_client_cert;
uint16_t ssl_client_cert_pid;
#endif
sw_atomic_t lock;
} swConnection;
typedef struct _swProtocol
{
/* one package: eof check */
uint8_t split_by_eof;
uint8_t package_eof_len;
char package_eof[SW_DATA_EOF_MAXLEN];
char package_length_type;
uint8_t package_length_size;
uint16_t package_length_offset;
uint16_t package_body_offset;
uint32_t package_max_length;
void *private_data;
void *private_data_2;
uint16_t real_header_length;
uint16_t ext_flags;
int (*onPackage)(struct _swProtocol *, swSocket *, char *, uint32_t);
ssize_t (*get_package_length)(struct _swProtocol *, swSocket *, char *, uint32_t);
uint8_t (*get_package_length_size)(swSocket *);
} swProtocol;
typedef ssize_t (*swProtocol_length_function)(struct _swProtocol *, swSocket *, char *, uint32_t);
//------------------------------String--------------------------------
#define swoole_tolower(c) (uchar) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
#define swoole_toupper(c) (uchar) ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)
uint32_t swoole_utf8_decode(uchar **p, size_t n);
size_t swoole_utf8_length(uchar *p, size_t n);
void swoole_random_string(char *buf, size_t size);
static sw_inline char *swoole_strlchr(char *p, char *last, char c)
{
while (p < last)
{
if (*p == c)
{
return p;
}
p++;
}
return NULL;
}
static sw_inline size_t swoole_size_align(size_t size, int pagesize)
{
return size + (pagesize - (size % pagesize));
}
#define SW_STRINGL(s) s->str, s->length
#define SW_STRINGS(s) s->str, s->size
#define SW_STRINGCVL(s) s->str + s->offset, s->length - s->offset
swString *swString_new(size_t size);
swString *swString_dup(const char *src_str, size_t length);
swString *swString_dup2(swString *src);
int swString_repeat(swString *src, const char *data, size_t len, size_t n);
void swString_print(swString *str);
int swString_append(swString *str, swString *append_str);
int swString_append_ptr(swString *str, const char *append_str, size_t length);
int swString_write(swString *str, off_t offset, swString *write_str);
int swString_write_ptr(swString *str, off_t offset, char *write_str, size_t length);
int swString_extend(swString *str, size_t new_size);
char* swString_alloc(swString *str, size_t __size);
static sw_inline void swString_clear(swString *str)
{
str->length = 0;
str->offset = 0;
}
static sw_inline void swString_free(swString *str)
{
sw_free(str->str);
sw_free(str);
}
static sw_inline int swString_extend_align(swString *str, size_t _new_size)
{
size_t align_size = SW_MEM_ALIGNED_SIZE(str->size * 2);
while (align_size < _new_size)
{
align_size *= 2;
}
return swString_extend(str, align_size);
}
static sw_inline int swString_grow(swString *str, size_t incr_value)
{
str->length += incr_value;
if (str->length == str->size && swString_extend(str, str->size * 2) < 0)
{
return SW_ERR;
}
else
{
return SW_OK;
}
}
/**
* migrate data to head, [offset, length - offset] -> [0, length - offset]
*/
static sw_inline void swString_pop_front(swString *str, off_t offset)
{
assert(offset >= 0 && (size_t ) offset <= str->length);
if (sw_unlikely(offset == 0)) return;
str->length = str->length - offset;
str->offset = 0;
if (str->length == 0) return;
memmove(str->str, str->str + offset, str->length);
}
static sw_inline void swString_sub(swString *str, off_t start, size_t length)
{
char *from = str->str + start + (start >= 0 ? 0 : str->length);
str->length = length != 0 ? length : str->length - start;
str->offset = 0;
if (sw_likely(str->length > 0))
{
memmove(str->str, from, str->length);
}
}
//------------------------------Base--------------------------------
enum _swEventData_flag
{
SW_EVENT_DATA_NORMAL,
SW_EVENT_DATA_PTR = 1u << 1,
SW_EVENT_DATA_CHUNK = 1u << 2,
SW_EVENT_DATA_END = 1u << 3,
SW_EVENT_DATA_OBJ_PTR = 1u << 4,
};
typedef struct _swDataHead
{
int fd;
uint32_t len;
int16_t reactor_id;
uint8_t type;
uint8_t flags;
uint16_t server_fd;
uint16_t ext_flags;
#ifdef SW_BUFFER_RECV_TIME
double time;
#endif
} swDataHead;
void swDataHead_dump(const swDataHead *data);
#define swTask_type(task) ((task)->info.server_fd)
/**
* use swDataHead->server_fd, 1 byte 8 bit
*/
enum swTask_type
{
SW_TASK_TMPFILE = 1, //tmp file
SW_TASK_SERIALIZE = 2, //php serialize
SW_TASK_NONBLOCK = 4, //task
SW_TASK_CALLBACK = 8, //callback
SW_TASK_WAITALL = 16, //for taskWaitAll
SW_TASK_COROUTINE = 32, //coroutine
SW_TASK_PEEK = 64, //peek
SW_TASK_NOREPLY = 128, //don't reply
};
typedef struct _swEvent
{
int fd;
int16_t reactor_id;
enum swFd_type type;
swSocket *socket;
} swEvent;
typedef struct
{
swDataHead info;
char data[SW_IPC_BUFFER_SIZE];
} swEventData;