-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathswoole.h
2280 lines (1957 loc) · 57 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]> |
+----------------------------------------------------------------------+
*/
#ifndef SWOOLE_H_
#define SWOOLE_H_
#if defined(HAVE_CONFIG_H) && !defined(COMPILE_DL_SWOOLE)
#include "config.h"
#endif
#ifdef SW_STATIC_COMPILATION
#include "php_config.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
/*--- C standard library ---*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stddef.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __sun
#include <strings.h>
#endif
#include <signal.h>
#include <time.h>
#include <limits.h>
#ifdef _WIN32
#include "win.h"
#else
#include "unix.h"
#endif
#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
#if __APPLE__
#define daemon(nochdir, noclose) swoole_daemon(nochdir, noclose)
#endif
/*----------------------------------------------------------------------------*/
#define SWOOLE_MAJOR_VERSION 4
#define SWOOLE_MINOR_VERSION 2
#define SWOOLE_RELEASE_VERSION 10
#define SWOOLE_EXTRA_VERSION "beta"
#define SWOOLE_VERSION "4.2.10-beta"
#define SWOOLE_VERSION_ID 40210
#define SWOOLE_BUG_REPORT \
"A bug occurred in Swoole-v" SWOOLE_VERSION ", please report it.\n"\
"The Swoole developers probably don't know about it,\n"\
"and unless you report it, chances are it won't be fixed.\n"\
"You can read How to report a bug doc before submitting any bug reports:\n"\
">> https://github.com/swoole/swoole-src/issues/2000\n"\
"Please do not send bug reports in the mailing list or personal letters.\n"\
"The issue page is also suitable to submit feature requests.\n"
/*----------------------------------------------------------------------------*/
#ifndef ulong
#define ulong unsigned long
#endif
typedef unsigned long ulong_t;
#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
#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
#ifndef likely
#define likely(x) __builtin_expect(!!(x), 1)
#endif
#ifndef unlikely
#define unlikely(x) __builtin_expect(!!(x), 0)
#endif
#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 "atomic.h"
#include "hashmap.h"
#include "list.h"
#include "heap.h"
#include "ring_queue.h"
#include "array.h"
#include "error.h"
#define SW_MAX_UINT UINT_MAX
#define SW_MAX_INT INT_MAX
#ifndef MAX
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#endif
#ifndef MIN
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#endif
#ifdef SW_DEBUG
#define SW_ASSERT(e) assert(e)
#else
#define SW_ASSERT(e)
#endif
#define SW_STRS(s) s, sizeof(s)
#define SW_STRL(s) s, sizeof(s)-1
#define SW_START_SLEEP usleep(100000) //sleep 1s,wait fork and pthread_create
#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
static sw_inline char* swoole_strdup(const char *s)
{
size_t l = strlen(s) + 1;
char *p = (char *)sw_malloc(l);
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);
strncpy(p, s, n);
p[n] = '\0';
return p;
}
#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 METHOD_DEF(class,name,...) class##_##name(class *object, ##__VA_ARGS__)
#define METHOD(class,name,...) class##_##name(object, ##__VA_ARGS__)
//-------------------------------------------------------------------------------
#define SW_OK 0
#define SW_ERR -1
#define SW_AGAIN -2
#define SW_BUSY -3
#define SW_DONE -4
#define SW_DECLINED -5
#define SW_ABORT -6
//-------------------------------------------------------------------------------
enum swReturnType
{
SW_CONTINUE = 1,
SW_WAIT = 2,
SW_CLOSE = 3,
SW_ERROR = 4,
SW_READY = 5,
};
//-------------------------------------------------------------------------------
enum swFd_type
{
SW_FD_TCP = 0, //tcp socket
SW_FD_LISTEN = 1, //server socket
SW_FD_CLOSE = 2, //socket closed
SW_FD_ERROR = 3, //socket error
SW_FD_UDP = 4, //udp socket
SW_FD_PIPE = 5, //pipe
SW_FD_STREAM = 6, //stream socket
SW_FD_WRITE = 7, //fd can write
SW_FD_TIMER = 8, //timer fd
SW_FD_AIO = 9, //linux native aio
SW_FD_CORO_SOCKET = 10, //CoroSocket
SW_FD_SIGNAL = 11, //signalfd
SW_FD_DNS_RESOLVER = 12, //dns resolver
SW_FD_INOTIFY = 13, //server socket
SW_FD_CHAN_PIPE = 14, //channel pipe
SW_FD_USER = 15, //SW_FD_USER or SW_FD_USER+n: for custom event
SW_FD_STREAM_CLIENT = 16, //swClient stream
SW_FD_DGRAM_CLIENT = 17, //swClient dgram
};
enum swBool_type
{
SW_TRUE = 1,
SW_FALSE = 0,
};
enum swEvent_type
{
SW_EVENT_DEAULT = 256,
SW_EVENT_READ = 1u << 9,
SW_EVENT_WRITE = 1u << 10,
SW_EVENT_ERROR = 1u << 11,
SW_EVENT_ONCE = 1u << 12,
};
enum swPipe_type
{
SW_PIPE_READ = 0,
SW_PIPE_WRITE = 1,
};
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,
};
//-------------------------------------------------------------------------------
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_DGRAM = 5, //unix sock dgram
SW_SOCK_UNIX_STREAM = 6, //unix sock stream
};
#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,
};
//-------------------------------------------------------------------------------
enum swFactory_dispatch_mode
{
SW_DISPATCH_ROUND = 1,
SW_DISPATCH_FDMOD = 2,
SW_DISPATCH_QUEUE = 3,
SW_DISPATCH_IPMOD = 4,
SW_DISPATCH_UIDMOD = 5,
SW_DISPATCH_USERFUNC = 6,
SW_DISPATCH_STREAM = 7,
};
enum swWorker_status
{
SW_WORKER_BUSY = 1,
SW_WORKER_IDLE = 2,
SW_WORKER_DEL = 3,
};
//-------------------------------------------------------------------------------
#define swWarn(str,...) if (SW_LOG_WARNING >= SwooleG.log_level){\
SwooleGS->lock_2.lock(&SwooleGS->lock_2);\
snprintf(sw_error,SW_ERROR_MSG_SIZE,"%s: " str,__func__,##__VA_ARGS__);\
swLog_put(SW_LOG_WARNING, sw_error);\
SwooleGS->lock_2.unlock(&SwooleGS->lock_2);}
#define swNotice(str,...) if (SW_LOG_NOTICE >= SwooleG.log_level){\
SwooleGS->lock_2.lock(&SwooleGS->lock_2);\
snprintf(sw_error,SW_ERROR_MSG_SIZE,str,##__VA_ARGS__);\
swLog_put(SW_LOG_NOTICE, sw_error);\
SwooleGS->lock_2.unlock(&SwooleGS->lock_2);}
#define swError(str,...) SwooleGS->lock_2.lock(&SwooleGS->lock_2);\
snprintf(sw_error, SW_ERROR_MSG_SIZE, str, ##__VA_ARGS__);\
swLog_put(SW_LOG_ERROR, sw_error);\
SwooleGS->lock_2.unlock(&SwooleGS->lock_2);\
exit(1)
#define swSysError(str,...) SwooleGS->lock_2.lock(&SwooleGS->lock_2);\
snprintf(sw_error,SW_ERROR_MSG_SIZE,"%s(:%d): " str " Error: %s[%d].",__func__,__LINE__,##__VA_ARGS__,strerror(errno),errno);\
swLog_put(SW_LOG_ERROR, sw_error);\
SwooleG.error=errno;\
SwooleGS->lock_2.unlock(&SwooleGS->lock_2)
#define swoole_error_log(level, __errno, str, ...) do{SwooleG.error=__errno;\
if (level >= SwooleG.log_level){\
snprintf(sw_error, SW_ERROR_MSG_SIZE, "%s (ERROR %d): " str,__func__,__errno,##__VA_ARGS__);\
SwooleGS->lock_2.lock(&SwooleGS->lock_2);\
swLog_put(level, sw_error);\
SwooleGS->lock_2.unlock(&SwooleGS->lock_2);}}while(0)
#ifdef SW_DEBUG
#define swDebug(str,...) if (SW_LOG_DEBUG >= SwooleG.log_level){\
SwooleGS->lock_2.lock(&SwooleGS->lock_2);\
snprintf(sw_error, SW_ERROR_MSG_SIZE, "%s(:%d): " str, __func__, __LINE__, ##__VA_ARGS__);\
swLog_put(SW_LOG_DEBUG, sw_error);\
SwooleGS->lock_2.unlock(&SwooleGS->lock_2);}
#else
#define swDebug(str,...)
#endif
enum swTraceType
{
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_HTTP2 = 1u << 10,
SW_TRACE_EOF_PROTOCOL = 1u << 11,
SW_TRACE_LENGTH_PROTOCOL = 1u << 12,
SW_TRACE_CLOSE = 1u << 13,
SW_TRACE_HTTP_CLIENT = 1u << 14,
SW_TRACE_COROUTINE = 1u << 15,
SW_TRACE_REDIS_CLIENT = 1u << 16,
SW_TRACE_MYSQL_CLIENT = 1u << 17,
SW_TRACE_AIO = 1u << 18,
SW_TRACE_SSL = 1u << 19,
SW_TRACE_NORMAL = 1u << 20,
SW_TRACE_CHANNEL = 1u << 21,
SW_TRACE_TIMER = 1u << 22,
};
#ifdef SW_LOG_TRACE_OPEN
#define swTraceLog(what,str,...) if (SW_LOG_TRACE >= SwooleG.log_level && (what & SwooleG.trace_flags)) {\
SwooleGS->lock_2.lock(&SwooleGS->lock_2);\
snprintf(sw_error,SW_ERROR_MSG_SIZE,"%s(:%d): " str, __func__, __LINE__, ##__VA_ARGS__);\
swLog_put(SW_LOG_TRACE, sw_error);\
SwooleGS->lock_2.unlock(&SwooleGS->lock_2);}
#else
#define swTraceLog(id,str,...)
#endif
#define swTrace(str,...) swTraceLog(SW_TRACE_NORMAL, str, ##__VA_ARGS__)
#define swYield() sched_yield() //or usleep(1)
//#define swYield() usleep(500000)
#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>
#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 void* swObject;
typedef struct _swLinkedList_node
{
struct _swLinkedList_node *prev;
struct _swLinkedList_node *next;
ulong_t priority;
void *data;
} swLinkedList_node;
typedef struct
{
uint32_t num;
uint8_t type;
swLinkedList_node *head;
swLinkedList_node *tail;
swDestructor dtor;
} swLinkedList;
typedef struct
{
union
{
struct sockaddr_in inet_v4;
struct sockaddr_in6 inet_v6;
#ifndef _WIN32
struct sockaddr_un un;
#endif
} addr;
socklen_t len;
} swSocketAddress;
typedef struct _swConnection
{
/**
* file descript
*/
int fd;
/**
* session id
*/
uint32_t session_id;
/**
* socket type, SW_SOCK_TCP or SW_SOCK_UDP
*/
uint16_t socket_type;
/**
* fd type, SW_FD_TCP or SW_FD_PIPE
*/
uint16_t fdtype;
int events;
//--------------------------------------------------------------
/**
* is active
* system fd must be 0. en: signalfd, listen socket
*/
uint8_t active;
uint8_t connect_notify;
uint8_t direct_send;
uint8_t ssl_send;
//--------------------------------------------------------------
uint8_t listen_wait;
uint8_t recv_wait;
uint8_t send_wait;
uint8_t close_wait;
uint8_t overflow;
uint8_t high_watermark;
uint8_t removed;
uint8_t tcp_nopush;
uint8_t dontwait;
//--------------------------------------------------------------
uint8_t tcp_nodelay;
uint8_t ssl_want_read;
uint8_t ssl_want_write;
uint8_t http_upgrade;
uint8_t http2_stream;
uint8_t skip_recv;
//--------------------------------------------------------------
/**
* server is actively close the connection
*/
uint8_t close_actively;
uint8_t closed;
uint8_t close_queued;
uint8_t closing;
uint8_t close_reset;
/**
* protected connection, cannot be closed by heartbeat thread.
*/
uint8_t protect;
uint8_t nonblock;
//--------------------------------------------------------------
uint8_t close_notify;
uint8_t close_force;
//--------------------------------------------------------------
/**
* ReactorThread id
*/
uint16_t from_id;
/**
* close error code
*/
uint16_t close_errno;
/**
* from which socket fd
*/
sw_atomic_t from_fd;
/**
* socket address
*/
swSocketAddress info;
/**
* link any thing, for kernel, do not use with application.
*/
void *object;
/**
* input buffer
*/
struct _swBuffer *in_buffer;
/**
* output buffer
*/
struct _swBuffer *out_buffer;
/**
* for receive data buffer
*/
swString *recv_buffer;
/**
* 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;
/**
* memory buffer size;
*/
int buffer_size;
/**
* upgarde websocket
*/
uint8_t websocket_status;
/**
* unfinished data frame
*/
swString *websocket_buffer;
#ifdef SW_USE_OPENSSL
SSL *ssl;
uint32_t ssl_state;
swString ssl_client_cert;
#endif
sw_atomic_t lock;
#ifdef SW_DEBUG
size_t total_recv_bytes;
size_t total_send_bytes;
#endif
} 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 + 1]; //数据缓存结束符
char package_length_type; //length field type
uint8_t package_length_size;
uint16_t package_length_offset; //第几个字节开始表示长度
uint16_t package_body_offset; //第几个字节开始计算长度
uint32_t package_max_length;
void *private_data;
uint16_t real_header_length;
int (*onPackage)(swConnection *conn, char *data, uint32_t length);
ssize_t (*get_package_length)(struct _swProtocol *protocol, swConnection *conn, char *data, uint32_t length);
uint8_t (*get_package_length_size)(swConnection *conn);
} swProtocol;
typedef ssize_t (*swProtocol_length_function)(struct _swProtocol *, swConnection *, char *, uint32_t);
//------------------------------String--------------------------------
#define swoole_tolower(c) (u_char) ((c >= 'A' && c <= 'Z') ? (c | 0x20) : c)
#define swoole_toupper(c) (u_char) ((c >= 'a' && c <= 'z') ? (c & ~0x20) : c)
uint32_t swoole_utf8_decode(u_char **p, size_t n);
size_t swoole_utf8_length(u_char *p, size_t n);
void swoole_random_string(char *buf, size_t size);
const char* swoole_get_mime_type(const char *file);
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));
}
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 size_t swString_length(swString *str)
{
return str->length;
}
static sw_inline size_t swString_size(swString *str)
{
return str->size;
}
swString *swString_new(size_t size);
swString *swString_dup(const char *src_str, int length);
swString *swString_dup2(swString *src);
void swString_print(swString *str);
void swString_free(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);
#define SWSTRING_CURRENT_VL(buffer) buffer->str + buffer->offset, buffer->length - buffer->offset
static sw_inline int swString_extend_align(swString *str, size_t _new_size)
{
size_t align_size = str->size * 2;
while (align_size < _new_size)
{
align_size *= 2;
}
return swString_extend(str, align_size);
}
#define swString_length(s) (s->length)
#define swString_ptr(s) (s->str)
//------------------------------Base--------------------------------
typedef struct _swDataHead
{
int fd;
uint16_t len;
int16_t from_id;
uint8_t type;
uint8_t flags;
uint16_t from_fd;
#ifdef SW_BUFFER_RECV_TIME
double time;
#endif
} swDataHead;
typedef struct _swEvent
{
int fd;
int16_t from_id;
uint8_t type;
swConnection *socket;
} swEvent;
typedef struct _swEventData
{
swDataHead info;
char data[SW_BUFFER_SIZE];
} swEventData;
typedef struct _swDgramPacket
{
union
{
struct in6_addr v6;
struct in_addr v4;
struct
{
uint16_t path_length;
} un;
} addr;
uint16_t port;
uint32_t length;
char data[0];
} swDgramPacket;
typedef struct _swSendData
{
swDataHead info;
/**
* for big package
*/
uint32_t length;
char *data;
} swSendData;
typedef struct
{
off_t offset;
size_t length;
char filename[0];
} swSendFile_request;
typedef void * (*swThreadStartFunc)(void *);
typedef int (*swHandle)(swEventData *buf);
typedef void (*swSignalHandler)(int);
typedef struct _swReactor swReactor;
typedef int (*swReactor_handle)(swReactor *reactor, swEvent *event);
//------------------Pipe--------------------
typedef struct _swPipe
{
void *object;
int blocking;
double timeout;
int (*read)(struct _swPipe *, void *recv, int length);
int (*write)(struct _swPipe *, void *send, int length);
int (*getFd)(struct _swPipe *, int master);
int (*close)(struct _swPipe *);
} swPipe;
enum _swPipe_close_which
{
SW_PIPE_CLOSE_MASTER = 1,
SW_PIPE_CLOSE_WORKER = 2,
SW_PIPE_CLOSE_READ = 3,
SW_PIPE_CLOSE_WRITE = 4,
SW_PIPE_CLOSE_BOTH = 0,
};
int swPipeBase_create(swPipe *p, int blocking);
int swPipeEventfd_create(swPipe *p, int blocking, int semaphore, int timeout);
int swPipeUnsock_create(swPipe *p, int blocking, int protocol);
int swPipeUnsock_close_ext(swPipe *p, int which);
static inline int swPipeNotify_auto(swPipe *p, int blocking, int semaphore)
{
#ifdef HAVE_EVENTFD
return swPipeEventfd_create(p, blocking, semaphore, 0);
#else
return swPipeBase_create(p, blocking);
#endif
}
void swBreakPoint(void);
//------------------Queue--------------------
#ifndef _WIN32
typedef struct _swQueue_Data
{
long mtype; /* type of received/sent message */
char mdata[sizeof(swEventData)]; /* text of the message */
} swQueue_data;
typedef struct _swMsgQueue
{
int blocking;
int msg_id;
int flags;
int perms;
} swMsgQueue;
int swMsgQueue_create(swMsgQueue *q, int blocking, key_t msg_key, int perms);
void swMsgQueue_set_blocking(swMsgQueue *q, uint8_t blocking);
int swMsgQueue_set_capacity(swMsgQueue *q, int queue_bytes);
int swMsgQueue_push(swMsgQueue *q, swQueue_data *in, int data_length);
int swMsgQueue_pop(swMsgQueue *q, swQueue_data *out, int buffer_length);
int swMsgQueue_stat(swMsgQueue *q, int *queue_num, int *queue_bytes);
int swMsgQueue_free(swMsgQueue *q);
#endif
//------------------Lock--------------------------------------
enum SW_LOCKS
{
SW_RWLOCK = 1,
#define SW_RWLOCK SW_RWLOCK
SW_FILELOCK = 2,
#define SW_FILELOCK SW_FILELOCK
SW_MUTEX = 3,
#define SW_MUTEX SW_MUTEX
SW_SEM = 4,
#define SW_SEM SW_SEM
SW_SPINLOCK = 5,
#define SW_SPINLOCK SW_SPINLOCK
SW_ATOMLOCK = 6,
#define SW_ATOMLOCK SW_ATOMLOCK
};
enum swDNSLookup_cache_type
{
SW_DNS_LOOKUP_RANDOM = (1u << 11),
};
typedef struct
{
char *hostname;
char *service;
int family;
int socktype;
int protocol;
int error;
void *result;
int count;
} swRequest_getaddrinfo;
typedef struct _swMutex
{
pthread_mutex_t _lock;
pthread_mutexattr_t attr;
} swMutex;
#ifndef _WIN32
typedef struct _swFileLock
{
struct flock lock_t;
int fd;
} swFileLock;
#ifdef HAVE_RWLOCK
typedef struct _swRWLock
{
pthread_rwlock_t _lock;
pthread_rwlockattr_t attr;
} swRWLock;
#endif
#ifdef HAVE_SPINLOCK
typedef struct _swSpinLock
{
pthread_spinlock_t lock_t;
} swSpinLock;
#endif
typedef struct _swAtomicLock
{
sw_atomic_t lock_t;
uint32_t spin;
} swAtomicLock;
typedef struct _swSem
{
key_t key;
int semid;
} swSem;
#endif
typedef struct _swLock
{
int type;
union
{
swMutex mutex;
#ifndef _WIN32
#ifdef HAVE_RWLOCK
swRWLock rwlock;
#endif
#ifdef HAVE_SPINLOCK
swSpinLock spinlock;
#endif
swFileLock filelock;
swSem sem;
swAtomicLock atomlock;
#endif
} object;
int (*lock_rd)(struct _swLock *);
int (*lock)(struct _swLock *);
int (*unlock)(struct _swLock *);
int (*trylock_rd)(struct _swLock *);
int (*trylock)(struct _swLock *);
int (*free)(struct _swLock *);
} swLock;
//Thread Condition
typedef struct _swCond
{
swLock _lock;
pthread_cond_t _cond;
int (*wait)(struct _swCond *object);
int (*timewait)(struct _swCond *object, long, long);
int (*notify)(struct _swCond *object);