forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_client.c
1847 lines (1629 loc) · 50.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 |
| license@swoole.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
+----------------------------------------------------------------------+
*/
#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"
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, 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);
static int client_event_add(zval *sock_array, fd_set *fds, int *max_fd TSRMLS_DC);
static int client_event_loop(zval *sock_array, fd_set *fds TSRMLS_DC);
static int client_close(zval *zobject, int fd TSRMLS_DC);
static int client_onRead(swReactor *reactor, swEvent *event);
static int client_onPackage(zval *zobject, swClient *cli TSRMLS_DC);
static int client_onWrite(swReactor *reactor, swEvent *event);
static int client_onError(swReactor *reactor, swEvent *event);
static void client_check_setting(swClient *cli, zval *zset TSRMLS_DC);
static int client_error_callback(zval *zobject, swEvent *event, int error TSRMLS_DC);
static swClient* client_create_socket(zval *object, char *host, int host_len, int port);
/**
* return the package total length
*/
static int client_get_package_length(swClient *cli, char *data, uint32_t size)
{
uint16_t length_offset = cli->package_length_offset;
uint32_t body_length;
/**
* no have length field, wait more data
*/
if (size < length_offset + cli->package_length_size)
{
return 0;
}
body_length = swoole_unpack(cli->package_length_type, data + length_offset);
//Length error
//Protocol length is not legitimate, out of bounds or exceed the allocated length
if (body_length < 1 || body_length > cli->package_max_length)
{
swWarn("invalid package, remote_addr=%s:%d, length=%d, size=%d.", swConnection_get_ip(cli->socket), swConnection_get_port(cli->socket), body_length, size);
return SW_ERR;
}
//total package length
return cli->package_body_offset + body_length;
}
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,
};
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, 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)
PHP_FE_END
};
HashTable 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)
{
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 client_close(zval *zobject, int fd TSRMLS_DC)
{
zval *zcallback = NULL;
zval *retval;
zval **args[1];
swClient *cli = swoole_get_object(zobject);
if (!cli)
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_client.");
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, "delete from hashtable failed.");
}
sw_free(cli->server_str);
ZVAL_LONG(ztype, 0);
}
else
{
sw_free(cli->server_str);
}
if (cli->buffer && (cli->open_eof_split || cli->open_length_check))
{
swString_free(cli->buffer);
cli->buffer = NULL;
}
//async connection
if (cli->async)
{
//remove from reactor
if (SwooleG.main_reactor)
{
SwooleG.main_reactor->del(SwooleG.main_reactor, fd);
}
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.");
return SW_ERR;
}
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");
return SW_ERR;
}
if (SwooleG.main_reactor->event_num == 0 && SwooleWG.in_client == 1)
{
SwooleG.running = 0;
}
cli->close(cli);
//free the callback return value
if (retval != NULL)
{
zval_ptr_dtor(&retval);
}
}
else
{
cli->close(cli);
}
return SW_OK;
}
static int client_onRead_check_eof(swReactor *reactor, swEvent *event)
{
int n;
zval *zobject;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
zobject = event->socket->object;
swClient *cli = swoole_get_object(zobject);
if (!cli)
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_client.");
return SW_ERR;
}
int recv_again = SW_FALSE;
int eof_pos = -1;
int buf_size;
char stack_buf[SW_PHP_CLIENT_BUFFER_SIZE];
swConnection *conn = cli->socket;
if (cli->buffer == NULL)
{
cli->buffer = swString_new(SW_BUFFER_SIZE);
//alloc memory failed.
if (!cli->buffer)
{
return SW_ERR;
}
}
swString *buffer = cli->buffer;
recv_data:
buf_size = buffer->size - buffer->length;
char *buf_ptr = buffer->str + buffer->length;
if (buf_size > SW_PHP_CLIENT_BUFFER_SIZE)
{
buf_size = SW_PHP_CLIENT_BUFFER_SIZE;
}
#ifdef SW_USE_EPOLLET
n = swRead(event->fd, trunk->data, SW_PHP_CLIENT_BUFFER_SIZE);
#else
//level trigger
n = swConnection_recv(conn, buf_ptr, buf_size, 0);
#endif
swTrace("ReactorThread: recv[len=%d]", n);
if (n < 0)
{
switch (swConnection_error(errno))
{
case SW_ERROR:
swSysError("recv from connection[%d@%d] failed.", event->fd, reactor->id);
return SW_OK;
case SW_CLOSE:
goto close_fd;
default:
return SW_OK;
}
}
else if (n == 0)
{
goto close_fd;
}
else
{
//buffer is full, may have not read data
if (buf_size == n)
{
recv_again = SW_TRUE;
uint32_t extend_size = buffer->size * 2;
if (extend_size > cli->package_max_length)
{
extend_size = cli->package_max_length;
}
if (swString_extend(buffer, extend_size) < 0)
{
return SW_ERR;
}
}
buffer->length += n;
if (buffer->length < cli->package_eof_len)
{
return SW_OK;
}
//find EOF
find_eof:
eof_pos = swoole_strnpos(buffer->str, buffer->length, cli->package_eof, cli->package_eof_len);
if (eof_pos >= 0)
{
int offset = eof_pos + cli->package_eof_len;
if (buffer->length > offset)
{
int remaining_length = buffer->length - offset;
//swWarn("eof_pos=%d, total_length=%d, remaining_length=%d", eof_pos, buffer->length, remaining_length);
memcpy(stack_buf, buffer->str + offset, remaining_length);
buffer->length = offset;
client_onPackage(zobject, cli TSRMLS_CC);
memcpy(buffer->str, stack_buf, remaining_length);
buffer->length = remaining_length;
goto find_eof;
}
else
{
client_onPackage(zobject, cli TSRMLS_CC);
return SW_OK;
}
}
//over max length, will discard
if (buffer->length == cli->package_max_length)
{
swWarn("Package is too big. package_length=%d", (int )buffer->length);
goto close_fd;
}
//no eof
if (recv_again)
{
goto recv_data;
}
}
close_fd:
return client_close(zobject, event->fd TSRMLS_CC);
}
static int client_onRead_check_length(swReactor *reactor, swEvent *event)
{
int n;
zval *zobject;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
zobject = event->socket->object;
swClient *cli = swoole_get_object(zobject);
if (!cli)
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_client.");
return SW_ERR;
}
swConnection *conn = cli->socket;
if (cli->buffer == NULL)
{
cli->buffer = swString_new(SW_BUFFER_SIZE);
//alloc memory failed.
if (!cli->buffer)
{
return SW_ERR;
}
}
swString *buffer = cli->buffer;
uint32_t buf_size;
if (buffer->offset)
{
buf_size = buffer->offset - buffer->length;
}
else
{
buf_size = buffer->size - buffer->length;
}
char *buf_ptr = buffer->str + buffer->length;
n = swConnection_recv(conn, buf_ptr, buf_size, 0);
if (n < 0)
{
switch (swConnection_error(errno))
{
case SW_ERROR:
swSysError("recv from connection[%d@%d] failed.", event->fd, reactor->id);
return SW_OK;
case SW_CLOSE:
goto close_fd;
default:
return SW_OK;
}
}
else if (n == 0)
{
goto close_fd;
}
else
{
buffer->length += n;
while (1)
{
if (cli->wait_data)
{
if (buffer->length == buffer->offset)
{
client_onPackage(zobject, cli TSRMLS_CC);
swString_clear(buffer);
cli->wait_data = 0;
}
return SW_OK;
}
else
{
int package_length = client_get_package_length(cli, buffer->str, buffer->length);
if (package_length == 0)
{
return SW_OK;
}
else if (package_length < 0)
{
goto close_fd;
}
else
{
if (package_length > buffer->size)
{
swString_extend(buffer, package_length);
}
buffer->offset = package_length;
cli->wait_data = 1;
}
}
}
}
close_fd:
return client_close(zobject, event->fd TSRMLS_CC);
}
static int client_onRead(swReactor *reactor, swEvent *event)
{
int n;
zval *zobject, *zcallback = NULL;
zval **args[2];
zval *retval;
char *buf = NULL;
long buf_len = SW_PHP_CLIENT_BUFFER_SIZE;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
zobject = event->socket->object;
swClient *cli = swoole_get_object(zobject);
if (!cli)
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_client.");
return SW_ERR;
}
if (cli->open_eof_split)
{
return client_onRead_check_eof(reactor, event);
}
else if (cli->open_length_check)
{
return client_onRead_check_length(reactor, event);
}
//packet mode
else if (cli->packet_mode == 1)
{
uint32_t len_tmp = 0;
n = recv(event->fd, &len_tmp, 4, 0);
if (n <= 0)
{
return client_close(zobject, event->fd TSRMLS_CC);
}
else
{
buf_len = ntohl(len_tmp);
}
}
buf = emalloc(buf_len + 1);
#ifdef SW_CLIENT_RECV_AGAIN
recv_again:
#endif
n = recv(event->fd, buf, buf_len, 0);
if (n < 0)
{
switch (swConnection_error(errno))
{
case SW_ERROR:
swSysError("Read from socket[%d] failed.", event->fd);
goto free_buf;
case SW_CLOSE:
goto close_fd;
case SW_WAIT:
if (cli->packet_mode == 1)
{
goto recv_again;
}
else
{
goto free_buf;
}
default:
swTrace("default");
goto free_buf;
}
}
else if (n == 0)
{
close_fd:
if (buf)
{
efree(buf);
}
return client_close(zobject, event->fd TSRMLS_CC);
}
else
{
zval *zdata;
MAKE_STD_ZVAL(zdata);
buf[n] = 0;
ZVAL_STRINGL(zdata, buf, n, 0);
args[0] = &zobject;
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 client_onPackage(zval *zobject, swClient *cli TSRMLS_DC)
{
zval *zcallback = NULL;
zval **args[2];
zval *retval;
zval *zdata;
MAKE_STD_ZVAL(zdata);
ZVAL_STRINGL(zdata, cli->buffer->str, cli->buffer->length, 1);
args[0] = &zobject;
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);
}
free_zdata:
zval_ptr_dtor(&zdata);
return SW_OK;
}
static int client_onError(swReactor *reactor, swEvent *event)
{
zval *zobject = event->socket->object;
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_client->onError[2]: getsockopt[sock=%d] failed. Error: %s[%d]", event->fd, strerror(errno), errno);
}
client_error_callback(zobject, event, error TSRMLS_CC);
return SW_OK;
}
static void client_check_setting(swClient *cli, zval *zset TSRMLS_DC)
{
HashTable *vht;
zval **v;
vht = Z_ARRVAL_P(zset);
//buffer: check eof
if (sw_zend_hash_find(vht, ZEND_STRS("open_eof_split"), (void **)&v) == SUCCESS ||
sw_zend_hash_find(vht, ZEND_STRS("open_eof_check"), (void **)&v) == SUCCESS )
{
convert_to_boolean(*v);
cli->open_eof_split = (uint8_t) Z_BVAL_PP(v);
}
//package eof
if (sw_zend_hash_find(vht, ZEND_STRS("package_eof"), (void **) &v) == SUCCESS)
{
convert_to_string(*v);
cli->package_eof_len = Z_STRLEN_PP(v);
if (cli->package_eof_len > SW_DATA_EOF_MAXLEN)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "pacakge_eof max length is %d", SW_DATA_EOF_MAXLEN);
return;
}
bzero(cli->package_eof, SW_DATA_EOF_MAXLEN);
memcpy(cli->package_eof, Z_STRVAL_PP(v), Z_STRLEN_PP(v));
}
//open length check
if (sw_zend_hash_find(vht, ZEND_STRS("open_length_check"), (void **)&v) == SUCCESS)
{
convert_to_long(*v);
cli->open_length_check = (uint8_t) Z_LVAL_PP(v);
}
//package length size
if (sw_zend_hash_find(vht, ZEND_STRS("package_length_type"), (void **)&v) == SUCCESS)
{
convert_to_string(*v);
cli->package_length_type = Z_STRVAL_PP(v)[0];
cli->package_length_size = swoole_type_size(cli->package_length_type);
if (cli->package_length_size == 0)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "unknow package_length_type, see pack(). Link: http://php.net/pack");
return;
}
}
//package length offset
if (sw_zend_hash_find(vht, ZEND_STRS("package_length_offset"), (void **)&v) == SUCCESS)
{
convert_to_long(*v);
cli->package_length_offset = (int)Z_LVAL_PP(v);
}
//package body start
if (sw_zend_hash_find(vht, ZEND_STRS("package_body_offset"), (void **) &v) == SUCCESS
|| sw_zend_hash_find(vht, ZEND_STRS("package_body_start"), (void **) &v) == SUCCESS)
{
convert_to_long(*v);
cli->package_body_offset = (int) Z_LVAL_PP(v);
}
/**
* package max length
*/
if (sw_zend_hash_find(vht, ZEND_STRS("package_max_length"), (void **) &v) == SUCCESS)
{
convert_to_long(*v);
cli->package_max_length = (int) Z_LVAL_PP(v);
}
else
{
cli->package_max_length = SW_BUFFER_INPUT_SIZE;
}
}
static int client_onWrite(swReactor *reactor, swEvent *event)
{
zval *zobject = event->socket->object;
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
swClient *cli = swoole_get_object(zobject);
if (!cli)
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_client.");
return SW_ERR;
}
if (cli->socket->active)
{
return swReactor_onWrite(SwooleG.main_reactor, event);
}
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->socket->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
{
client_error_callback(zobject, event, error TSRMLS_CC);
event->socket->removed = 1;
}
}
return SW_OK;
}
static int client_error_callback(zval *zobject, swEvent *event, int error TSRMLS_DC)
{
zval *zcallback;
zval *retval;
zval **args[1];
if (error != 0)
{
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;
}
void php_swoole_at_shutdown(char *function)
{
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
zval *callback;
MAKE_STD_ZVAL(callback);
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);
zval_ptr_dtor(&callback);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to register shutdown function [swoole_event_wait]");
}
#else
zval *register_shutdown_function;
zval *retval;
MAKE_STD_ZVAL(register_shutdown_function);
ZVAL_STRING(register_shutdown_function, "register_shutdown_function", 1);
zval **args[1] = {&callback};
if (call_user_function_ex(EG(function_table), NULL, register_shutdown_function, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to register shutdown function [swoole_event_wait]");
return;
}
#endif
}
void php_swoole_check_reactor()
{
if (SwooleWG.reactor_init == 0)
{
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
if (!SWOOLE_G(cli))
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "async-io must use in cli environment.");
return;
}
if (swIsTaskWorker())
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "cannot use async-io in task process.");
return;
}
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, "malloc failed.");
return;
}
if (swReactor_create(SwooleG.main_reactor, SW_REACTOR_MAXEVENTS) < 0)
{
php_error_docref(NULL TSRMLS_CC, 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");
}
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, (SW_FD_USER + 1) | SW_EVENT_READ, client_onRead);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, (SW_FD_USER + 1) | SW_EVENT_WRITE, client_onWrite);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, (SW_FD_USER + 1) | SW_EVENT_ERROR, client_onError);
php_swoole_event_init();
SwooleWG.reactor_init = 1;
}
return;
}
static swClient* client_create_socket(zval *object, char *host, int host_len, int port)
{
zval *ztype;
int async = 0;
int packet_mode = 0;
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_tmp = Z_LVAL_P(ztype);
packet_mode = type_tmp & SW_MODE_PACKET;
packet_mode >>= 4;
long type = type_tmp & (~SW_MODE_PACKET);
//debug
//swTrace("type:%d,type_tmp:%d\r\n",type,type_tmp);
//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 = 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->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;
}
}
}
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;
}
swoole_set_object(object, cli);
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;
}
if (packet_mode == 1)
{
cli->packet_mode = 1;
}
return cli;
}
static PHP_METHOD(swoole_client, __construct)
{
long async = 0;
zval *ztype;
char *id = NULL;
int len = 0;