-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngx_event.php
1290 lines (1012 loc) · 32.8 KB
/
ngx_event.php
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
<?php
/**
* Created by PhpStorm.
* User: zenus
* Date: 15-11-7
* Time: 下午7:34
*/
/*
* The event filter requires to read/write the whole data:
* select, poll, /dev/poll, kqueue, epoll.
*/
define('NGX_USE_LEVEL_EVENT',0x00000001);
/*
* The event filter is deleted after a notification without an additional
* syscall: kqueue, epoll.
*/
define('NGX_USE_ONESHOT_EVENT',0x00000002);
/*
* The event filter notifies only the changes and an initial level:
* kqueue, epoll.
*/
define('NGX_USE_CLEAR_EVENT',0x00000004);
/*
* The event filter has kqueue features: the eof flag, errno,
* available data, etc.
*/
define('NGX_USE_KQUEUE_EVENT',0x00000008);
/*
* The event filter supports low water mark: kqueue's NOTE_LOWAT.
* kqueue in FreeBSD 4.1-4.2 has no NOTE_LOWAT so we need a separate flag.
*/
define('NGX_USE_LOWAT_EVENT',0x00000010);
/*
* The event filter requires to do i/o operation until EAGAIN: epoll.
*/
define('NGX_USE_GREEDY_EVENT',0x00000020);
/*
* The event filter is epoll.
*/
define('NGX_USE_EPOLL_EVENT',0x00000040);
/*
* Obsolete.
*/
define('NGX_USE_RTSIG_EVENT',0x00000080);
/*
* Obsolete.
*/
define('NGX_USE_AIO_EVENT',0x00000100);
/*
* Need to add socket or handle only once: i/o completion port.
*/
define('NGX_USE_IOCP_EVENT',0x00000200);
/*
* The event filter has no opaque data and requires file descriptors table:
* poll, /dev/poll.
*/
define('NGX_USE_FD_EVENT',0x00000400);
/*
* The event module handles periodic or absolute timer event by itself:
* kqueue in FreeBSD 4.4, NetBSD 2.0, and MacOSX 10.4, Solaris 10's event ports.
*/
define('NGX_USE_TIMER_EVENT',0x00000800);
/*
* All event filters on file descriptor are deleted after a notification:
* Solaris 10's event ports.
*/
define('NGX_USE_EVENTPORT_EVENT',0x00001000);
/*
* The event filter support vnode notifications: kqueue.
*/
define('NGX_USE_VNODE_EVENT',0x00002000);
define('NGX_EVENT_MODULE',0x544E5645);
define('NGX_EVENT_CONF',0x02000000);
define('NGX_READ_EVENT', EV_READ);
define('NGX_WRITE_EVENT',EV_WRITE);
define('NGX_CLOSE_EVENT',1);
define('DEFAULT_CONNECTIONS', 512);
define('NGX_UPDATE_TIME',1);
define('NGX_POST_EVENTS',2);
define('NGX_DISABLE_EVENT',2);
define('NGX_INVALID_INDEX',0xd0d0d0d0);
function ngx_event_flags($i = null){
static $ngx_event_flags = null;
if(!is_null($i)){
$ngx_event_flags = $i;
}else{
return $ngx_event_flags;
}
}
function ngx_event_ident(ngx_connection_t $p) {
return $p->fd;
}
//ngx_atomic_t *ngx_accept_mutex_ptr;
//ngx_shmtx_t ngx_accept_mutex;
function ngx_use_accept_mutex($i = null){
static $ngx_use_accept_mutex = null;
if(!is_null($i)){
$ngx_use_accept_mutex = $i ;
}else{
return $ngx_use_accept_mutex;
}
}
function ngx_accept_events($i = null){
static $ngx_accept_events = null;
if(!is_null($i)){
$ngx_accept_events = $i ;
}else{
return $ngx_accept_events;
}
}
function ngx_accept_mutex_held($i = null){
static $ngx_accept_mutex_held = null;
if(!is_null($i)){
$ngx_accept_mutex_held = $i ;
}else{
return $ngx_accept_mutex_held;
}
}
function ngx_accept_mutex_delay($i = null){
static $ngx_accept_mutex_delay = null;
if(!is_null($i)){
$ngx_accept_mutex_delay = $i ;
}else{
return $ngx_accept_mutex_delay;
}
}
function ngx_accept_disabled($i = null){
static $ngx_accept_disabled = null;
if(!is_null($i)){
$ngx_accept_disabled = $i ;
}else{
return $ngx_accept_disabled;
}
}
function ngx_event_timer_alarm($i = null){
static $ngx_event_timer_alarm = null;
if(!is_null($i)){
$ngx_event_timer_alarm = $i;
}else{
return $ngx_event_timer_alarm;
}
}
function ngx_event_actions(ngx_event_actions_t $action){
static $ngx_event_actions = null;
if(!is_null($action)){
$ngx_event_actions = $action;
}else{
return $ngx_event_actions;
}
}
class ngx_event_module_t {
public $name;
//void *(*create_conf)(ngx_cycle_t *cycle);
public $create_conf;
//char *(*init_conf)(ngx_cycle_t *cycle, void *conf);
public $init_conf;
// // ngx_int_t (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
public /*ngx_event_actions_t*/ $actions;
// public $add;
// //ngx_int_t (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
// public $del;
//
// //ngx_int_t (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
// public $enable;
// //ngx_int_t (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);
// public $disable;
//
// //ngx_int_t (*add_conn)(ngx_connection_t *c);
// public $add_conn;
// //ngx_int_t (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);
// public $del_conn;
//
// //ngx_int_t (*notify)(ngx_event_handler_pt handler);
// public $notify;
//
//// ngx_int_t (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,
// // ngx_uint_t flags);
// public $process_events;
//
// //ngx_int_t (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);
// public $init;
// //void (*done)(ngx_cycle_t *cycle);
// public $done;
}
class ngx_event_t {
/** void **/ /****ngx_connection_t***/ public $data;
/** unsigned **/ public $write;
/** unsigned **/ public $accept;
/***** instead of epoll , i use libevent******/
/** unsigned **/ public $libevent;
/* used to detect the stale events in kqueue and epoll */
/** unsigned **/ public $instance;
/*
* the event was passed or would be passed to a kernel;
* in aio mode - operation was posted.
*/
/** unsigned **/ public $active;
/** unsigned **/ private $disabled;
/* the ready event; in aio mode 0 means that no operation can be posted */
/** unsigned **/ private $ready;
/** unsigned **/ private $oneshot;
/* aio operation is complete */
/** unsigned **/ private $complete;
/** unsigned **/ private $eof;
/** unsigned **/ private $error;
/** unsigned **/ private $timedout;
/** unsigned **/ private $timer_set;
/** unsigned **/ private $delayed;
/** unsigned **/ private $deferred_accept;
/* the pending eof reported by kqueue, epoll or in aio chain operation */
/** unsigned **/ private $pending_eof;
/** unsigned **/ private $posted;
/** unsigned **/ private $closed;
/* to test on worker exit */
/** unsigned **/ private $channel;
/** unsigned **/ private $resolver;
/** unsigned **/ private $cancelable;
/** unsigned **/ private $available;
/** ngx_event_handler_pt **/ private $handler;
#if (NGX_HAVE_IOCP)
// ngx_event_ovlp_t ovlp;
#endif
/** ngx_uint_t **/ private $index;
/** ngx_log_t **/ private $log;
//todo php special use it as libevent event element
private $event;
/** ngx_rbtree_node_t **/ private $timer;
/* the posted queue */
/** ngx_queue_t **/ private $queue;
#if 0
/* the threads support */
/*
* the event thread context, we store it here
* if $(CC) does not understand __thread declaration
* and pthread_getspecific() is too costly
*/
/** void **/ private $thr_ctx;
public function __set($property,$value){
$this->$property = $value;
}
public function __get($property){
return $this->$property;
}
#if (NGX_EVENT_T_PADDING)
/* event should not cross cache line in SMP */
// private $padding[NGX_EVENT_T_PADDING];
#endif
#endif
}
function ngx_io(ngx_os_io_t $ngx_os_io_t = null)
{
static $ngx_io = null;
if ($ngx_io !== null) {
$ngx_io = $ngx_os_io_t;
} else {
return $ngx_io;
}
}
function ngx_add_timer(ngx_event_t $e, $time){
ngx_event_add_timer($e, $time);
}
function ngx_del_timer(ngx_event_t $ev){
ngx_event_del_timer($ev);
}
function ngx_event_del_timer(ngx_event_t $ev){
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, $ev->log, 0,
"event timer del: %d: %M",
ngx_event_ident($ev->data), $ev->timer);
event_del($ev->event);
event_free($ev->event);
$ev->timer_set = 0;
}
function ngx_accept_mutex_ptr($ptr = null){
static $ngx_accept_mutex_ptr = null;
if(!is_null($ptr)){
$ngx_accept_mutex_ptr = $ptr;
}else{
return $ngx_accept_mutex_ptr;
}
}
//ngx_atomic_t *ngx_accept_mutex_ptr;
//ngx_shmtx_t ngx_accept_mutex;
//ngx_uint_t ngx_use_accept_mutex;
//ngx_uint_t ngx_accept_events;
//ngx_uint_t ngx_accept_mutex_held;
//ngx_msec_t ngx_accept_mutex_delay;
function ngx_timer_resolution($i = null){
static $ngx_timer_resolution = null;
if(!is_null($i)){
$ngx_timer_resolution = $i;
}else{
return $ngx_timer_resolution;
}
}
function ngx_accept_mutex($shmtx = null){
static $ngx_accept_mutex = null;
if(!is_null($shmtx)){
$ngx_accept_mutex = $shmtx;
}else{
return $ngx_accept_mutex;
}
}
function ngx_process_events_and_timers(ngx_cycle_t $cycle)
{
//ngx_uint_t flags;
// ngx_msec_t timer, delta;
if (ngx_timer_resolution()) {
$timer = NGX_TIMER_INFINITE;
$flags = 0;
} else {
//todo should complete event method
$timer = ngx_event_find_timer();
$flags = NGX_UPDATE_TIME;
}
if (ngx_use_accept_mutex()) {
if ($ngx_accept_disabled = ngx_accept_disabled() > 0) {
$ngx_accept_disabled--;
ngx_accept_disabled($ngx_accept_disabled);
} else {
//todo should complete lock method
if (ngx_trylock_accept_mutex($cycle) == NGX_ERROR) {
return;
}
if (ngx_accept_mutex_held()) {
$flags |= NGX_POST_EVENTS;
} else {
if ($timer == NGX_TIMER_INFINITE
|| ($timer > ($ngx_accept_mutex_delay = ngx_accept_mutex_delay())))
{
$timer = ngx_accept_mutex_delay();
}
}
}
}
$delta = ngx_current_msec();
//todo should complete event method
ngx_process_events($cycle, $timer, $flags);
$delta = ngx_current_msec() - $delta;
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, $cycle->log, 0,
"timer delta: %M", $delta);
ngx_event_process_posted($cycle, ngx_posted_accept_events());
if (ngx_accept_mutex_held()) {
//todo should complete lock method
ngx_shmtx_unlock(ngx_accept_mutex());
}
if ($delta) {
ngx_event_expire_timers();
}
ngx_event_process_posted($cycle, ngx_posted_events());
}
//todo need to complete event method
function ngx_add_event(ngx_event_t $ev, $event, $flags){
return NGX_OK;
}
//todo need to complete event method
function ngx_add_conn(ngx_connection_t $con){
return NGX_OK;
}
//todo need to complete event method
function ngx_del_conn(ngx_connection_t $con, $flags){
return NGX_OK;
}
//todo need to complete event method
function ngx_process_events(ngx_cycle_t $cycle, $timer, $flags){
}
//todo need to complete event method
function ngx_del_event(ngx_event_t $ev, $event, $flags)
{
// int op;
// uint32_t prev;
// ngx_event_t *e;
// ngx_connection_t *c;
// struct epoll_event ee;
/*
* when the file descriptor is closed, the epoll automatically deletes
* it from its queue, so we do not need to delete explicitly the event
* before the closing the file descriptor
*/
if ($flags & NGX_CLOSE_EVENT) {
$ev->active = 0;
return NGX_OK;
}
$c = $ev->data;
if ($event == NGX_READ_EVENT) {
$e = $c->write;
$prev = NGX_WRITE_EVENT;
} else {
$e = $c->read;
$prev = NGX_READ_EVENT;
}
// if ($e->active) {
// op = EPOLL_CTL_MOD;
// ee.events = prev | (uint32_t) flags;
// ee.data.ptr = (void *) ((uintptr_t) c | ev->instance);
//
// } else {
// op = EPOLL_CTL_DEL;
// ee.events = 0;
// ee.data.ptr = NULL;
//}
//
// ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0,
// "epoll del event: fd:%d op:%d ev:%08XD",
// c->fd, op, ee.events);
//
// if (epoll_ctl(ep, op, c->fd, &ee) == -1) {
// ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno,
// "epoll_ctl(%d, %d) failed", op, c->fd);
// return NGX_ERROR;
// }
//
// ev->active = 0;
return NGX_OK;
}
//ngx_int_t ngx_accept_disabled;
function ngx_events_module(){
static $ngx_events_module;
if(is_null($ngx_events_module)){
$obj = new ngx_module_t();
$ngx_events_module = $obj;
$ngx_events_module->version = 1;
$ngx_events_module->ctx = ngx_events_module_ctx();
$ngx_events_module->commands = ngx_events_commands();
$ngx_events_module->type = NGX_CORE_MODULE;
}
return $ngx_events_module;
}
function ngx_events_module_ctx(){
static $ngx_events_module_ctx;
if(is_null($ngx_events_module_ctx)){
$obj= new ngx_core_module_t();
$ngx_events_module_ctx = $obj;
$ngx_events_module_ctx->name = 'events';
$ngx_events_module_ctx->create_conf = null;
$ngx_events_module_ctx->init_conf = 'ngx_event_init_conf';
}
return $ngx_events_module_ctx;
}
function ngx_events_commands(){
$ngx_events_commands = array(
array(
'name'=>"events",
'type'=>NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
'set'=>'ngx_events_block',
'conf'=>0,
'offset'=>0,
'post'=>NULL
),
array(
'name'=>'',
'type'=>0,
'set'=>NULL,
'conf'=>0,
'offset'=>0,
'post'=>NULL
),
);
return $ngx_events_commands;
}
function ngx_event_core_module(){
static $ngx_event_core_module;
if(is_null($ngx_event_core_module)){
$obj = new ngx_module_t();
$ngx_event_core_module = $obj;
$ngx_event_core_module->version = 1;
$ngx_event_core_module->ctx = ngx_event_core_module_ctx();
$ngx_event_core_module->commands = ngx_event_core_commands();
$ngx_event_core_module->type = NGX_EVENT_MODULE;
$ngx_event_core_module->init_module = 'ngx_event_module_init';
$ngx_event_core_module->init_process = 'ngx_event_process_init';
}
return $ngx_event_core_module;
}
function ngx_event_core_module_ctx(){
static $ngx_events_module_ctx;
if(is_null($ngx_events_module_ctx)){
$obj= new ngx_event_module_t();
$ngx_events_module_ctx = $obj;
$ngx_events_module_ctx->name = 'event_core';
$ngx_events_module_ctx->create_conf = 'ngx_event_core_create_conf';
$ngx_events_module_ctx->init_conf = 'ngx_event_core_init_conf';
}
return $ngx_events_module_ctx;
}
function ngx_event_core_commands(){
$ngx_event_core_commands = array(
array(
'name'=>"work_connections",
'type'=>NGX_EVENT_CONF|NGX_CONF_TAKE1,
'set'=>'ngx_event_connections',
'conf'=>0,
'offset'=>0,
'post'=>NULL
),
array(
'name'=>"use",
'type'=>NGX_EVENT_CONF|NGX_CONF_TAKE1,
'set'=>'ngx_event_use',
'conf'=>0,
'offset'=>0,
'post'=>NULL
),
array(
'name'=>"multi_accept",
'type'=>NGX_EVENT_CONF|NGX_CONF_FLAG,
'set'=>'ngx_conf_set_flag_slot',
'conf'=>0,
'offset'=>0,
'post'=>NULL
),
array(
'name'=>"multi_mutex_delay",
'type'=>NGX_EVENT_CONF|NGX_CONF_TAKE1,
'set'=>'ngx_conf_set_msec_slot',
'conf'=>0,
'offset'=>0,
'post'=>NULL
),
array(
'name'=>'',
'type'=>0,
'set'=>NULL,
'conf'=>0,
'offset'=>0,
'post'=>NULL
),
);
return $ngx_event_core_commands;
}
function ngx_event_max_module($i = null){
static $ngx_event_max_module = null;
if(!is_null($i)){
$ngx_event_max_module = $i;
}else{
return $ngx_event_max_module;
}
}
function ngx_events_block(ngx_conf_t $cf, /*ngx_command_t*/ $cmd, $conf)
{
//char *rv;
// void ***ctx;
// ngx_uint_t i;
// ngx_conf_t pcf;
// ngx_event_module_t *m;
if ($conf) {
return "is duplicate";
}
/* count the number of the event modules and set up their indices */
ngx_event_max_module(0);
for ($i = 0; ngx_modules($i); $i++) {
if (ngx_modules($i)->type != NGX_EVENT_MODULE) {
continue;
}
$max = ngx_event_max_module();
ngx_modules($i)->ctx_index = $max++;
ngx_event_max_module($max);
}
// ctx = ngx_pcalloc(cf->pool, sizeof(void *));
// if (ctx == NULL) {
// return NGX_CONF_ERROR;
// }
//
// *ctx = ngx_pcalloc(cf->pool, ngx_event_max_module * sizeof(void *));
// if (*ctx == NULL) {
// return NGX_CONF_ERROR;
//}
$ctx = array();
$conf = $ctx;
for ($i = 0; ngx_modules($i); $i++) {
if (ngx_modules($i)->type != NGX_EVENT_MODULE) {
continue;
}
$m = ngx_modules($i)->ctx;
if ($m->create_conf) {
$ctx[ngx_modules($i)->ctx_index] = $m->create_conf($cf->cycle);
if ($ctx[ngx_modules($i)->ctx_index] == NULL) {
return NGX_CONF_ERROR;
}
}
}
$pcf = $cf;
$cf->ctx = $ctx;
$cf->module_type = NGX_EVENT_MODULE;
$cf->cmd_type = NGX_EVENT_CONF;
$rv = ngx_conf_parse($cf, NULL);
$cf = $pcf;
if ($rv != NGX_CONF_OK) {
return $rv;
}
for ($i = 0; ngx_modules($i); $i++) {
if (ngx_modules($i)->type != NGX_EVENT_MODULE) {
continue;
}
$m = ngx_modules($i)->ctx;
if ($m->init_conf) {
$rv = $m->init_conf($cf->cycle, $ctx[ngx_modules($i)->ctx_index]);
if ($rv != NGX_CONF_OK) {
return $rv;
}
}
}
return NGX_CONF_OK;
}
function ngx_event_init_conf(ngx_cycle_t $cycle, $conf)
{
if (ngx_get_conf($cycle->conf_ctx, ngx_events_module()) == NULL) {
ngx_log_error(NGX_LOG_EMERG, $cycle->log, 0,
"no \"events\" section in configuration");
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
class ngx_event_conf_t {
private $connections;
private $use;
private $multi_accept;
private $accept_mutex;
private $accept_mutex_delay;
private $name;
public function __set($name,$value){
$this->$name = $value;
}
public function __get($name){
return $this->$name;
}
}
function ngx_event_core_create_conf(ngx_cycle_t $cycle)
{
//ngx_event_conf_t *ecf;
$ecf = new ngx_event_conf_t();
$ecf->connections = NGX_CONF_UNSET_UINT;
$ecf->use = NGX_CONF_UNSET_UINT;
$ecf->multi_accept = NGX_CONF_UNSET;
$ecf->accept_mutex = NGX_CONF_UNSET;
$ecf->accept_mutex_delay = NGX_CONF_UNSET_MSEC;
$ecf->name = NGX_CONF_UNSET;
return $ecf;
}
function ngx_event_core_init_conf(ngx_cycle_t $cycle, $conf)
{
//ngx_event_conf_t *ecf = conf;
$ecf = $conf;
// int fd;
// ngx_int_t i;
// ngx_module_t *module;
// ngx_event_module_t *event_module;
$module = NULL;
// fd = epoll_create(100);
//
// if (fd != -1) {
// (void) close(fd);
// module = &ngx_epoll_module;
//
// } else if (ngx_errno != NGX_ENOSYS) {
// module = &ngx_epoll_module;
// }
//todo should find the best event method
//$module = ngx_epoll_module();
if ($module == NULL) {
for ($i = 0; ngx_modules($i); $i++) {
if (ngx_modules($i)->type != NGX_EVENT_MODULE) {
continue;
}
$event_module = ngx_modules($i)->ctx;
if (ngx_strcmp($event_module->name, 'event_core') == 0)
{
continue;
}
$module = ngx_modules($i);
break;
}
}
if ($module == NULL) {
ngx_log_error(NGX_LOG_EMERG, $cycle->log, 0, "no events module found");
return NGX_CONF_ERROR;
}
ngx_conf_init_uint_value($ecf->connections, DEFAULT_CONNECTIONS);
$cycle->connection_n = $ecf->connections;
ngx_conf_init_uint_value($ecf->use, $module->ctx_index);
$event_module = $module->ctx;
ngx_conf_init_ptr_value($ecf->name, $event_module->name);
ngx_conf_init_value($ecf->multi_accept, 0);
ngx_conf_init_value($ecf->accept_mutex, 1);
ngx_conf_init_msec_value($ecf->accept_mutex_delay, 500);
return NGX_CONF_OK;
}
function ngx_event_connections(ngx_conf_t $cf, /*ngx_command_t*/ $cmd, $conf)
{
$ecf = $conf;
// ngx_str_t *value;
if ($ecf->connections != NGX_CONF_UNSET_UINT) {
return "is duplicate";
}
$value = $cf->args;
$ecf->connections = ngx_atoi($value[1]);
if ($ecf->connections == NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, $cf, 0,
"invalid number \"%V\"", $value[1]);
return NGX_CONF_ERROR;
}
$cf->cycle->connection_n = $ecf->connections;
return NGX_CONF_OK;
}
function ngx_event_get_conf($conf_ctx, $module)
{
return ngx_get_conf($conf_ctx, ngx_events_module())[$module->ctx_index];
}
function ngx_event_use(ngx_conf_t $cf, $conf)
//function ngx_event_use(ngx_conf_t $cf, /*ngx_command_t*/ $cmd, $conf)
{
$ecf = $conf;
// ngx_int_t m;
// ngx_str_t *value;
// ngx_event_conf_t *old_ecf;
// ngx_event_module_t *module;
if ($ecf->use != NGX_CONF_UNSET_UINT) {
return "is duplicate";
}
$value = $cf->args;
if ($cf->cycle->old_cycle->conf_ctx) {
$old_ecf = ngx_event_get_conf($cf->cycle->old_cycle->conf_ctx, ngx_event_core_module());
} else {
$old_ecf = NULL;
}
for ($m = 0; ngx_modules($m); $m++) {
if (ngx_modules($m)->type != NGX_EVENT_MODULE) {
continue;
}
$module = ngx_modules($m)->ctx;
//if (module->name->len == value[1].len) {
if (ngx_strcmp($module->name, $value[1]) == 0) {
$ecf->use = ngx_modules($m)->ctx_index;
$ecf->name = $module->name;
if (ngx_process() == NGX_PROCESS_SINGLE
&& $old_ecf
&& $old_ecf->use != $ecf->use)
{
ngx_conf_log_error(NGX_LOG_EMERG, $cf, 0,
"when the server runs without a master process ".
"the \"%V\" event type must be the same as ".
"in previous configuration - \"%s\" ".
"and it cannot be changed on the fly, ".
"to change it you need to stop server ".
"and start it again",
array($value[1], $old_ecf->name));
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
//}
}
ngx_conf_log_error(NGX_LOG_EMERG, $cf, 0,
"invalid event type \"%V\"", $value[1]);
return NGX_CONF_ERROR;
}
function ngx_event_module_init(ngx_cycle_t $cycle)
{
//void ***cf;
// u_char *shared;
// size_t size, cl;
// ngx_shm_t shm;
// ngx_time_t *tp;
// ngx_core_conf_t *ccf;
// ngx_event_conf_t *ecf;
$cf = ngx_get_conf($cycle->conf_ctx, ngx_events_module());
$ecf = $cf[ngx_event_core_module()->ctx_index];
if (!ngx_test_config() && ngx_process() <= NGX_PROCESS_MASTER) {
ngx_log_error(NGX_LOG_NOTICE, $cycle->log, 0,
"using the \"%s\" event method", $ecf->name);
}
$ccf = ngx_get_conf($cycle->conf_ctx, ngx_core_module());
ngx_timer_resolution($ccf->timer_resolution);
// ngx_int_t limit;
// struct rlimit rlmt;
if ($rlmt = posix_getrlimit() == false) {
ngx_log_error(NGX_LOG_ALERT, $cycle->log, 0,
"getrlimit(RLIMIT_NOFILE) failed, ignored");
} else {
if ($ecf->connections > $rlmt['soft openfiles']
&& ($ccf->rlimit_nofile == NGX_CONF_UNSET
|| $ecf->connections > $ccf->rlimit_nofile))
{
$limit = ($ccf->rlimit_nofile == NGX_CONF_UNSET) ?
$rlmt['rlim_cur'] : $ccf->rlimit_nofile;
ngx_log_error(NGX_LOG_WARN, $cycle->log, 0,
"%ui worker_connections exceed ".
"open file resource limit: %i",
array($ecf->connections, $limit));
}
}
if ($ccf->master == 0) {
return NGX_OK;
}
// if (ngx_accept_mutex_ptr) {
// return NGX_OK;