forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_redis.c
1036 lines (913 loc) · 32.4 KB
/
swoole_redis.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"
#ifdef SW_USE_REDIS
#include <hiredis/hiredis.h>
#include <hiredis/async.h>
#define SW_REDIS_COMMAND_BUFFER_SIZE 64
#define SW_REDIS_COMMAND_KEY_SIZE 128
typedef struct
{
redisAsyncContext *context;
uint8_t state;
uint8_t connected;
uint8_t subscribe;
uint8_t connecting;
uint32_t reqnum;
zval *object;
zval *message_callback;
double timeout;
swTimer_node *timer;
char *password;
uint8_t password_len;
int8_t database;
uint8_t failure;
uint8_t wait_count;
#if PHP_MAJOR_VERSION >= 7
zval _message_callback;
zval _object;
#endif
} swRedisClient;
enum swoole_redis_state
{
SWOOLE_REDIS_STATE_CONNECT,
SWOOLE_REDIS_STATE_READY,
SWOOLE_REDIS_STATE_WAIT_RESULT,
SWOOLE_REDIS_STATE_SUBSCRIBE,
SWOOLE_REDIS_STATE_CLOSED,
};
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_redis_construct, 0, 0, 0)
ZEND_ARG_ARRAY_INFO(0, setting, 1)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_redis_connect, 0, 0, 3)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, port)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_redis_call, 0, 0, 2)
ZEND_ARG_INFO(0, command)
ZEND_ARG_INFO(0, params)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_redis_on, 0, 0, 2)
ZEND_ARG_INFO(0, event_name)
ZEND_ARG_INFO(0, callback)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_void, 0, 0, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(swoole_redis, __construct);
static PHP_METHOD(swoole_redis, __destruct);
static PHP_METHOD(swoole_redis, on);
static PHP_METHOD(swoole_redis, connect);
static PHP_METHOD(swoole_redis, getState);
static PHP_METHOD(swoole_redis, __call);
static PHP_METHOD(swoole_redis, close);
static void swoole_redis_onConnect(const redisAsyncContext *c, int status);
static void swoole_redis_onClose(const redisAsyncContext *c, int status);
static int swoole_redis_onRead(swReactor *reactor, swEvent *event);
static int swoole_redis_onWrite(swReactor *reactor, swEvent *event);
static int swoole_redis_onError(swReactor *reactor, swEvent *event);
static void swoole_redis_onResult(redisAsyncContext *c, void *r, void *privdata);
static void swoole_redis_parse_result(swRedisClient *redis, zval* return_value, redisReply* reply TSRMLS_DC);
static void swoole_redis_onCompleted(redisAsyncContext *c, void *r, void *privdata);
static void swoole_redis_onTimeout(swTimer *timer, swTimer_node *tnode);
static void swoole_redis_event_AddRead(void *privdata);
static void swoole_redis_event_AddWrite(void *privdata);
static void swoole_redis_event_DelRead(void *privdata);
static void swoole_redis_event_DelWrite(void *privdata);
static void swoole_redis_event_Cleanup(void *privdata);
static zend_class_entry swoole_redis_ce;
static zend_class_entry *swoole_redis_class_entry_ptr;
static const zend_function_entry swoole_redis_methods[] =
{
PHP_ME(swoole_redis, __construct, arginfo_swoole_redis_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_redis, __destruct, arginfo_swoole_void, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_redis, on, arginfo_swoole_redis_on, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis, connect, arginfo_swoole_redis_connect, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis, close, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis, getState, arginfo_swoole_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis, __call, arginfo_swoole_redis_call, ZEND_ACC_PUBLIC)
PHP_FE_END
};
static sw_inline int swoole_redis_is_message_command(char *command, int command_len)
{
if (strncasecmp("subscribe", command, command_len) == 0)
{
return SW_TRUE;
}
else if (strncasecmp("psubscribe", command, command_len) == 0)
{
return SW_TRUE;
}
else if (strncasecmp("unsubscribe", command, command_len) == 0)
{
return SW_TRUE;
}
else if (strncasecmp("punsubscribe", command, command_len) == 0)
{
return SW_TRUE;
}
else
{
return SW_FALSE;
}
}
static sw_inline void redis_execute_connect_callback(swRedisClient *redis, int success TSRMLS_DC)
{
zval *result, *retval;
SW_MAKE_STD_ZVAL(result);
ZVAL_BOOL(result, success);
zval **args[2];
zval *zcallback = sw_zend_read_property(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("onConnect"), 0 TSRMLS_CC);
args[0] = &redis->object;
args[1] = &result;
redis->connecting = 1;
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) != SUCCESS)
{
swoole_php_fatal_error(E_WARNING, "swoole_async_redis connect_callback handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
sw_zval_ptr_dtor(&result);
redis->connecting = 0;
}
void swoole_redis_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_redis_ce, "swoole_redis", "Swoole\\Redis", swoole_redis_methods);
swoole_redis_class_entry_ptr = zend_register_internal_class(&swoole_redis_ce TSRMLS_CC);
SWOOLE_CLASS_ALIAS(swoole_redis, "Swoole\\Redis");
zend_declare_property_null(swoole_redis_class_entry_ptr, ZEND_STRL("onConnect"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_redis_class_entry_ptr, ZEND_STRL("onClose"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_redis_class_entry_ptr, ZEND_STRL("onMessage"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_redis_class_entry_ptr, ZEND_STRL("setting"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_redis_class_entry_ptr, ZEND_STRL("host"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_redis_class_entry_ptr, ZEND_STRL("port"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_redis_class_entry_ptr, ZEND_STRL("sock"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_redis_class_entry_ptr, ZEND_STRL("errCode"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(swoole_redis_class_entry_ptr, ZEND_STRL("errMsg"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_class_constant_long(swoole_redis_class_entry_ptr, SW_STRL("STATE_CONNECT")-1, SWOOLE_REDIS_STATE_CONNECT TSRMLS_CC);
zend_declare_class_constant_long(swoole_redis_class_entry_ptr, SW_STRL("STATE_READY")-1, SWOOLE_REDIS_STATE_READY TSRMLS_CC);
zend_declare_class_constant_long(swoole_redis_class_entry_ptr, SW_STRL("STATE_WAIT_RESULT")-1, SWOOLE_REDIS_STATE_WAIT_RESULT TSRMLS_CC);
zend_declare_class_constant_long(swoole_redis_class_entry_ptr, SW_STRL("STATE_SUBSCRIBE")-1, SWOOLE_REDIS_STATE_SUBSCRIBE TSRMLS_CC);
zend_declare_class_constant_long(swoole_redis_class_entry_ptr, SW_STRL("STATE_CLOSED")-1, SWOOLE_REDIS_STATE_CLOSED TSRMLS_CC);
}
static PHP_METHOD(swoole_redis, __construct)
{
zval *zset = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &zset) == FAILURE)
{
return;
}
swRedisClient *redis = emalloc(sizeof(swRedisClient));
bzero(redis, sizeof(swRedisClient));
redis->object = getThis();
redis->timeout = SW_REDIS_CONNECT_TIMEOUT;
redis->database = -1;
if (zset && !ZVAL_IS_NULL(zset))
{
php_swoole_array_separate(zset);
zend_update_property(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("setting"), zset TSRMLS_CC);
sw_zval_ptr_dtor(&zset);
HashTable *vht;
zval *ztmp;
vht = Z_ARRVAL_P(zset);
/**
* timeout
*/
if (php_swoole_array_get_value(vht, "timeout", ztmp))
{
convert_to_double(ztmp);
redis->timeout = (double) Z_DVAL_P(ztmp);
}
/**
* password
*/
if (php_swoole_array_get_value(vht, "password", ztmp))
{
convert_to_string(ztmp);
if (Z_STRLEN_P(ztmp) >= 1 << 8)
{
swoole_php_fatal_error(E_WARNING, "redis password is too long.");
}
else if (Z_STRLEN_P(ztmp) > 0)
{
redis->password = estrdup(Z_STRVAL_P(ztmp));
redis->password_len = Z_STRLEN_P(ztmp);
}
}
/**
* database
*/
if (php_swoole_array_get_value(vht, "database", ztmp))
{
convert_to_long(ztmp);
if (Z_LVAL_P(ztmp) > 1 << 8)
{
swoole_php_fatal_error(E_WARNING, "redis database number is too big.");
}
else
{
redis->database = (int8_t) Z_LVAL_P(ztmp);
}
}
}
sw_copy_to_stack(redis->object, redis->_object);
swoole_set_object(getThis(), redis);
}
static PHP_METHOD(swoole_redis, on)
{
char *name;
zend_size_t len;
zval *cb;
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "sz", &name, &len, &cb) == FAILURE)
{
return;
}
swRedisClient *redis = swoole_get_object(getThis());
if (redis->context != NULL)
{
swoole_php_fatal_error(E_WARNING, "Must be called before connecting.");
RETURN_FALSE;
}
if (strncasecmp("close", name, len) == 0)
{
zend_update_property(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("onClose"), cb TSRMLS_CC);
}
else if (strncasecmp("message", name, len) == 0)
{
zend_update_property(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("onMessage"), cb TSRMLS_CC);
redis->message_callback = sw_zend_read_property(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("onMessage"), 0 TSRMLS_CC);
sw_copy_to_stack(redis->message_callback, redis->_message_callback);
redis->subscribe = 1;
}
else
{
swoole_php_error(E_WARNING, "Unknown event type[%s]", name);
RETURN_FALSE;
}
RETURN_TRUE;
}
static PHP_METHOD(swoole_redis, connect)
{
char *host;
zend_size_t host_len;
long port;
zval *callback;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slz", &host, &host_len, &port, &callback) == FAILURE)
{
RETURN_FALSE;
}
if (host_len <= 0)
{
swoole_php_error(E_WARNING, "redis server host is empty.");
RETURN_FALSE;
}
swRedisClient *redis = swoole_get_object(getThis());
redisAsyncContext *context;
if (strncasecmp(host, ZEND_STRL("unix:/")) == 0)
{
context = redisAsyncConnectUnix(host + 5);
}
else
{
if (port <= 1 || port > 65535)
{
swoole_php_error(E_WARNING, "redis server port is invalid.");
RETURN_FALSE;
}
context = redisAsyncConnect(host, (int) port);
}
if (context->err)
{
swoole_php_error(E_WARNING, "failed to connect to the redis-server[%s:%d], Erorr: %s[%d]", host, (int) port, context->errstr, context->err);
RETURN_FALSE;
}
php_swoole_check_reactor();
if (!swReactor_handle_isset(SwooleG.main_reactor, PHP_SWOOLE_FD_REDIS))
{
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_REDIS | SW_EVENT_READ, swoole_redis_onRead);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_REDIS | SW_EVENT_WRITE, swoole_redis_onWrite);
SwooleG.main_reactor->setHandle(SwooleG.main_reactor, PHP_SWOOLE_FD_REDIS | SW_EVENT_ERROR, swoole_redis_onError);
}
redisAsyncSetConnectCallback(context, swoole_redis_onConnect);
redisAsyncSetDisconnectCallback(context, swoole_redis_onClose);
zend_update_property_long(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("sock"), context->c.fd TSRMLS_CC);
zend_update_property(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("onConnect"), callback TSRMLS_CC);
redis->context = context;
context->ev.addRead = swoole_redis_event_AddRead;
context->ev.delRead = swoole_redis_event_DelRead;
context->ev.addWrite = swoole_redis_event_AddWrite;
context->ev.delWrite = swoole_redis_event_DelWrite;
context->ev.cleanup = swoole_redis_event_Cleanup;
context->ev.data = redis;
zend_update_property_string(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("host"), host TSRMLS_CC);
zend_update_property_long(swoole_redis_class_entry_ptr, getThis(), ZEND_STRL("port"), port TSRMLS_CC);
if (SwooleG.main_reactor->add(SwooleG.main_reactor, redis->context->c.fd, PHP_SWOOLE_FD_REDIS | SW_EVENT_WRITE) < 0)
{
swoole_php_fatal_error(E_WARNING, "swoole_event_add failed. Erorr: %s[%d].", redis->context->errstr, redis->context->err);
RETURN_FALSE;
}
if (redis->timeout > 0)
{
php_swoole_check_timer((int) (redis->timeout * 1000));
redis->timer = SwooleG.timer.add(&SwooleG.timer, (int) (redis->timeout * 1000), 0, redis, swoole_redis_onTimeout);
}
sw_zval_add_ref(&redis->object);
swConnection *conn = swReactor_get(SwooleG.main_reactor, redis->context->c.fd);
conn->object = redis;
}
static void redis_close(void* data)
{
swRedisClient *redis = data;
redisAsyncDisconnect(redis->context);
}
static void redis_free_object(void *data)
{
zval *object = (zval*) data;
sw_zval_ptr_dtor(&object);
}
static PHP_METHOD(swoole_redis, close)
{
swRedisClient *redis = swoole_get_object(getThis());
if (redis && redis->context && redis->state != SWOOLE_REDIS_STATE_CLOSED)
{
if (redis->connecting)
{
SwooleG.main_reactor->defer(SwooleG.main_reactor, redis_close, redis);
}
else
{
redis_close(redis);
}
}
}
static PHP_METHOD(swoole_redis, __destruct)
{
swRedisClient *redis = swoole_get_object(getThis());
if (redis)
{
if (redis->context && redis->state != SWOOLE_REDIS_STATE_CLOSED)
{
redisAsyncDisconnect(redis->context);
}
if (redis->password)
{
efree(redis->password);
}
efree(redis);
swoole_set_object(getThis(), NULL);
}
}
static PHP_METHOD(swoole_redis, __call)
{
zval *params;
char *command;
zend_size_t command_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &command, &command_len, ¶ms) == FAILURE)
{
return;
}
if (Z_TYPE_P(params) != IS_ARRAY)
{
swoole_php_fatal_error(E_WARNING, "invalid params.");
RETURN_FALSE;
}
swRedisClient *redis = swoole_get_object(getThis());
if (!redis)
{
swoole_php_fatal_error(E_WARNING, "the object is not an instance of swoole_redis.");
RETURN_FALSE;
}
switch (redis->state)
{
case SWOOLE_REDIS_STATE_CONNECT:
swoole_php_error(E_WARNING, "redis client is not connected.");
RETURN_FALSE;
break;
case SWOOLE_REDIS_STATE_WAIT_RESULT:
if (swoole_redis_is_message_command(command, command_len))
{
swoole_php_error(E_WARNING, "redis client is waiting for response.");
RETURN_FALSE;
}
break;
case SWOOLE_REDIS_STATE_SUBSCRIBE:
if (!swoole_redis_is_message_command(command, command_len))
{
swoole_php_error(E_WARNING, "redis client is waiting for subscribed messages.");
RETURN_FALSE;
}
break;
case SWOOLE_REDIS_STATE_CLOSED:
swoole_php_error(E_WARNING, "redis client connection is closed.");
RETURN_FALSE;
break;
default:
break;
}
int argc = zend_hash_num_elements(Z_ARRVAL_P(params));
size_t stack_argvlen[SW_REDIS_COMMAND_BUFFER_SIZE];
char *stack_argv[SW_REDIS_COMMAND_BUFFER_SIZE];
size_t *argvlen;
char **argv;
zend_bool free_mm = 0;
if (argc > SW_REDIS_COMMAND_BUFFER_SIZE)
{
argvlen = emalloc(sizeof(size_t) * argc);
argv = emalloc(sizeof(char*) * argc);
free_mm = 1;
}
else
{
argvlen = stack_argvlen;
argv = stack_argv;
}
#define FREE_MEM() do { \
for (i = 1; i < argc; i++) \
{ \
efree((void* )argv[i]); \
} \
\
if (redis->state == SWOOLE_REDIS_STATE_SUBSCRIBE) \
{ \
efree(argv[argc]); \
} \
\
if (free_mm) \
{ \
efree(argvlen); \
efree(argv); \
} \
} while (0)
assert(command_len < SW_REDIS_COMMAND_KEY_SIZE - 1);
char command_name[SW_REDIS_COMMAND_KEY_SIZE];
memcpy(command_name, command, command_len);
command_name[command_len] = '\0';
argv[0] = command_name;
argvlen[0] = command_len;
zval *value;
int i = 1;
/**
* subscribe command
*/
if (redis->state == SWOOLE_REDIS_STATE_SUBSCRIBE || (redis->subscribe && swoole_redis_is_message_command(command, command_len)))
{
redis->state = SWOOLE_REDIS_STATE_SUBSCRIBE;
SW_HASHTABLE_FOREACH_START(Z_ARRVAL_P(params), value)
convert_to_string(value);
argvlen[i] = (size_t) Z_STRLEN_P(value);
argv[i] = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value));
if (i == argc)
{
break;
}
i++;
SW_HASHTABLE_FOREACH_END();
if (redisAsyncCommandArgv(redis->context, swoole_redis_onResult, NULL, argc + 1, (const char **) argv, (const size_t *) argvlen) < 0)
{
swoole_php_error(E_WARNING, "redisAsyncCommandArgv() failed.");
FREE_MEM();
RETURN_FALSE;
}
}
/**
* storage command
*/
else
{
redis->state = SWOOLE_REDIS_STATE_WAIT_RESULT;
redis->reqnum++;
#if PHP_MAJOR_VERSION < 7
zval *callback;
zval **cb_tmp;
if (zend_hash_index_find(Z_ARRVAL_P(params), zend_hash_num_elements(Z_ARRVAL_P(params)) - 1, (void **) &cb_tmp) == FAILURE)
{
swoole_php_error(E_WARNING, "index out of array bounds.");
FREE_MEM();
RETURN_FALSE;
}
callback = *cb_tmp;
#else
zval *callback = zend_hash_index_find(Z_ARRVAL_P(params), zend_hash_num_elements(Z_ARRVAL_P(params)) - 1);
if (callback == NULL)
{
swoole_php_error(E_WARNING, "index out of array bounds.");
FREE_MEM();
RETURN_FALSE;
}
#endif
sw_zval_add_ref(&callback);
callback = sw_zval_dup(callback);
SW_HASHTABLE_FOREACH_START(Z_ARRVAL_P(params), value)
if (i == argc)
{
break;
}
convert_to_string(value);
argvlen[i] = (size_t) Z_STRLEN_P(value);
argv[i] = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value));
i++;
SW_HASHTABLE_FOREACH_END();
if (redisAsyncCommandArgv(redis->context, swoole_redis_onResult, callback, argc, (const char **) argv, (const size_t *) argvlen) < 0)
{
swoole_php_error(E_WARNING, "redisAsyncCommandArgv() failed.");
FREE_MEM();
RETURN_FALSE;
}
}
FREE_MEM();
RETURN_TRUE;
}
static PHP_METHOD(swoole_redis, getState)
{
swRedisClient *redis = swoole_get_object(getThis());
if (!redis)
{
swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_redis.");
RETURN_FALSE;
}
RETURN_LONG(redis->state);
}
static void swoole_redis_set_error(swRedisClient *redis, zval* return_value, redisReply* reply TSRMLS_DC)
{
char *str = malloc(reply->len + 1);
memcpy(str, reply->str, reply->len);
str[reply->len] = 0;
ZVAL_FALSE(return_value);
zend_update_property_long(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errCode"), -1 TSRMLS_CC);
zend_update_property_string(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), str TSRMLS_CC);
free(str);
}
static void swoole_redis_parse_result(swRedisClient *redis, zval* return_value, redisReply* reply TSRMLS_DC)
{
zval *val;
int j;
#if PHP_MAJOR_VERSION >= 7
zval _val;
val = &_val;
bzero(val, sizeof(zval));
#endif
switch (reply->type)
{
case REDIS_REPLY_INTEGER:
ZVAL_LONG(return_value, reply->integer);
break;
case REDIS_REPLY_ERROR:
swoole_redis_set_error(redis, return_value, reply TSRMLS_CC);
break;
case REDIS_REPLY_STATUS:
if (redis->context->err == 0)
{
if (reply->len > 0)
{
SW_ZVAL_STRINGL(return_value, reply->str, reply->len, 1);
}
else
{
ZVAL_TRUE(return_value);
}
}
else
{
zend_update_property_long(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errCode"), redis->context->err TSRMLS_CC);
zend_update_property_string(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), redis->context->errstr TSRMLS_CC);
}
break;
case REDIS_REPLY_STRING:
SW_ZVAL_STRINGL(return_value, reply->str, reply->len, 1);
break;
case REDIS_REPLY_ARRAY:
array_init(return_value);
for (j = 0; j < reply->elements; j++)
{
#if PHP_MAJOR_VERSION < 7
SW_ALLOC_INIT_ZVAL(val);
#endif
swoole_redis_parse_result(redis, val, reply->element[j] TSRMLS_CC);
add_next_index_zval(return_value, val);
}
break;
case REDIS_REPLY_NIL:
default:
ZVAL_NULL(return_value);
return;
}
}
static void swoole_redis_onTimeout(swTimer *timer, swTimer_node *tnode)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swRedisClient *redis = tnode->data;
zend_update_property_long(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errCode"), ETIMEDOUT TSRMLS_CC);
zend_update_property_string(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), strerror(ETIMEDOUT) TSRMLS_CC);
redis->state = SWOOLE_REDIS_STATE_CLOSED;
redis_execute_connect_callback(redis, 0 TSRMLS_CC);
redisAsyncDisconnect(redis->context);
sw_zval_ptr_dtor(&redis->object);
}
static void swoole_redis_onCompleted(redisAsyncContext *c, void *r, void *privdata)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swRedisClient *redis = c->ev.data;
if (redis->state == SWOOLE_REDIS_STATE_CLOSED)
{
return;
}
if (redis->failure == 0)
{
redisReply *reply = r;
switch (reply->type)
{
case REDIS_REPLY_ERROR:
zend_update_property_long(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errCode"), 0 TSRMLS_CC);
zend_update_property_stringl(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), reply->str,
reply->len TSRMLS_CC);
redis->failure = 1;
break;
case REDIS_REPLY_STATUS:
if (redis->context->err == 0)
{
break;
}
else
{
zend_update_property_long(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errCode"),
redis->context->err TSRMLS_CC);
zend_update_property_string(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errMsg"),
redis->context->errstr TSRMLS_CC);
redis->failure = 1;
}
break;
}
}
redis->wait_count--;
if (redis->wait_count == 0)
{
if (redis->failure)
{
redis_execute_connect_callback(redis, 0 TSRMLS_CC);
redis->connecting = 0;
zval *retval = NULL;
zval *zobject = redis->object;
sw_zend_call_method_with_0_params(&zobject, swoole_redis_class_entry_ptr, NULL, "close", &retval);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
return;
}
else
{
redis_execute_connect_callback(redis, 1 TSRMLS_CC);
}
}
}
static void swoole_redis_onResult(redisAsyncContext *c, void *r, void *privdata)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
redisReply *reply = r;
if (reply == NULL)
{
return;
}
zend_bool is_subscribe = 0;
char *callback_type;
swRedisClient *redis = c->ev.data;
zval *result, *retval, *callback;
SW_MAKE_STD_ZVAL(result);
swoole_redis_parse_result(redis, result, reply TSRMLS_CC);
if (redis->state == SWOOLE_REDIS_STATE_SUBSCRIBE)
{
callback = redis->message_callback;
callback_type = "Message";
is_subscribe = 1;
}
else
{
callback = (zval *)privdata;
callback_type = "Result";
assert(redis->reqnum > 0 && redis->state == SWOOLE_REDIS_STATE_WAIT_RESULT);
redis->reqnum--;
if (redis->reqnum == 0)
{
redis->state = SWOOLE_REDIS_STATE_READY;
}
}
zval **args[2];
args[0] = &redis->object;
args[1] = &result;
if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 2, args, 0, NULL TSRMLS_CC) != SUCCESS)
{
swoole_php_fatal_error(E_WARNING, "swoole_redis callback[%s] handler error.", callback_type);
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
sw_zval_ptr_dtor(&result);
if (!is_subscribe)
{
sw_zval_free(callback);
}
}
void swoole_redis_onConnect(const redisAsyncContext *c, int status)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swRedisClient *redis = c->ev.data;
if (redis->timer)
{
swTimer_del(&SwooleG.timer, redis->timer);
redis->timer = NULL;
}
if (status != REDIS_OK)
{
zend_update_property_long(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errCode"), errno TSRMLS_CC);
zend_update_property_string(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), c->errstr TSRMLS_CC);
redis->state = SWOOLE_REDIS_STATE_CLOSED;
redis_execute_connect_callback(redis, 0 TSRMLS_CC);
SwooleG.main_reactor->defer(SwooleG.main_reactor, redis_free_object, redis->object);
return;
}
else
{
redis->state = SWOOLE_REDIS_STATE_READY;
redis->connected = 1;
}
if (redis->password)
{
redisAsyncCommand((redisAsyncContext *) c, swoole_redis_onCompleted, NULL, "AUTH %b", redis->password, redis->password_len);
redis->wait_count++;
}
if (redis->database >= 0)
{
redisAsyncCommand((redisAsyncContext *) c, swoole_redis_onCompleted, (char*) "end-1", "SELECT %d", redis->database);
redis->wait_count++;
}
if (redis->wait_count == 0)
{
redis_execute_connect_callback(redis, 1 TSRMLS_CC);
}
}
void swoole_redis_onClose(const redisAsyncContext *c, int status)
{
swRedisClient *redis = c->ev.data;
redis->state = SWOOLE_REDIS_STATE_CLOSED;
redis->context = NULL;
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
zval *zcallback = sw_zend_read_property(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("onClose"), 1 TSRMLS_CC);
if (zcallback && !ZVAL_IS_NULL(zcallback))
{
zval *retval;
zval **args[1];
args[0] = &redis->object;
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 1, args, 0, NULL TSRMLS_CC) != SUCCESS)
{
swoole_php_fatal_error(E_WARNING, "swoole_async_redis close_callback handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
}
sw_zval_ptr_dtor(&redis->object);
}
static int swoole_redis_onError(swReactor *reactor, swEvent *event)
{
#if PHP_MAJOR_VERSION < 7
TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL);
#endif
swRedisClient *redis = event->socket->object;
zval *zcallback = sw_zend_read_property(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("onConnect"), 0 TSRMLS_CC);
if (!ZVAL_IS_NULL(zcallback))
{
const redisAsyncContext *c = redis->context;
zval *result;
SW_MAKE_STD_ZVAL(result);
ZVAL_BOOL(result, 0);
zend_update_property_long(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errCode"), c->err TSRMLS_CC);
zend_update_property_string(swoole_redis_class_entry_ptr, redis->object, ZEND_STRL("errMsg"), c->errstr TSRMLS_CC);
redis->state = SWOOLE_REDIS_STATE_CLOSED;
zval *retval = NULL;
zval **args[2];
args[0] = &redis->object;
args[1] = &result;
redis->connecting = 1;
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) != SUCCESS)
{
swoole_php_fatal_error(E_WARNING, "swoole_async_redis connect_callback handler error.");
}
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
if (retval != NULL)
{
sw_zval_ptr_dtor(&retval);
}
sw_zval_ptr_dtor(&result);
redis->connecting = 0;
retval = NULL;
zval *zobject = redis->object;
sw_zend_call_method_with_0_params(&zobject, swoole_redis_class_entry_ptr, NULL, "close", &retval);
if (retval)
{
sw_zval_ptr_dtor(&retval);
}
}
return SW_OK;
}
static void swoole_redis_event_AddRead(void *privdata)
{
swRedisClient *redis = (swRedisClient*) privdata;
if (redis->context && SwooleG.main_reactor)
{
swReactor_add_event(SwooleG.main_reactor, redis->context->c.fd, SW_EVENT_READ);
}
}
static void swoole_redis_event_DelRead(void *privdata)
{
swRedisClient *redis = (swRedisClient*) privdata;
if (redis->context && SwooleG.main_reactor)
{
swReactor_del_event(SwooleG.main_reactor, redis->context->c.fd, SW_EVENT_READ);
}
}
static void swoole_redis_event_AddWrite(void *privdata)
{
swRedisClient *redis = (swRedisClient*) privdata;
if (redis->context && SwooleG.main_reactor)
{
swReactor_add_event(SwooleG.main_reactor, redis->context->c.fd, SW_EVENT_WRITE);
}
}
static void swoole_redis_event_DelWrite(void *privdata)
{
swRedisClient *redis = (swRedisClient*) privdata;
if (redis->context && SwooleG.main_reactor)