forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
swoole_client.c
2501 lines (2284 loc) · 73.8 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 "socks5.h"
#include "mqtt.h"
#include "ext/standard/basic_functions.h"
typedef struct
{
zval *onConnect;
zval *onReceive;
zval *onClose;
zval *onError;
zval *onBufferFull;
zval *onBufferEmpty;
#ifdef SW_USE_OPENSSL
zval *onSSLReady;
#endif
#ifdef PHP_SWOOLE_ENABLE_FASTCALL
zend_fcall_info_cache cache_onConnect;
zend_fcall_info_cache cache_onReceive;
zend_fcall_info_cache cache_onClose;
zend_fcall_info_cache cache_onError;
zend_fcall_info_cache cache_onBufferFull;
zend_fcall_info_cache cache_onBufferEmpty;
#ifdef SW_USE_OPENSSL
zend_fcall_info_cache cache_onSSLReady;
#endif
#endif
#if PHP_MAJOR_VERSION >= 7
zval _object;
zval _onConnect;
zval _onReceive;
zval _onClose;
zval _onError;
zval _onBufferFull;
zval _onBufferEmpty;
#ifdef SW_USE_OPENSSL
zval _onSSLReady;
#endif
#endif
} client_callback;
enum client_property
{
client_property_callback = 0,
client_property_coroutine = 1,
client_property_socket = 2,
};
static PHP_METHOD(swoole_client, __construct);
static PHP_METHOD(swoole_client, __destruct);
static PHP_METHOD(swoole_client, set);
static PHP_METHOD(swoole_client, connect);
static PHP_METHOD(swoole_client, recv);
static PHP_METHOD(swoole_client, send);
static PHP_METHOD(swoole_client, pipe);
static PHP_METHOD(swoole_client, sendfile);
static PHP_METHOD(swoole_client, sendto);
static PHP_METHOD(swoole_client, sleep);
static PHP_METHOD(swoole_client, wakeup);
#ifdef SW_USE_OPENSSL
static PHP_METHOD(swoole_client, enableSSL);
static PHP_METHOD(swoole_client, getPeerCert);
static PHP_METHOD(swoole_client, verifyPeerCert);
#endif
static PHP_METHOD(swoole_client, isConnected);
static PHP_METHOD(swoole_client, getsockname);
static PHP_METHOD(swoole_client, getpeername);
static PHP_METHOD(swoole_client, close);
static PHP_METHOD(swoole_client, shutdown);
static PHP_METHOD(swoole_client, on);
#ifdef SWOOLE_SOCKETS_SUPPORT
static PHP_METHOD(swoole_client, getSocket);
#endif
#ifdef PHP_SWOOLE_CLIENT_USE_POLL
static int client_poll_add(zval *sock_array, int index, struct pollfd *fds, int maxevents, int event);
static int client_poll_wait(zval *sock_array, struct pollfd *fds, int maxevents, int n_event, int revent);
#else
static int client_select_add(zval *sock_array, fd_set *fds, int *max_fd TSRMLS_DC);
static int client_select_wait(zval *sock_array, fd_set *fds TSRMLS_DC);
#endif
static void client_onConnect(swClient *cli);
static void client_onReceive(swClient *cli, char *data, uint32_t length);
static void client_onClose(swClient *cli);
static void client_onError(swClient *cli);
static void client_onBufferFull(swClient *cli);
static void client_onBufferEmpty(swClient *cli);
static sw_inline void client_execute_callback(zval *zobject, enum php_swoole_client_callback_type type)
{
SWOOLE_GET_TSRMLS;
zval *callback = NULL;
zval *retval = NULL;
zval **args[1];
client_callback *cb = (client_callback *) swoole_get_property(zobject, client_property_callback);
char *callback_name;
#ifdef PHP_SWOOLE_ENABLE_FASTCALL
zend_fcall_info_cache *fci_cache;
#endif
switch(type)
{
case SW_CLIENT_CB_onConnect:
callback = cb->onConnect;
callback_name = "onConnect";
#ifdef PHP_SWOOLE_ENABLE_FASTCALL
fci_cache = &cb->cache_onConnect;
#endif
break;
case SW_CLIENT_CB_onError:
callback = cb->onError;
callback_name = "onError";
#ifdef PHP_SWOOLE_ENABLE_FASTCALL
fci_cache = &cb->cache_onError;
#endif
break;
case SW_CLIENT_CB_onClose:
callback = cb->onClose;
callback_name = "onClose";
#ifdef PHP_SWOOLE_ENABLE_FASTCALL
fci_cache = &cb->cache_onClose;
#endif
break;
case SW_CLIENT_CB_onBufferFull:
callback = cb->onBufferFull;
callback_name = "onBufferFull";
#ifdef PHP_SWOOLE_ENABLE_FASTCALL
fci_cache = &cb->cache_onBufferFull;
#endif
break;
case SW_CLIENT_CB_onBufferEmpty:
callback = cb->onBufferEmpty;
callback_name = "onBufferEmpty";
#ifdef PHP_SWOOLE_ENABLE_FASTCALL
fci_cache = &cb->cache_onBufferEmpty;
#endif
break;
#ifdef SW_USE_OPENSSL
case SW_CLIENT_CB_onSSLReady:
callback = cb->onSSLReady;
callback_name = "onSSLReady";
#ifdef PHP_SWOOLE_ENABLE_FASTCALL
fci_cache = &cb->cache_onSSLReady;
#endif
break;
#endif
default:
return;
}
if (callback == NULL || ZVAL_IS_NULL(callback))
{
swoole_php_fatal_error(E_WARNING, "object have not %s callback.", callback_name);
return;
}
args[0] = &zobject;
#ifdef PHP_SWOOLE_ENABLE_FASTCALL
if (sw_call_user_function_fast(callback, fci_cache, &retval, 1, args TSRMLS_CC) == FAILURE)
#else
if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
#endif
{
swoole_php_fatal_error(E_WARNING, "%s handler error.", callback_name);
return;
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
}
static sw_inline swClient* client_get_ptr(zval *zobject TSRMLS_DC)
{
swClient *cli = (swClient *) swoole_get_object(zobject);
if (cli && cli->socket && cli->socket->active == 1)
{
return cli;
}
else
{
SwooleG.error = SW_ERROR_CLIENT_NO_CONNECTION;
zend_update_property_long(swoole_client_class_entry_ptr, zobject, SW_STRL("errCode")-1, SwooleG.error TSRMLS_CC);
swoole_php_error(E_WARNING, "client is not connected to server.");
return NULL;
}
}
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_void, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_construct, 0, 0, 1)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, async)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_set, 0, 0, 1)
ZEND_ARG_ARRAY_INFO(0, settings, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_connect, 0, 0, 1)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, timeout)
ZEND_ARG_INFO(0, sock_flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_recv, 0, 0, 0)
ZEND_ARG_INFO(0, size)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_send, 0, 0, 1)
ZEND_ARG_INFO(0, data)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_pipe, 0, 0, 1)
ZEND_ARG_INFO(0, dst_socket)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_sendfile, 0, 0, 1)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_INFO(0, offset)
ZEND_ARG_INFO(0, length)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_sendto, 0, 0, 3)
ZEND_ARG_INFO(0, ip)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_close, 0, 0, 0)
ZEND_ARG_INFO(0, force)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_shutdown, 0, 0, 1)
ZEND_ARG_INFO(0, how)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_on, 0, 0, 2)
ZEND_ARG_INFO(0, event_name)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
#ifdef SW_USE_OPENSSL
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_client_enableSSL, 0, 0, 0)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
#endif
static const zend_function_entry swoole_client_methods[] =
{
PHP_ME(swoole_client, __construct, arginfo_swoole_client_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_client, __destruct, arginfo_swoole_client_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_client, set, arginfo_swoole_client_set, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, connect, arginfo_swoole_client_connect, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, recv, arginfo_swoole_client_recv, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, send, arginfo_swoole_client_send, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, pipe, arginfo_swoole_client_pipe, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, sendfile, arginfo_swoole_client_sendfile, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, sendto, arginfo_swoole_client_sendto, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, sleep, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, wakeup, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_client, pause, sleep, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_client, resume, wakeup, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, shutdown, arginfo_swoole_client_shutdown, ZEND_ACC_PUBLIC)
#ifdef SW_USE_OPENSSL
PHP_ME(swoole_client, enableSSL, arginfo_swoole_client_enableSSL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, getPeerCert, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, verifyPeerCert, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
#endif
PHP_ME(swoole_client, isConnected, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, getsockname, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, getpeername, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, close, arginfo_swoole_client_close, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, on, arginfo_swoole_client_on, ZEND_ACC_PUBLIC)
#ifdef SWOOLE_SOCKETS_SUPPORT
PHP_ME(swoole_client, getSocket, arginfo_swoole_client_void, ZEND_ACC_PUBLIC)
#endif
PHP_FE_END
};
static swHashMap *php_sw_long_connections;
zend_class_entry swoole_client_ce;
zend_class_entry *swoole_client_class_entry_ptr;
void swoole_client_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_client_ce, "swoole_client", "Swoole\\Client", swoole_client_methods);
swoole_client_class_entry_ptr = zend_register_internal_class(&swoole_client_ce TSRMLS_CC);
swoole_client_class_entry_ptr->serialize = zend_class_serialize_deny;
swoole_client_class_entry_ptr->unserialize = zend_class_unserialize_deny;
SWOOLE_CLASS_ALIAS(swoole_client, "Swoole\\Client");
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_declare_property_bool(swoole_client_class_entry_ptr, SW_STRL("reuse")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_long(swoole_client_class_entry_ptr, SW_STRL("reuseCount")-1, 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_long(swoole_client_class_entry_ptr, ZEND_STRL("type"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_client_class_entry_ptr, ZEND_STRL("id"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_client_class_entry_ptr, ZEND_STRL("setting"), ZEND_ACC_PUBLIC TSRMLS_CC);
/**
* event callback
*/
zend_declare_property_null(swoole_client_class_entry_ptr, ZEND_STRL("onConnect"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_client_class_entry_ptr, ZEND_STRL("onError"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_client_class_entry_ptr, ZEND_STRL("onReceive"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_client_class_entry_ptr, ZEND_STRL("onClose"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_client_class_entry_ptr, ZEND_STRL("onBufferFull"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_client_class_entry_ptr, ZEND_STRL("onBufferEmpty"), ZEND_ACC_PUBLIC TSRMLS_CC);
#ifdef SW_USE_OPENSSL
zend_declare_property_null(swoole_client_class_entry_ptr, ZEND_STRL("onSSLReady"), ZEND_ACC_PUBLIC TSRMLS_CC);
#endif
php_sw_long_connections = swHashMap_new(SW_HASHMAP_INIT_BUCKET_N, NULL);
zend_declare_class_constant_long(swoole_client_class_entry_ptr, ZEND_STRL("MSG_OOB"), MSG_OOB TSRMLS_CC);
zend_declare_class_constant_long(swoole_client_class_entry_ptr, ZEND_STRL("MSG_PEEK"), MSG_PEEK TSRMLS_CC);
zend_declare_class_constant_long(swoole_client_class_entry_ptr, ZEND_STRL("MSG_DONTWAIT"), MSG_DONTWAIT TSRMLS_CC);
zend_declare_class_constant_long(swoole_client_class_entry_ptr, ZEND_STRL("MSG_WAITALL"), MSG_WAITALL TSRMLS_CC);
zend_declare_class_constant_long(swoole_client_class_entry_ptr, ZEND_STRL("SHUT_RDWR"), SHUT_RDWR TSRMLS_CC);
zend_declare_class_constant_long(swoole_client_class_entry_ptr, ZEND_STRL("SHUT_RD"), SHUT_RD TSRMLS_CC);
zend_declare_class_constant_long(swoole_client_class_entry_ptr, ZEND_STRL("SHUT_WR"), SHUT_WR TSRMLS_CC);
}
static void client_onReceive(swClient *cli, char *data, uint32_t length)
{
zval *zobject = (zval *) cli->object;
zval *zcallback = NULL;
zval **args[2];
zval *retval;
zval *zdata;
SW_MAKE_STD_ZVAL(zdata);
SW_ZVAL_STRINGL(zdata, data, length, 1);
args[0] = &zobject;
args[1] = &zdata;
client_callback *cb = (client_callback *) swoole_get_property(zobject, 0);
zcallback = cb->onReceive;
if (zcallback == NULL || ZVAL_IS_NULL(zcallback))
{
swoole_php_fatal_error(E_WARNING, "swoole_client object has no 'onReceive' callback function.");
goto free_zdata;
}
#ifdef PHP_SWOOLE_ENABLE_FASTCALL
if (sw_call_user_function_fast(zcallback, &cb->cache_onReceive, &retval, 2, args TSRMLS_CC) == FAILURE)
#else
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
#endif
{
swoole_php_fatal_error(E_WARNING, "onReactorCallback handler error");
goto free_zdata;
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
free_zdata:
sw_zval_ptr_dtor(&zdata);
}
static void client_onConnect(swClient *cli)
{
zval *zobject = (zval *) cli->object;
#ifdef SW_USE_OPENSSL
if (cli->ssl_wait_handshake)
{
client_execute_callback(zobject, SW_CLIENT_CB_onSSLReady);
}
else
#endif
if (!cli->redirect)
{
client_execute_callback(zobject, SW_CLIENT_CB_onConnect);
}
else
{
SWOOLE_GET_TSRMLS;
client_callback *cb = (client_callback *) swoole_get_property(zobject, 0);
if (!cb || !cb->onReceive)
{
swoole_php_fatal_error(E_ERROR, "has no 'onReceive' callback function.");
}
}
}
static void client_onClose(swClient *cli)
{
SWOOLE_GET_TSRMLS;
zval *zobject = (zval *) cli->object;
php_swoole_client_free(zobject, cli TSRMLS_CC);
client_execute_callback(zobject, SW_CLIENT_CB_onClose);
sw_zval_ptr_dtor(&zobject);
}
static void client_onError(swClient *cli)
{
SWOOLE_GET_TSRMLS;
zval *zobject = (zval *) cli->object;
zend_update_property_long(swoole_client_class_entry_ptr, zobject, ZEND_STRL("errCode"), SwooleG.error TSRMLS_CC);
php_swoole_client_free(zobject, cli TSRMLS_CC);
client_execute_callback(zobject, SW_CLIENT_CB_onError);
sw_zval_ptr_dtor(&zobject);
}
static void client_onBufferFull(swClient *cli)
{
zval *zobject = (zval *) cli->object;
client_execute_callback(zobject, SW_CLIENT_CB_onBufferFull);
}
static void client_onBufferEmpty(swClient *cli)
{
zval *zobject = (zval *) cli->object;
client_execute_callback(zobject, SW_CLIENT_CB_onBufferEmpty);
}
#ifdef SW_USE_OPENSSL
void php_swoole_client_check_ssl_setting(swClient *cli, zval *zset TSRMLS_DC)
{
HashTable *vht = Z_ARRVAL_P(zset);
zval *v;
if (php_swoole_array_get_value(vht, "ssl_method", v))
{
convert_to_long(v);
cli->ssl_option.method = (int) Z_LVAL_P(v);
}
if (php_swoole_array_get_value(vht, "ssl_compress", v))
{
convert_to_boolean(v);
cli->ssl_option.disable_compress = !Z_BVAL_P(v);
}
if (php_swoole_array_get_value(vht, "ssl_cert_file", v))
{
convert_to_string(v);
cli->ssl_option.cert_file = sw_strdup(Z_STRVAL_P(v));
if (access(cli->ssl_option.cert_file, R_OK) < 0)
{
swoole_php_fatal_error(E_ERROR, "ssl cert file[%s] not found.", cli->ssl_option.cert_file);
return;
}
}
if (php_swoole_array_get_value(vht, "ssl_key_file", v))
{
convert_to_string(v);
cli->ssl_option.key_file = sw_strdup(Z_STRVAL_P(v));
if (access(cli->ssl_option.key_file, R_OK) < 0)
{
swoole_php_fatal_error(E_ERROR, "ssl key file[%s] not found.", cli->ssl_option.key_file);
return;
}
}
if (php_swoole_array_get_value(vht, "ssl_passphrase", v))
{
convert_to_string(v);
cli->ssl_option.passphrase = sw_strdup(Z_STRVAL_P(v));
}
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
if (php_swoole_array_get_value(vht, "ssl_host_name", v))
{
convert_to_string(v);
cli->ssl_option.tls_host_name = sw_strdup(Z_STRVAL_P(v));
}
#endif
if (php_swoole_array_get_value(vht, "ssl_verify_peer", v))
{
convert_to_boolean(v);
cli->ssl_option.verify_peer = Z_BVAL_P(v);
}
if (php_swoole_array_get_value(vht, "ssl_allow_self_signed", v))
{
convert_to_boolean(v);
cli->ssl_option.allow_self_signed = Z_BVAL_P(v);
}
if (php_swoole_array_get_value(vht, "ssl_cafile", v))
{
convert_to_string(v);
cli->ssl_option.cafile = sw_strdup(Z_STRVAL_P(v));
}
if (php_swoole_array_get_value(vht, "ssl_capath", v))
{
convert_to_string(v);
cli->ssl_option.capath = sw_strdup(Z_STRVAL_P(v));
}
if (php_swoole_array_get_value(vht, "ssl_verify_depth", v))
{
convert_to_long(v);
cli->ssl_option.verify_depth = (int) Z_LVAL_P(v);
}
if (cli->ssl_option.cert_file && !cli->ssl_option.key_file)
{
swoole_php_fatal_error(E_ERROR, "ssl require key file.");
return;
}
}
#endif
int php_swoole_client_isset_callback(zval *zobject, int type TSRMLS_DC)
{
client_callback *cb = (client_callback *) swoole_get_property(zobject, client_property_callback);
switch (type)
{
case SW_CLIENT_CB_onConnect:
return cb->onConnect != NULL;
case SW_CLIENT_CB_onError:
return cb->onError != NULL;
case SW_CLIENT_CB_onClose:
return cb->onClose != NULL;
case SW_CLIENT_CB_onBufferFull:
return cb->onBufferFull != NULL;
case SW_CLIENT_CB_onBufferEmpty:
return cb->onBufferEmpty != NULL;
#ifdef SW_USE_OPENSSL
case SW_CLIENT_CB_onSSLReady:
return cb->onSSLReady != NULL;
#endif
default:
return SW_FALSE;
}
}
void php_swoole_client_check_setting(swClient *cli, zval *zset TSRMLS_DC)
{
HashTable *vht;
zval *v;
int value = 1;
char *bind_address = NULL;
int bind_port = 0;
vht = Z_ARRVAL_P(zset);
//buffer: eof check
if (php_swoole_array_get_value(vht, "open_eof_check", v))
{
convert_to_boolean(v);
cli->open_eof_check = Z_BVAL_P(v);
}
//buffer: split package with eof
if (php_swoole_array_get_value(vht, "open_eof_split", v))
{
convert_to_boolean(v);
cli->protocol.split_by_eof = Z_BVAL_P(v);
if (cli->protocol.split_by_eof)
{
cli->open_eof_check = 1;
}
}
//package eof
if (php_swoole_array_get_value(vht, "package_eof", v))
{
convert_to_string(v);
cli->protocol.package_eof_len = Z_STRLEN_P(v);
if (cli->protocol.package_eof_len > SW_DATA_EOF_MAXLEN)
{
swoole_php_fatal_error(E_ERROR, "pacakge_eof max length is %d", SW_DATA_EOF_MAXLEN);
return;
}
bzero(cli->protocol.package_eof, SW_DATA_EOF_MAXLEN);
memcpy(cli->protocol.package_eof, Z_STRVAL_P(v), Z_STRLEN_P(v));
}
//open mqtt protocol
if (php_swoole_array_get_value(vht, "open_mqtt_protocol", v))
{
convert_to_boolean(v);
cli->open_length_check = Z_BVAL_P(v);
cli->protocol.get_package_length = swMqtt_get_package_length;
}
//open length check
if (php_swoole_array_get_value(vht, "open_length_check", v))
{
convert_to_boolean(v);
cli->open_length_check = Z_BVAL_P(v);
cli->protocol.get_package_length = swProtocol_get_package_length;
}
//package length size
if (php_swoole_array_get_value(vht, "package_length_type", v))
{
convert_to_string(v);
cli->protocol.package_length_type = Z_STRVAL_P(v)[0];
cli->protocol.package_length_size = swoole_type_size(cli->protocol.package_length_type);
if (cli->protocol.package_length_size == 0)
{
swoole_php_fatal_error(E_ERROR, "Unknown package_length_type name '%c', see pack(). Link: http://php.net/pack", cli->protocol.package_length_type);
return;
}
}
//length function
if (php_swoole_array_get_value(vht, "package_length_func", v))
{
while(1)
{
if (Z_TYPE_P(v) == IS_STRING)
{
swProtocol_length_function func = (swProtocol_length_function) swoole_get_function(Z_STRVAL_P(v),
Z_STRLEN_P(v));
if (func != NULL)
{
cli->protocol.get_package_length = func;
break;
}
}
char *func_name = NULL;
if (!sw_zend_is_callable(v, 0, &func_name TSRMLS_CC))
{
swoole_php_fatal_error(E_ERROR, "Function '%s' is not callable", func_name);
efree(func_name);
return;
}
efree(func_name);
cli->protocol.get_package_length = php_swoole_length_func;
sw_zval_add_ref(&v);
cli->protocol.private_data = sw_zval_dup(v);
break;
}
cli->protocol.package_length_size = 0;
cli->protocol.package_length_type = '\0';
cli->protocol.package_length_offset = SW_BUFFER_SIZE;
}
//package length offset
if (php_swoole_array_get_value(vht, "package_length_offset", v))
{
convert_to_long(v);
cli->protocol.package_length_offset = (int) Z_LVAL_P(v);
}
//package body start
if (php_swoole_array_get_value(vht, "package_body_offset", v))
{
convert_to_long(v);
cli->protocol.package_body_offset = (int) Z_LVAL_P(v);
}
/**
* package max length
*/
if (php_swoole_array_get_value(vht, "package_max_length", v))
{
convert_to_long(v);
cli->protocol.package_max_length = (int) Z_LVAL_P(v);
}
else
{
cli->protocol.package_max_length = SW_BUFFER_INPUT_SIZE;
}
/**
* socket send/recv buffer size
*/
if (php_swoole_array_get_value(vht, "socket_buffer_size", v))
{
convert_to_long(v);
value = (int) Z_LVAL_P(v);
if (value <= 0)
{
value = SW_MAX_INT;
}
swSocket_set_buffer_size(cli->socket->fd, value);
cli->socket->buffer_size = value;
}
if (php_swoole_array_get_value(vht, "buffer_high_watermark", v))
{
convert_to_long(v);
value = (int) Z_LVAL_P(v);
cli->buffer_high_watermark = value;
}
if (php_swoole_array_get_value(vht, "buffer_low_watermark", v))
{
convert_to_long(v);
value = (int) Z_LVAL_P(v);
cli->buffer_low_watermark = value;
}
/**
* bind address
*/
if (php_swoole_array_get_value(vht, "bind_address", v))
{
convert_to_string(v);
bind_address = Z_STRVAL_P(v);
}
/**
* bind port
*/
if (php_swoole_array_get_value(vht, "bind_port", v))
{
convert_to_long(v);
bind_port = (int) Z_LVAL_P(v);
}
if (bind_address)
{
swSocket_bind(cli->socket->fd, cli->type, bind_address, &bind_port);
}
/**
* TCP_NODELAY
*/
if (php_swoole_array_get_value(vht, "open_tcp_nodelay", v))
{
value = 1;
if (setsockopt(cli->socket->fd, IPPROTO_TCP, TCP_NODELAY, &value, sizeof(value)) < 0)
{
swSysError("setsockopt(%d, TCP_NODELAY) failed.", cli->socket->fd);
}
}
/**
* socks5 proxy
*/
if (php_swoole_array_get_value(vht, "socks5_host", v))
{
convert_to_string(v);
cli->socks5_proxy = (struct _swSocks5 *) emalloc(sizeof(swSocks5));
bzero(cli->socks5_proxy, sizeof(swSocks5));
cli->socks5_proxy->host = estrdup(Z_STRVAL_P(v));
cli->socks5_proxy->dns_tunnel = 1;
if (php_swoole_array_get_value(vht, "socks5_port", v))
{
convert_to_long(v);
cli->socks5_proxy->port = Z_LVAL_P(v);
}
else
{
swoole_php_fatal_error(E_ERROR, "socks5 proxy require server port option.");
return;
}
if (php_swoole_array_get_value(vht, "socks5_username", v))
{
convert_to_string(v);
cli->socks5_proxy->username = estrdup(Z_STRVAL_P(v));
cli->socks5_proxy->l_username = Z_STRLEN_P(v);
cli->socks5_proxy->method = 0x02;
}
if (php_swoole_array_get_value(vht, "socks5_password", v))
{
convert_to_string(v);
cli->socks5_proxy->password = estrdup(Z_STRVAL_P(v));
cli->socks5_proxy->l_password = Z_STRLEN_P(v);
}
}
if (php_swoole_array_get_value(vht, "http_proxy_host", v))
{
convert_to_string(v);
char *host = Z_STRVAL_P(v);
if (php_swoole_array_get_value(vht, "http_proxy_port", v))
{
convert_to_long(v);
cli->http_proxy = (struct _http_proxy*) ecalloc(1, sizeof(struct _http_proxy));
cli->http_proxy->proxy_host = estrdup(host);
cli->http_proxy->proxy_port = Z_LVAL_P(v);
}
else
{
swoole_php_fatal_error(E_WARNING, "http_proxy_port can not be null.");
}
}
if (php_swoole_array_get_value(vht, "http_proxy_user", v) && cli->http_proxy)
{
convert_to_string(v);
char *user = Z_STRVAL_P(v);
zval *v2;
if (php_swoole_array_get_value(vht, "http_proxy_password", v2))
{
convert_to_string(v);
if (Z_STRLEN_P(v) + Z_STRLEN_P(v2) >= 128 - 1)
{
swoole_php_fatal_error(E_WARNING, "http_proxy user and password is too long.");
}
cli->http_proxy->l_user = Z_STRLEN_P(v);
cli->http_proxy->l_password = Z_STRLEN_P(v2);
cli->http_proxy->user = estrdup(user);
cli->http_proxy->password = estrdup(Z_STRVAL_P(v2));
}
else
{
swoole_php_fatal_error(E_WARNING, "http_proxy_password can not be null.");
}
}
#ifdef SW_USE_OPENSSL
if (cli->open_ssl)
{
php_swoole_client_check_ssl_setting(cli, zset TSRMLS_CC);
}
#endif
}
void php_swoole_at_shutdown(char *function)
{
#if PHP_MAJOR_VERSION >=7
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(&shutdown_function_entry.arguments[0], function);
if (!register_user_shutdown_function(function, ZSTR_LEN(Z_STR(shutdown_function_entry.arguments[0])), &shutdown_function_entry TSRMLS_CC))
{
zval_ptr_dtor(&shutdown_function_entry.arguments[0]);
efree(shutdown_function_entry.arguments);
swoole_php_fatal_error(E_WARNING, "Unable to register shutdown function [%s]",function);
}
#else
zval *callback;
SW_MAKE_STD_ZVAL(callback);
SW_ZVAL_STRING(callback, function, 1);
#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);
shutdown_function_entry.arguments[0] = callback;
if (!register_user_shutdown_function(function, ZSTR_LEN(Z_STR(callback)), &shutdown_function_entry TSRMLS_CC))
{
efree(shutdown_function_entry.arguments);
sw_zval_ptr_dtor(&callback);
swoole_php_fatal_error(E_WARNING, "Unable to register shutdown function [swoole_event_wait]");
}
#else
zval *register_shutdown_function;
zval *retval = NULL;
SW_MAKE_STD_ZVAL(register_shutdown_function);
SW_ZVAL_STRING(register_shutdown_function, "register_shutdown_function", 1);
zval **args[1] = {&callback};
if (sw_call_user_function_ex(EG(function_table), NULL, register_shutdown_function, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "Unable to register shutdown function [swoole_event_wait]");
return;
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
#endif
#endif
}
void php_swoole_check_reactor()
{
if (SwooleWG.reactor_init)
{
return;
}
if (!SWOOLE_G(cli))
{
swoole_php_fatal_error(E_ERROR, "async-io must be used in PHP CLI mode.");
return;
}
if (swIsTaskWorker())
{
swoole_php_fatal_error(E_ERROR, "can't use async-io in task process.");
return;
}
if (SwooleG.main_reactor == NULL)
{
swTraceLog(SW_TRACE_PHP, "init reactor");
SwooleG.main_reactor = (swReactor *) sw_malloc(sizeof(swReactor));
if (SwooleG.main_reactor == NULL)
{
swoole_php_fatal_error(E_ERROR, "malloc failed.");
return;
}
if (swReactor_create(SwooleG.main_reactor, SW_REACTOR_MAXEVENTS) < 0)
{
swoole_php_fatal_error(E_ERROR, "failed to create reactor.");
return;
}
#ifdef SW_COROUTINE
SwooleG.main_reactor->can_exit = php_coroutine_reactor_can_exit;
#endif
//client, swoole_event_exit will set swoole_running = 0
SwooleWG.in_client = 1;
SwooleWG.reactor_wait_onexit = 1;
SwooleWG.reactor_ready = 0;
//only client side
php_swoole_at_shutdown("swoole_event_wait");
}
php_swoole_event_init();
SwooleWG.reactor_init = 1;
}
void php_swoole_client_free(zval *zobject, swClient *cli TSRMLS_DC)
{
if (cli->timer)
{
swTimer_del(&SwooleG.timer, cli->timer);
cli->timer = NULL;
}
//socks5 proxy config
if (cli->socks5_proxy)
{
efree(cli->socks5_proxy->host);
if (cli->socks5_proxy->username)
{
efree(cli->socks5_proxy->username);
}
if (cli->socks5_proxy->password)
{
efree(cli->socks5_proxy->password);
}
efree(cli->socks5_proxy);
}
//http proxy config
if (cli->http_proxy)
{
efree(cli->http_proxy->proxy_host);
if (cli->http_proxy->user)
{
efree(cli->http_proxy->user);
}
if (cli->http_proxy->password)
{
efree(cli->http_proxy->password);
}
efree(cli->http_proxy);
}
if (cli->protocol.private_data)
{
zval *zcallback = (zval *) cli->protocol.private_data;
sw_zval_free(zcallback);
}
//long tcp connection, delete from php_sw_long_connections
if (cli->keep)
{
if (swHashMap_del(php_sw_long_connections, cli->server_str, cli->server_strlen))
{
swoole_php_fatal_error(E_WARNING, "failed to delete key[%s] from hashtable.", cli->server_str);
}
sw_free(cli->server_str);
swClient_free(cli);
pefree(cli, 1);
}
else
{
sw_free(cli->server_str);
swClient_free(cli);
efree(cli);
}
#ifdef SWOOLE_SOCKETS_SUPPORT
zval *zsocket = (zval *) swoole_get_property(zobject, client_property_socket);
if (zsocket)
{
sw_zval_free(zsocket);
swoole_set_property(zobject, client_property_socket, NULL);
}
#endif
//unset object