forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_client.c
1804 lines (1573 loc) · 50.2 KB
/
swoole_client.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
/*
+----------------------------------------------------------------------+
| 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]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
#include "php_streams.h"
#include "php_network.h"
#include "ext/standard/basic_functions.h"
#ifdef SW_SOCKETS
#if PHP_VERSION_ID >= 50301 && (HAVE_SOCKETS || defined(COMPILE_DL_SOCKETS))
#include "ext/sockets/php_sockets.h"
#define SWOOLE_SOCKETS_SUPPORT
#else
#error "Enable sockets support, But no sockets extension"
#endif
#endif
#define php_sw_client_onConnect "onConnect"
#define php_sw_client_onReceive "onReceive"
#define php_sw_client_onClose "onClose"
#define php_sw_client_onError "onError"
typedef struct
{
zval *cb_read;
zval *cb_write;
zval *socket;
} swoole_reactor_fd;
uint8_t php_sw_reactor_wait_onexit = 0;
uint8_t php_sw_reactor_ok = 0;
uint8_t php_sw_in_client = 0;
static uint8_t php_sw_event_wait = 0;
static char *php_sw_callbacks[PHP_CLIENT_CALLBACK_NUM] =
{
php_sw_client_onConnect,
php_sw_client_onReceive,
php_sw_client_onClose,
php_sw_client_onError,
};
const zend_function_entry swoole_client_methods[] =
{
PHP_ME(swoole_client, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_client, connect, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, recv, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, send, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, sendfile, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, isConnected, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, close, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, on, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
HashTable php_sw_long_connections;
zend_class_entry swoole_client_ce;
zend_class_entry *swoole_client_class_entry_ptr;
static int php_swoole_client_event_add(zval *sock_array, fd_set *fds, int *max_fd TSRMLS_DC);
static int php_swoole_client_event_loop(zval *sock_array, fd_set *fds TSRMLS_DC);
static int php_swoole_client_close(zval **zobject, int fd TSRMLS_DC);
static int php_swoole_event_onRead(swReactor *reactor, swEvent *event);
static int php_swoole_event_onWrite(swReactor *reactor, swEvent *event);
static int php_swoole_event_onError(swReactor *reactor, swEvent *event);
static int php_swoole_client_onRead(swReactor *reactor, swEvent *event);
static int php_swoole_client_onWrite(swReactor *reactor, swEvent *event);
static int php_swoole_client_onError(swReactor *reactor, swEvent *event);
static void php_swoole_check_eventloop(swReactor *reactor);
static int swoole_client_error_callback(zval *zobject, swEvent *event, int error TSRMLS_DC);
static void php_swoole_onTimeout(swTimer *timer, void *data);
static void php_swoole_onTimerInterval(swTimer *timer, int interval);
static int swoole_convert_to_fd(zval **fd);
static swClient* swoole_client_create_socket(zval *object, char *host, int host_len, int port);
void swoole_client_init(int module_number TSRMLS_DC)
{
INIT_CLASS_ENTRY(swoole_client_ce, "swoole_client", swoole_client_methods);
swoole_client_class_entry_ptr = zend_register_internal_class(&swoole_client_ce TSRMLS_CC);
zend_declare_property_long(swoole_client_class_entry_ptr, SW_STRL("errCode")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_long(swoole_client_class_entry_ptr, SW_STRL("sock")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_hash_init(&php_sw_long_connections, 16, NULL, ZVAL_PTR_DTOR, 1);
}
/**
* @zobject: swoole_client object
*/
static int php_swoole_client_close(zval **zobject, int fd TSRMLS_DC)
{
zval *zcallback = NULL;
zval *retval;
zval **args[1];
swClient *cli;
zval **zres;
if (zend_hash_find(Z_OBJPROP_PP(zobject), SW_STRL("_client"), (void **) &zres) == SUCCESS)
{
ZEND_FETCH_RESOURCE_NO_RETURN(cli, swClient*, zres, -1, SW_RES_CLIENT_NAME, le_swoole_client);
}
else
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client->close[1]: no _client property.");
return SW_ERR;
}
//long tcp connection, clear from php_sw_long_connections
zval *ztype = zend_read_property(swoole_client_class_entry_ptr, *zobject, SW_STRL("type")-1, 0 TSRMLS_CC);
if (ztype == NULL || ZVAL_IS_NULL(ztype))
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "get swoole_client->type failed.");
}
else if (Z_LVAL_P(ztype) & SW_FLAG_KEEP)
{
if (zend_hash_del(&php_sw_long_connections, cli->server_str, cli->server_strlen) == SUCCESS)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client_create_socket add to hashtable failed.");
}
free(cli->server_str);
ZVAL_LONG(ztype, 0);
}
else
{
free(cli->server_str);
}
//async connection
if (cli->async)
{
//remove from reactor
if (SwooleG.main_reactor)
{
SwooleG.main_reactor->del(SwooleG.main_reactor, fd);
}
if (zend_hash_find(&php_sw_client_callback, (char *) &cli->connection.fd, sizeof(cli->connection.fd), (void **)&zobject) != SUCCESS)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client->close[2]: Fd[%d] is not a swoole_client object", fd);
fatal_error:
return SW_ERR;
}
zcallback = zend_read_property(swoole_client_class_entry_ptr, *zobject, SW_STRL(php_sw_client_onClose)-1, 0 TSRMLS_CC);
if (zcallback == NULL || ZVAL_IS_NULL(zcallback))
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client->close[3]: no close callback.");
goto fatal_error;
}
args[0] = zobject;
if (call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client->close[4]: onClose handler error");
goto fatal_error;
}
if (SwooleG.main_reactor->event_num == 0 && php_sw_in_client == 1)
{
SwooleG.running = 0;
}
if (cli->connection.out_buffer != NULL)
{
swBuffer_free(cli->connection.out_buffer);
}
if (zend_hash_del(&php_sw_client_callback, (char *) &cli->connection.fd, sizeof(cli->connection.fd)) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client: del from client callback hashtable failed.");
}
//free the callback return value
if (retval != NULL)
{
zval_ptr_dtor(&retval);
}
cli->close(cli);
}
else
{
cli->close(cli);
}
return SW_OK;
}
static int php_swoole_client_onRead(swReactor *reactor, swEvent *event)
{
int n;
zval **zobject, *zcallback = NULL;
zval **args[2];
zval *retval;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
if (zend_hash_find(&php_sw_client_callback, (char*) &event->fd, sizeof(event->fd), (void **) &zobject) != SUCCESS)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client: Fd[%d] is not a swoole_client object", event->fd);
return SW_ERR;
}
args[0] = zobject;
char *buf = emalloc(SW_CLIENT_BUFFER_SIZE + 1);
#ifdef SW_CLIENT_RECV_AGAIN
recv_again:
#endif
#ifdef SW_USE_EPOLLET
n = swRead(event->fd, buf, SW_CLIENT_BUFFER_SIZE);
#else
//非ET模式会持续通知
n = recv(event->fd, buf, SW_CLIENT_BUFFER_SIZE, 0);
#endif
if (n < 0)
{
switch (swConnection_error(errno))
{
case SW_ERROR:
swWarn("Read from socket[%d] failed. Error: %s [%d]", event->fd, strerror(errno), errno);
goto free_buf;
case SW_CLOSE:
goto close_fd;
default:
goto free_buf;
}
}
else if (n == 0)
{
close_fd:
efree(buf);
return php_swoole_client_close(zobject, event->fd TSRMLS_CC);
}
else
{
zval *zdata;
MAKE_STD_ZVAL(zdata);
buf[n] = 0;
ZVAL_STRINGL(zdata, buf, n, 0);
args[1] = &zdata;
zcallback = zend_read_property(swoole_client_class_entry_ptr, *zobject, SW_STRL(php_sw_client_onReceive)-1, 0 TSRMLS_CC);
if (zcallback == NULL || ZVAL_IS_NULL(zcallback))
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client object have not receive callback.");
goto free_zdata;
}
if (call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "onReactorCallback handler error");
goto free_zdata;
}
if (retval != NULL)
{
zval_ptr_dtor(&retval);
}
#ifdef SW_CLIENT_RECV_AGAIN
if (n == SW_CLIENT_BUFFER_SIZE)
{
goto recv_again;
}
#endif
free_zdata:
zval_ptr_dtor(&zdata);
return SW_OK;
}
free_buf:
efree(buf);
return SW_OK;
}
static int php_swoole_client_onError(swReactor *reactor, swEvent *event)
{
zval **zobject;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
if (zend_hash_find(&php_sw_client_callback, (char*) &event->fd, sizeof(event->fd), (void **)&zobject) != SUCCESS)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client: Fd[%d] is not a swoole_client object", event->fd);
return SW_ERR;
}
int error;
socklen_t len = sizeof(error);
if (getsockopt (event->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client: getsockopt[sock=%d] failed. Error: %s[%d]", event->fd, strerror(errno), errno);
}
swoole_client_error_callback(*zobject, event, error TSRMLS_CC);
return SW_OK;
}
static int php_swoole_event_onError(swReactor *reactor, swEvent *event)
{
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
int error;
socklen_t len = sizeof(error);
if (getsockopt(event->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_event: getsockopt[sock=%d] failed. Error: %s[%d]", event->fd, strerror(errno), errno);
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_event: socket error. Error: %s [%d]", strerror(error), error);
SwooleG.main_reactor->del(SwooleG.main_reactor, event->fd);
return SW_OK;
}
static int php_swoole_client_onWrite(swReactor *reactor, swEvent *event)
{
int ret;
swClient *cli;
swBuffer *out_buffer;
zval **zobject, **zres;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
if (zend_hash_find(&php_sw_client_callback, (char*) &event->fd, sizeof(event->fd), (void **) &zobject) != SUCCESS)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client: Fd[%d] is not a swoole_client object", event->fd);
return SW_ERR;
}
if (zend_hash_find(Z_OBJPROP_PP(zobject), SW_STRL("_client"), (void **) &zres) != SUCCESS)
{
return SW_ERR;
}
ZEND_FETCH_RESOURCE_NO_RETURN(cli, swClient*, zres, -1, SW_RES_CLIENT_NAME, le_swoole_client);
if (cli->connection.active)
{
out_buffer = cli->connection.out_buffer;
while (!swBuffer_empty(out_buffer))
{
swBuffer_trunk *trunk = swBuffer_get_trunk(out_buffer);
if (trunk->type == SW_TRUNK_SENDFILE)
{
swTask_sendfile *task = trunk->store.ptr;
int sendn = (task->filesize - task->offset > SW_SENDFILE_TRUNK) ? SW_SENDFILE_TRUNK : task->filesize - task->offset;
ret = swoole_sendfile(cli->connection.fd, task->fd, &task->offset, sendn);
swTrace("ret=%d|task->offset=%ld|sendn=%d|filesize=%ld", ret, task->offset, sendn, task->filesize);
if (ret <= 0)
{
switch (swConnection_error(errno))
{
case SW_ERROR:
swWarn("sendfile failed. Error: %s[%d]", strerror(errno), errno);
swBuffer_pop_trunk(out_buffer, trunk);
return SW_OK;
case SW_CLOSE:
goto close_fd;
default:
break;
}
}
//sendfile finish
if (task->offset >= task->filesize)
{
swBuffer_pop_trunk(out_buffer, trunk);
close(task->fd);
sw_free(task);
}
}
else
{
ret = swConnection_buffer_send(&cli->connection);
switch (ret)
{
//connection error, close it
case SW_CLOSE:
close_fd:
{
return php_swoole_client_close(zobject, event->fd TSRMLS_CC);
}
break;
//send continue
case SW_CONTINUE:
break;
//reactor_wait
case SW_WAIT:
default:
return SW_OK;
}
}
}
//remove write event
SwooleG.main_reactor->set(SwooleG.main_reactor, event->fd, cli->reactor_fdtype | SW_EVENT_READ);
}
else
{
zval *zcallback = NULL;
zval **args[1];
zval *retval;
int error;
socklen_t len = sizeof(error);
args[0] = zobject;
if (getsockopt (event->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client: getsockopt[sock=%d] failed. Error: %s[%d]", event->fd, strerror(errno), errno);
return SW_ERR;
}
//success
if (error == 0)
{
SwooleG.main_reactor->set(SwooleG.main_reactor, event->fd, (SW_FD_USER+1) | SW_EVENT_READ);
//connected
cli->connection.active = 1;
zcallback = zend_read_property(swoole_client_class_entry_ptr, *zobject, SW_STRL(php_sw_client_onConnect)-1, 0 TSRMLS_CC);
if (zcallback == NULL || ZVAL_IS_NULL(zcallback))
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client: swoole_client object have not connect callback.");
return SW_ERR;
}
if (call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client: onConnect handler error");
return SW_ERR;
}
if (retval)
{
zval_ptr_dtor(&retval);
}
}
else
{
swoole_client_error_callback(*zobject, event, error TSRMLS_CC);
//set fd=0, will not execute other event callback
event->fd = 0;
}
}
return SW_OK;
}
static int swoole_client_error_callback(zval *zobject, swEvent *event, int error TSRMLS_DC)
{
zval *zcallback;
zval *retval;
zval **args[1];
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client: connect to server failed. Error: %s [%d]", strerror(error), error);
SwooleG.main_reactor->del(SwooleG.main_reactor, event->fd);
zcallback = zend_read_property(swoole_client_class_entry_ptr, zobject, SW_STRL(php_sw_client_onError)-1, 0 TSRMLS_CC);
zend_update_property_long(swoole_client_class_entry_ptr, zobject, ZEND_STRL("errCode"), error TSRMLS_CC);
args[0] = &zobject;
if (zcallback == NULL || ZVAL_IS_NULL(zcallback))
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client: swoole_client object have not error callback.");
return SW_ERR;
}
if (call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client: onError handler error");
return SW_ERR;
}
if (retval)
{
zval_ptr_dtor(&retval);
}
return SW_OK;
}
static void php_swoole_check_eventloop(swReactor *reactor)
{
if (reactor->event_num == 0)
{
SwooleG.running = 0;
}
}
void php_swoole_check_reactor()
{
if (php_sw_reactor_ok == 0)
{
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
if (SwooleG.main_reactor == NULL)
{
SwooleG.main_reactor = sw_malloc(sizeof(swReactor));
if (SwooleG.main_reactor == NULL)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "swoole_client: malloc SwooleG.main_reactor failed.");
return;
}
if (swReactor_auto(SwooleG.main_reactor, SW_REACTOR_MAXEVENTS) < 0)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "swoole_client: create SwooleG.main_reactor failed.");
return;
}
SwooleG.main_reactor->onFinish = php_swoole_check_eventloop;
//client, swoole_event_exit will set swoole_running = 0
php_sw_in_client = 1;
}
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, (SW_FD_USER+1) | SW_EVENT_READ, php_swoole_client_onRead);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, (SW_FD_USER+1) | SW_EVENT_WRITE, php_swoole_client_onWrite);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, (SW_FD_USER+1) | SW_EVENT_ERROR, php_swoole_client_onError);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_USER | SW_EVENT_READ , php_swoole_event_onRead);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_USER | SW_EVENT_WRITE, php_swoole_event_onWrite);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, SW_FD_USER | SW_EVENT_ERROR, php_swoole_event_onError);
php_sw_reactor_ok = 1;
}
return;
}
void php_swoole_check_timer(int msec)
{
if (SwooleG.timer.fd == 0)
{
if (SwooleG.serv)
{
SwooleG.timer.onTimer = swServer_onTimer;
}
else
{
SwooleG.timer.onTimer = php_swoole_onTimerInterval;
}
if (!SwooleG.main_reactor)
{
swTimer_init(msec, SwooleG.use_timer_pipe);
}
else
{
swEventTimer_init();
SwooleG.main_reactor->onFinish = swWorker_onReactorFinish;
SwooleG.main_reactor->onTimeout = swWorker_onReactorTimeout;
SwooleG.main_reactor->timeout_msec = msec;
}
SwooleG.timer.interval = msec;
SwooleG.timer.onTimeout = php_swoole_onTimeout;
}
}
static void php_swoole_onTimeout(swTimer *timer, void *data)
{
swTimer_callback* callback = (swTimer_callback*)data;
zval *retval = NULL;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
zval **args[1];
args[0] = &callback->data;
if (call_user_function_ex(EG(function_table), NULL, callback->callback, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_timer: onTimerout handler error");
return;
}
if (retval != NULL)
{
zval_ptr_dtor(&retval);
}
zval_ptr_dtor(&callback->callback);
zval_ptr_dtor(&callback->data);
}
static void php_swoole_onTimerInterval(swTimer *timer, int interval)
{
zval *retval;
zval **args[1];
swoole_timer_item *timer_item;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
if (zend_hash_find(&php_sw_timer_callback, (char *) &interval, sizeof(interval), (void**) &timer_item) != SUCCESS)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_timer: onTimerCallback not found");
return;
}
zval *zinterval;
MAKE_STD_ZVAL(zinterval);
ZVAL_LONG(zinterval, interval);
args[0] = &zinterval;
if (call_user_function_ex(EG(function_table), NULL, timer_item->callback, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_timer: onTimerCallback handler error");
return;
}
if (retval != NULL)
{
zval_ptr_dtor(&retval);
}
zval_ptr_dtor(&zinterval);
}
static int php_swoole_event_onRead(swReactor *reactor, swEvent *event)
{
zval *retval;
zval **args[1];
swoole_reactor_fd *fd;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
if (zend_hash_find(&php_sw_event_callback, (char *)&(event->fd), sizeof(event->fd), (void**)&fd) != SUCCESS)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_event: onRead not found");
return SW_ERR;
}
args[0] = &fd->socket;
if (call_user_function_ex(EG(function_table), NULL, fd->cb_read, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_event: onRead handler error");
return SW_ERR;
}
if (retval != NULL)
{
zval_ptr_dtor(&retval);
}
return SW_OK;
}
static int php_swoole_event_onWrite(swReactor *reactor, swEvent *event)
{
zval *retval;
zval **args[1];
swoole_reactor_fd *fd;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
if (zend_hash_find(&php_sw_event_callback, (char *)&(event->fd), sizeof(event->fd), (void**)&fd) != SUCCESS)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_event: onWrite not found");
return SW_ERR;
}
args[0] = &fd->socket;
if (call_user_function_ex(EG(function_table), NULL, fd->cb_write, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_event: onWrite handler error");
return SW_ERR;
}
if (retval != NULL)
{
zval_ptr_dtor(&retval);
}
return SW_OK;
}
void php_swoole_try_run_reactor()
{
//only client side
if (php_sw_in_client == 1 && php_sw_reactor_wait_onexit == 0)
{
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
zval *callback;
MAKE_STD_ZVAL(callback);
#if PHP_MAJOR_VERSION >= 5 && PHP_MINOR_VERSION >= 4
php_shutdown_function_entry shutdown_function_entry;
shutdown_function_entry.arg_count = 1;
shutdown_function_entry.arguments = (zval **) safe_emalloc(sizeof(zval *), 1, 0);
ZVAL_STRING(callback, "swoole_event_wait", 1);
shutdown_function_entry.arguments[0] = callback;
if (!register_user_shutdown_function("swoole_event_wait", sizeof("swoole_event_wait"), &shutdown_function_entry TSRMLS_CC))
{
efree(shutdown_function_entry.arguments);
zval_ptr_dtor(&callback);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to register shutdown function [swoole_event_wait]");
}
#else
SwooleG.running = 1;
php_sw_event_wait = 1;
int ret = SwooleG.main_reactor->wait(SwooleG.main_reactor, NULL);
if (ret < 0)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "reactor wait failed. Error: %s [%d]", strerror(errno), errno);
}
#endif
php_sw_reactor_wait_onexit = 1;
php_sw_event_wait = 0;
}
}
static swClient* swoole_client_create_socket(zval *object, char *host, int host_len, int port)
{
zval *ztype, *zres;
int async = 0;
swClient *cli;
char conn_key[SW_LONG_CONNECTION_KEY_LEN];
int conn_key_len = 0;
uint64_t tmp_buf;
int ret;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
ztype = zend_read_property(swoole_client_class_entry_ptr, object, SW_STRL("type")-1, 0 TSRMLS_CC);
if (ztype == NULL || ZVAL_IS_NULL(ztype))
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "get swoole_client->type failed.");
return NULL;
}
long type = Z_LVAL_P(ztype);
//new flag, swoole-1.6.12+
if (type & SW_FLAG_ASYNC)
{
async = 1;
}
bzero(conn_key, SW_LONG_CONNECTION_KEY_LEN);
zval *connection_id = zend_read_property(swoole_client_class_entry_ptr, object, ZEND_STRL("id"), 1 TSRMLS_CC);
if (connection_id == NULL || ZVAL_IS_NULL(connection_id))
{
conn_key_len = snprintf(conn_key, SW_LONG_CONNECTION_KEY_LEN, "%s:%d", host, port) + 1;
}
else
{
conn_key_len = snprintf(conn_key, SW_LONG_CONNECTION_KEY_LEN, "%s", Z_STRVAL_P(connection_id)) + 1;
}
//keep the tcp connection
if (type & SW_FLAG_KEEP)
{
swClient **find;
if (zend_hash_find(&php_sw_long_connections, conn_key, conn_key_len, (void **) &find) == FAILURE)
{
cli = (swClient*) pemalloc(sizeof(swClient), 1);
if (zend_hash_update(&php_sw_long_connections, conn_key, conn_key_len, &cli, sizeof(cli), NULL) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_client_create_socket add to hashtable failed.");
}
goto create_socket;
}
else
{
cli = *find;
//try recv, check connection status
ret = recv(cli->connection.fd, &tmp_buf, sizeof(tmp_buf), MSG_DONTWAIT | MSG_PEEK);
if (ret == 0 || (ret < 0 && swConnection_error(errno) == SW_CLOSE))
{
cli->close(cli);
goto create_socket;
}
}
}
else
{
cli = (swClient*) emalloc(sizeof(swClient));
create_socket:
if (swClient_create(cli, php_swoole_socktype(type), async) < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "create failed. Error: %s [%d]", strerror(errno), errno);
zend_update_property_long(swoole_client_class_entry_ptr, object, ZEND_STRL("errCode"), errno TSRMLS_CC);
return NULL;
}
//don't forget free it
cli->server_str = strdup(conn_key);
cli->server_strlen = conn_key_len;
}
MAKE_STD_ZVAL(zres);
ZEND_REGISTER_RESOURCE(zres, cli, le_swoole_client);
zend_update_property_long(swoole_client_class_entry_ptr, object, ZEND_STRL("sock"), cli->connection.fd TSRMLS_CC);
zend_update_property(swoole_client_class_entry_ptr, object, ZEND_STRL("_client"), zres TSRMLS_CC);
zval_ptr_dtor(&zres);
if (type & SW_FLAG_KEEP)
{
cli->keep = 1;
}
return cli;
}
static int swoole_convert_to_fd(zval **fd)
{
php_stream *stream;
int socket_fd;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#ifdef SWOOLE_SOCKETS_SUPPORT
php_socket *php_sock;
#endif
if (Z_TYPE_PP(fd) == IS_RESOURCE)
{
if (ZEND_FETCH_RESOURCE_NO_RETURN(stream, php_stream *, fd, -1, NULL, php_file_le_stream()))
{
if (php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void* )&socket_fd, 1)
!= SUCCESS || socket_fd < 0)
{
return SW_ERR;
}
}
else
{
#ifdef SWOOLE_SOCKETS_SUPPORT
if (ZEND_FETCH_RESOURCE_NO_RETURN(php_sock, php_socket *, fd, -1, NULL, php_sockets_le_socket()))
{
socket_fd = php_sock->bsd_socket;
}
else
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be either valid PHP stream or valid PHP socket resource");
return SW_ERR;
}
#else
php_error_docref(NULL TSRMLS_CC, E_WARNING, "fd argument must be valid PHP stream resource");
return SW_ERR;
#endif
}
}
else if (Z_TYPE_PP(fd) == IS_LONG)
{
socket_fd = Z_LVAL_PP(fd);
if (socket_fd < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid file descriptor passed");
return SW_ERR;
}
}
else
{
return SW_ERR;
}
return socket_fd;
}
PHP_FUNCTION(swoole_timer_add)
{
swoole_timer_item timer_item;
long interval;
if (swIsMaster())
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_timer_add can not use in swoole_server. Please use swoole_server->addtimer");
RETURN_FALSE;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lz", &interval, &timer_item.callback) == FAILURE)
{
return;
}
#ifdef ZTS
if (sw_thread_ctx == NULL)
{
TSRMLS_SET_CTX(sw_thread_ctx);
}
#endif
zval_add_ref(&timer_item.callback);
timer_item.interval = (int)interval;
if (zend_hash_update(&php_sw_timer_callback, (char *)&timer_item.interval, sizeof(timer_item.interval), &timer_item, sizeof(swoole_timer_item), NULL) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_timer_add add to hashtable failed.");
RETURN_FALSE;
}
php_swoole_check_reactor();
php_swoole_check_timer(timer_item.interval);
if (SwooleG.timer.add(&SwooleG.timer, timer_item.interval, 1, NULL) < 0)
{
RETURN_FALSE;
}
php_swoole_try_run_reactor();
RETURN_TRUE;
}
PHP_FUNCTION(swoole_timer_del)
{
long interval;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &interval) == FAILURE)
{
return;
}
if (SwooleG.timer.fd == 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no timer.");
RETURN_FALSE;
}
SwooleG.timer.del(&SwooleG.timer, (int)interval);
RETURN_TRUE;
}
PHP_FUNCTION(swoole_event_add)
{
zval *cb_read = NULL;
zval *cb_write = NULL;
zval **fd;
char *func_name = NULL;
long event_flag = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|zzl", &fd, &cb_read, &cb_write, &event_flag) == FAILURE)
{
return;
}
#ifdef ZTS
if (sw_thread_ctx == NULL)
{
TSRMLS_SET_CTX(sw_thread_ctx);
}
#endif
if ((cb_read == NULL && cb_write == NULL) || (ZVAL_IS_NULL(cb_read) && ZVAL_IS_NULL(cb_write)))
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no read or write event callback.");
RETURN_FALSE;
}
int socket_fd = swoole_convert_to_fd(fd);
if (socket_fd < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknow type.");
RETURN_FALSE;
}
swoole_reactor_fd event;
event.socket = *fd;
event.cb_read = cb_read;
event.cb_write = cb_write;
zval_add_ref(&event.socket);
if (cb_read!= NULL && !ZVAL_IS_NULL(cb_read))
{
if (!zend_is_callable(cb_read, 0, &func_name TSRMLS_CC))
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Function '%s' is not callable", func_name);
efree(func_name);
RETURN_FALSE;
}
zval_add_ref(&event.cb_read);
}
if (cb_write!= NULL && !ZVAL_IS_NULL(cb_write))
{
if (!zend_is_callable(cb_write, 0, &func_name TSRMLS_CC))
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Function '%s' is not callable", func_name);
efree(func_name);
RETURN_FALSE;
}