forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_client.c
1904 lines (1717 loc) · 54.7 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 "ext/standard/basic_functions.h"
typedef struct
{
zval *onConnect;
zval *onReceive;
zval *onClose;
zval *onError;
#ifdef SW_USE_OPENSSL
zval *onSSLReady;
#endif
#if PHP_MAJOR_VERSION >= 7
zval _object;
zval _onConnect;
zval _onReceive;
zval _onClose;
zval _onError;
#ifdef SW_USE_OPENSSL
zval _onSSLReady;
#endif
#endif
} client_callback;
enum client_property
{
client_property_callback = 0,
client_property_socket = 1,
};
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, 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, on);
#ifdef SWOOLE_SOCKETS_SUPPORT
static PHP_METHOD(swoole_client, getSocket);
#endif
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);
static void client_onConnect(swClient *cli);
static void client_onReceive(swClient *cli, char *data, uint32_t length);
static int client_onPackage(swConnection *conn, char *data, uint32_t length);
static void client_onClose(swClient *cli);
static void client_onError(swClient *cli);
static sw_inline void client_execute_callback(zval *zobject, enum php_swoole_client_callback_type type)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
zval *callback = NULL;
zval *retval = NULL;
zval **args[1];
client_callback *cb = swoole_get_property(zobject, 0);
char *callback_name;
switch(type)
{
case SW_CLIENT_CB_onConnect:
callback = cb->onConnect;
callback_name = "onConnect";
break;
case SW_CLIENT_CB_onError:
callback = cb->onError;
callback_name = "onError";
break;
case SW_CLIENT_CB_onClose:
callback = cb->onClose;
callback_name = "onClose";
break;
#ifdef SW_USE_OPENSSL
case SW_CLIENT_CB_onSSLReady:
callback = cb->onSSLReady;
callback_name = "onSSLReady";
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;
if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
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 const zend_function_entry swoole_client_methods[] =
{
PHP_ME(swoole_client, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_client, __destruct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_client, set, NULL, ZEND_ACC_PUBLIC)
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, sendto, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, sleep, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, wakeup, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_client, pause, sleep, NULL, ZEND_ACC_PUBLIC)
PHP_MALIAS(swoole_client, resume, wakeup, NULL, ZEND_ACC_PUBLIC)
#ifdef SW_USE_OPENSSL
PHP_ME(swoole_client, enableSSL, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, getPeerCert, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, verifyPeerCert, NULL, ZEND_ACC_PUBLIC)
#endif
PHP_ME(swoole_client, isConnected, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, getsockname, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, getpeername, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, close, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_client, on, NULL, ZEND_ACC_PUBLIC)
#ifdef SWOOLE_SOCKETS_SUPPORT
PHP_ME(swoole_client, getSocket, NULL, 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_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);
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);
}
static int client_onPackage(swConnection *conn, char *data, uint32_t length)
{
client_onReceive(conn->object, data, length);
return SW_OK;
}
static void client_onReceive(swClient *cli, char *data, uint32_t length)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
zval *zobject = 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 = swoole_get_property(zobject, 0);
zcallback = cb->onReceive;
if (zcallback == NULL || ZVAL_IS_NULL(zcallback))
{
swoole_php_fatal_error(E_WARNING, "swoole_client object have not receive callback.");
goto free_zdata;
}
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
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 = cli->object;
#ifdef SW_USE_OPENSSL
if (cli->ssl_wait_handshake)
{
client_execute_callback(zobject, SW_CLIENT_CB_onSSLReady);
}
else
#endif
{
client_execute_callback(zobject, SW_CLIENT_CB_onConnect);
}
}
static void client_onClose(swClient *cli)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
zval *zobject = cli->object;
if (!cli->released)
{
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)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
zval *zobject = cli->object;
zend_update_property_long(swoole_client_class_entry_ptr, zobject, ZEND_STRL("errCode"), SwooleG.error TSRMLS_CC);
if (!cli->released)
{
php_swoole_client_free(zobject, cli TSRMLS_CC);
}
client_execute_callback(zobject, SW_CLIENT_CB_onError);
sw_zval_ptr_dtor(&zobject);
}
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: check eof
if (php_swoole_array_get_value(vht, "open_eof_split", v) || php_swoole_array_get_value(vht, "open_eof_check", v))
{
convert_to_boolean(v);
cli->open_eof_check = Z_BVAL_P(v);
cli->protocol.split_by_eof = 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));
cli->protocol.onPackage = client_onPackage;
}
//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;
cli->protocol.onPackage = client_onPackage;
}
//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;
}
}
//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);
swSocket_set_buffer_size(cli->socket->fd, value);
cli->socket->buffer_size = cli->buffer_input_size = 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 = emalloc(sizeof(swSocks5));
bzero(cli->socks5_proxy, sizeof(swSocks5));
cli->socks5_proxy->host = strdup(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 = Z_STRVAL_P(v);
cli->socks5_proxy->l_username = Z_STRLEN_P(v);
}
if (php_swoole_array_get_value(vht, "socks5_password", v))
{
convert_to_string(v);
cli->socks5_proxy->password = Z_STRVAL_P(v);
cli->socks5_proxy->l_password = Z_STRLEN_P(v);
}
}
#ifdef SW_USE_OPENSSL
if (php_swoole_array_get_value(vht, "ssl_method", v))
{
convert_to_long(v);
cli->ssl_method = (int) Z_LVAL_P(v);
cli->open_ssl = 1;
}
if (php_swoole_array_get_value(vht, "ssl_compress", v))
{
convert_to_boolean(v);
cli->ssl_disable_compress = !Z_BVAL_P(v);
}
if (php_swoole_array_get_value(vht, "ssl_cert_file", v))
{
convert_to_string(v);
cli->ssl_cert_file = strdup(Z_STRVAL_P(v));
if (access(cli->ssl_cert_file, R_OK) < 0)
{
swoole_php_fatal_error(E_ERROR, "ssl cert file[%s] not found.", cli->ssl_cert_file);
return;
}
cli->open_ssl = 1;
}
if (php_swoole_array_get_value(vht, "ssl_key_file", v))
{
convert_to_string(v);
cli->ssl_key_file = strdup(Z_STRVAL_P(v));
if (access(cli->ssl_key_file, R_OK) < 0)
{
swoole_php_fatal_error(E_ERROR, "ssl key file[%s] not found.", cli->ssl_key_file);
return;
}
}
if (cli->ssl_cert_file && !cli->ssl_key_file)
{
swoole_php_fatal_error(E_ERROR, "ssl require key file.");
return;
}
#endif
}
void php_swoole_at_shutdown(char *function)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
#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], "swoole_event_wait");
if (!register_user_shutdown_function("swoole_event_wait", sizeof("swoole_event_wait")-1, &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 [swoole_event_wait]");
}
#else
zval *callback;
SW_MAKE_STD_ZVAL(callback);
SW_ZVAL_STRING(callback, "swoole_event_wait", 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("swoole_event_wait", sizeof("swoole_event_wait"), &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 PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
if (!SWOOLE_G(cli))
{
swoole_php_fatal_error(E_ERROR, "async-io must use in cli environment.");
return;
}
if (swIsTaskWorker())
{
swoole_php_fatal_error(E_ERROR, "cannot use async-io in task process.");
return;
}
if (SwooleG.main_reactor == NULL)
{
swTraceLog(SW_TRACE_PHP, "init reactor");
SwooleG.main_reactor = 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, "create reactor failed.");
return;
}
//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)
{
//socks5 proxy config
if (cli->socks5_proxy)
{
efree(cli->socks5_proxy);
}
//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, "delete from hashtable failed.");
}
efree(cli->server_str);
swClient_free(cli);
pefree(cli, 1);
}
else
{
efree(cli->server_str);
swClient_free(cli);
efree(cli);
}
//unset object
swoole_set_object(zobject, NULL);
}
swClient* php_swoole_client_new(zval *object, char *host, int host_len, int port)
{
zval *ztype;
int async = 0;
char conn_key[SW_LONG_CONNECTION_KEY_LEN];
int conn_key_len = 0;
uint64_t tmp_buf;
int ret;
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
ztype = sw_zend_read_property(swoole_client_class_entry_ptr, object, SW_STRL("type")-1, 0 TSRMLS_CC);
if (ztype == NULL || ZVAL_IS_NULL(ztype))
{
swoole_php_fatal_error(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;
}
swClient *cli;
bzero(conn_key, SW_LONG_CONNECTION_KEY_LEN);
zval *connection_id = sw_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 = swHashMap_find(php_sw_long_connections, conn_key, conn_key_len);
if (find == NULL)
{
cli = (swClient*) pemalloc(sizeof(swClient), 1);
if (swHashMap_add(php_sw_long_connections, conn_key, conn_key_len, cli) == FAILURE)
{
swoole_php_fatal_error(E_WARNING, "swoole_client_create_socket add to hashtable failed.");
}
goto create_socket;
}
else
{
cli = find;
//try recv, check connection status
ret = recv(cli->socket->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;
}
//clear history data
if (ret > 0)
{
swSocket_clean(cli->socket->fd);
}
}
}
else
{
cli = (swClient*) emalloc(sizeof(swClient));
create_socket:
if (swClient_create(cli, php_swoole_socktype(type), async) < 0)
{
swoole_php_fatal_error(E_WARNING, "swClient_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 = estrdup(conn_key);
cli->server_strlen = conn_key_len;
}
zend_update_property_long(swoole_client_class_entry_ptr, object, ZEND_STRL("sock"), cli->socket->fd TSRMLS_CC);
if (type & SW_FLAG_KEEP)
{
cli->keep = 1;
}
#ifdef SW_USE_OPENSSL
if (type & SW_SOCK_SSL)
{
cli->open_ssl = 1;
}
#endif
return cli;
}
static PHP_METHOD(swoole_client, __construct)
{
long async = 0;
long type = 0;
char *id = NULL;
zend_size_t len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|ls", &type, &async, &id, &len) == FAILURE)
{
swoole_php_fatal_error(E_ERROR, "require socket type param.");
RETURN_FALSE;
}
if (async == 1)
{
type |= SW_FLAG_ASYNC;
}
if ((type & SW_FLAG_ASYNC))
{
if ((type & SW_FLAG_KEEP) && SWOOLE_G(cli))
{
swoole_php_fatal_error(E_ERROR, "The 'SWOOLE_KEEP' flag can only be used in the php-fpm or apache environment.");
}
php_swoole_check_reactor();
}
int client_type = php_swoole_socktype(type);
if (client_type < SW_SOCK_TCP || client_type > SW_SOCK_UNIX_STREAM)
{
swoole_php_fatal_error(E_ERROR, "Unknown client type '%d'.", client_type);
}
zend_update_property_long(swoole_client_class_entry_ptr, getThis(), ZEND_STRL("type"), type TSRMLS_CC);
if (id)
{
zend_update_property_stringl(swoole_client_class_entry_ptr, getThis(), ZEND_STRL("id"), id, len TSRMLS_CC);
}
else
{
zend_update_property_null(swoole_client_class_entry_ptr, getThis(), ZEND_STRL("id") TSRMLS_CC);
}
//init
swoole_set_object(getThis(), NULL);
RETURN_TRUE;
}
static PHP_METHOD(swoole_client, __destruct)
{
swClient *cli = swoole_get_object(getThis());
//no keep connection
if (cli)
{
zval *zobject = getThis();
zval *retval = NULL;
sw_zend_call_method_with_0_params(&zobject, swoole_client_class_entry_ptr, NULL, "close", &retval);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
}
//free memory
client_callback *cb = swoole_get_property(getThis(), client_property_callback);
if (cb)
{
efree(cb);
swoole_set_property(getThis(), client_property_callback, NULL);
}
#ifdef SWOOLE_SOCKETS_SUPPORT
zval *zsocket = swoole_get_property(getThis(), client_property_socket);
if (zsocket)
{
sw_zval_ptr_dtor(&zsocket);
#if PHP_MAJOR_VERSION >= 7
efree(zsocket);
#endif
swoole_set_property(getThis(), client_property_socket, NULL);
}
#endif
}
static PHP_METHOD(swoole_client, set)
{
zval *zset;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zset) == FAILURE)
{
return;
}
zend_update_property(swoole_client_class_entry_ptr, getThis(), ZEND_STRL("setting"), zset TSRMLS_CC);
RETURN_TRUE;
}
static PHP_METHOD(swoole_client, connect)
{
long port = 0, sock_flag = 0;
char *host = NULL;
zend_size_t host_len;
double timeout = SW_CLIENT_DEFAULT_TIMEOUT;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ldl", &host, &host_len, &port, &timeout, &sock_flag) == FAILURE)
{
return;
}
if (host_len <= 0)
{
swoole_php_fatal_error(E_WARNING, "The host is empty.");
RETURN_FALSE;
}
swClient *cli = swoole_get_object(getThis());
if (cli)
{
swoole_php_fatal_error(E_WARNING, "The client is already connected server.");
RETURN_FALSE;
}
cli = php_swoole_client_new(getThis(), host, host_len, port);
if (cli == NULL)
{
RETURN_FALSE;
}
swoole_set_object(getThis(), cli);
if (cli->type == SW_SOCK_TCP || cli->type == SW_SOCK_TCP6)
{
if (port <= 0 || port > SW_CLIENT_MAX_PORT)
{
swoole_php_fatal_error(E_WARNING, "The port is invalid.");
RETURN_FALSE;
}
if (cli->async == 1)
{
//for tcp: nonblock
//for udp: have udp connect
sock_flag = 1;
}
}
if (cli->keep == 1 && cli->socket->active == 1)
{
zend_update_property_bool(swoole_client_class_entry_ptr, getThis(), SW_STRL("reuse")-1, 1 TSRMLS_CC);
RETURN_TRUE;
}
else if (cli->socket->active == 1)
{
swoole_php_fatal_error(E_WARNING, "swoole_client is already connected.");
RETURN_FALSE;
}
zval *zset = sw_zend_read_property(swoole_client_class_entry_ptr, getThis(), ZEND_STRL("setting"), 1 TSRMLS_CC);
if (zset && !ZVAL_IS_NULL(zset))
{
php_swoole_client_check_setting(cli, zset TSRMLS_CC);
}
//nonblock async
if (cli->async)
{
client_callback *cb = swoole_get_property(getThis(), 0);
if (!cb || !cb->onReceive)
{
swoole_php_fatal_error(E_ERROR, "no receive callback.");
RETURN_FALSE;
}
if (swSocket_is_stream(cli->type))
{
cli->onConnect = client_onConnect;
cli->onClose = client_onClose;
cli->onError = client_onError;
cli->onReceive = client_onReceive;
if (!cb->onConnect)
{
swoole_php_fatal_error(E_ERROR, "no connect callback.");
RETURN_FALSE;
}
if (!cb->onError)
{
swoole_php_fatal_error(E_ERROR, "no error callback.");
RETURN_FALSE;
}
if (!cb->onClose)
{
swoole_php_fatal_error(E_ERROR, "no close callback.");
RETURN_FALSE;
}
cli->reactor_fdtype = PHP_SWOOLE_FD_STREAM_CLIENT;
}
else
{
if (cb->onConnect)
{
cli->onConnect = client_onConnect;
}
if (cb->onClose)
{
cli->onClose = client_onClose;
}
cli->onReceive = client_onReceive;
cli->reactor_fdtype = PHP_SWOOLE_FD_DGRAM_CLIENT;
}
zval *zobject = getThis();
cli->object = zobject;
sw_copy_to_stack(cli->object, cb->_object);
sw_zval_add_ref(&zobject);
}
//nonblock async
if (cli->connect(cli, host, port, timeout, sock_flag) < 0)
{
swoole_php_sys_error(E_WARNING, "connect to server[%s:%d] failed.", host, (int )port);
zend_update_property_long(swoole_client_class_entry_ptr, getThis(), SW_STRL("errCode")-1, errno TSRMLS_CC);
RETURN_FALSE;
}
RETURN_TRUE;
}
static PHP_METHOD(swoole_client, send)
{
char *data;
zend_size_t data_len;
long flags = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &flags) == FAILURE)
{
return;
}
if (data_len <= 0)
{
swoole_php_fatal_error(E_WARNING, "data is empty.");
RETURN_FALSE;
}
swClient *cli = swoole_get_object(getThis());
if (!cli)
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_client.");
RETURN_FALSE;
}
if (cli->socket->active == 0)
{
swoole_php_error(E_WARNING, "server is not connected.");
RETURN_FALSE;
}
//clear errno
SwooleG.error = 0;
int ret = cli->send(cli, data, data_len, flags);
if (ret < 0)
{
SwooleG.error = errno;
swoole_php_sys_error(E_WARNING, "send(%d) %d bytes failed.", cli->socket->fd, data_len);
zend_update_property_long(swoole_client_class_entry_ptr, getThis(), SW_STRL("errCode")-1, SwooleG.error TSRMLS_CC);
RETVAL_FALSE;
}
else
{
RETURN_LONG(ret);
}
}
static PHP_METHOD(swoole_client, sendto)
{
char* ip;
zend_size_t ip_len;
long port;
char *data;