forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_redis_server.c
541 lines (483 loc) · 17.3 KB
/
swoole_redis_server.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
/*
+----------------------------------------------------------------------+
| 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 "redis.h"
#ifdef SW_COROUTINE
#include "swoole_coroutine.h"
#endif
#include "ext/standard/php_string.h"
static zend_class_entry swoole_redis_server_ce;
static zend_class_entry *swoole_redis_server_class_entry_ptr;
static swString *format_buffer;
#ifdef SW_COROUTINE
static struct
{
zend_fcall_info_cache **array;
uint32_t size;
uint32_t count;
} func_cache_array = {NULL, 0, 0};
#endif
static PHP_METHOD(swoole_redis_server, start);
static PHP_METHOD(swoole_redis_server, setHandler);
static PHP_METHOD(swoole_redis_server, format);
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_redis_server_start, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_redis_server_setHandler, 0, 0, 2)
ZEND_ARG_INFO(0, command)
ZEND_ARG_INFO(0, callback)
ZEND_ARG_INFO(0, number_of_string_param)
ZEND_ARG_INFO(0, type_of_array_param)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_swoole_redis_server_format, 0, 0, 1)
ZEND_ARG_INFO(0, type)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
const zend_function_entry swoole_redis_server_methods[] =
{
PHP_ME(swoole_redis_server, start, arginfo_swoole_redis_server_start, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_server, setHandler, arginfo_swoole_redis_server_setHandler, ZEND_ACC_PUBLIC)
PHP_ME(swoole_redis_server, format, arginfo_swoole_redis_server_format, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_FE_END
};
void swoole_redis_server_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_redis_server_ce, "swoole_redis_server", "Swoole\\Redis\\Server", swoole_redis_server_methods);
swoole_redis_server_class_entry_ptr = sw_zend_register_internal_class_ex(&swoole_redis_server_ce, swoole_server_class_entry_ptr, "swoole_server" TSRMLS_CC);
SWOOLE_CLASS_ALIAS(swoole_redis_server, "Swoole\\Redis\\Server");
if (SWOOLE_G(use_shortname))
{
sw_zend_register_class_alias("Co\\Redis\\Server", swoole_redis_server_class_entry_ptr);
}
zend_declare_class_constant_long(swoole_redis_server_class_entry_ptr, SW_STRL("NIL")-1, SW_REDIS_REPLY_NIL TSRMLS_CC);
zend_declare_class_constant_long(swoole_redis_server_class_entry_ptr, SW_STRL("ERROR")-1, SW_REDIS_REPLY_ERROR TSRMLS_CC);
zend_declare_class_constant_long(swoole_redis_server_class_entry_ptr, SW_STRL("STATUS")-1, SW_REDIS_REPLY_STATUS TSRMLS_CC);
zend_declare_class_constant_long(swoole_redis_server_class_entry_ptr, SW_STRL("INT")-1, SW_REDIS_REPLY_INT TSRMLS_CC);
zend_declare_class_constant_long(swoole_redis_server_class_entry_ptr, SW_STRL("STRING")-1, SW_REDIS_REPLY_STRING TSRMLS_CC);
zend_declare_class_constant_long(swoole_redis_server_class_entry_ptr, SW_STRL("SET")-1, SW_REDIS_REPLY_SET TSRMLS_CC);
zend_declare_class_constant_long(swoole_redis_server_class_entry_ptr, SW_STRL("MAP")-1, SW_REDIS_REPLY_MAP TSRMLS_CC);
}
static int redis_onReceive(swServer *serv, swEventData *req)
{
if (swEventData_is_dgram(req->info.type))
{
return php_swoole_onReceive(serv, req);
}
int fd = req->info.fd;
swConnection *conn = swWorker_get_connection(SwooleG.serv, fd);
if (!conn)
{
swWarn("connection[%d] is closed.", fd);
return SW_ERR;
}
swListenPort *port = serv->connection_list[req->info.from_fd].object;
//other server port
if (!port->open_redis_protocol)
{
return php_swoole_onReceive(serv, req);
}
SWOOLE_GET_TSRMLS;
zval *zdata;
SW_MAKE_STD_ZVAL(zdata);
php_swoole_get_recv_data(zdata, req, NULL, 0);
char *p = Z_STRVAL_P(zdata);
char *pe = p + Z_STRLEN_P(zdata);
int ret;
int length = 0;
zval *zparams;
SW_MAKE_STD_ZVAL(zparams);
array_init(zparams);
zval *retval = NULL;
int state = SW_REDIS_RECEIVE_TOTAL_LINE;
int add_param = 0;
char *command = NULL;
int command_len = 0;
do
{
switch (state)
{
case SW_REDIS_RECEIVE_TOTAL_LINE:
if (*p == '*' && (p = swRedis_get_number(p, &ret)))
{
state = SW_REDIS_RECEIVE_LENGTH;
break;
}
/* no break */
case SW_REDIS_RECEIVE_LENGTH:
if (*p == '$' && (p = swRedis_get_number(p, &ret)))
{
if (ret == -1)
{
add_next_index_null(zparams);
break;
}
length = ret;
state = SW_REDIS_RECEIVE_STRING;
break;
}
//integer
else if (*p == ':' && (p = swRedis_get_number(p, &ret)))
{
add_next_index_long(zparams, ret);
break;
}
/* no break */
case SW_REDIS_RECEIVE_STRING:
if (add_param == 0)
{
command = p;
command_len = length;
add_param = 1;
}
else
{
sw_add_next_index_stringl(zparams, p, length, 1);
}
p += length + SW_CRLF_LEN;
state = SW_REDIS_RECEIVE_LENGTH;
break;
default:
break;
}
} while(p < pe);
if (command_len >= SW_REDIS_MAX_COMMAND_SIZE)
{
swoole_php_error(E_WARNING, "command is too long.");
serv->close(serv, fd, 0);
return SW_OK;
}
char _command[SW_REDIS_MAX_COMMAND_SIZE];
command[command_len] = 0;
int _command_len = snprintf(_command, sizeof(_command), "_handler_%*s", command_len, command);
php_strtolower(_command, _command_len);
zval *zobject = serv->ptr2;
char err_msg[256];
zval *zfd;
SW_MAKE_STD_ZVAL(zfd);
ZVAL_LONG(zfd, fd);
#ifndef SW_COROUTINE
zval **args[2];
zval *zcallback = sw_zend_read_property(swoole_redis_server_class_entry_ptr, zobject, _command, _command_len, 1 TSRMLS_CC);
if (!zcallback || ZVAL_IS_NULL(zcallback))
{
length = snprintf(err_msg, sizeof(err_msg), "-ERR unknown command '%*s'\r\n", command_len, command);
swServer_tcp_send(serv, fd, err_msg, length);
return SW_OK;
}
args[0] = &zfd;
args[1] = &zparams;
if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE)
{
swoole_php_error(E_WARNING, "command handler error.");
}
#else
zval *index = sw_zend_read_property(swoole_redis_server_class_entry_ptr, zobject, _command, _command_len, 1 TSRMLS_CC);
if (!index || ZVAL_IS_NULL(index))
{
length = snprintf(err_msg, sizeof(err_msg), "-ERR unknown command '%*s'\r\n", command_len, command);
swServer_tcp_send(serv, fd, err_msg, length);
return SW_OK;
}
zval *args[2];
args[0] = zfd;
args[1] = zparams;
zend_fcall_info_cache *cache = func_cache_array.array[Z_LVAL_P(index)];
if (coro_create(cache, args, 2, &retval, NULL, NULL) != 0)
{
sw_zval_ptr_dtor(&zfd);
sw_zval_ptr_dtor(&zdata);
sw_zval_ptr_dtor(&zparams);
return SW_OK;
}
#endif
if (EG(exception))
{
zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
}
//free the callback return value
if (retval != NULL)
{
if (Z_TYPE_P(retval) == IS_STRING)
{
serv->send(serv, fd, Z_STRVAL_P(retval), Z_STRLEN_P(retval));
}
sw_zval_ptr_dtor(&retval);
}
sw_zval_ptr_dtor(&zfd);
sw_zval_ptr_dtor(&zdata);
sw_zval_ptr_dtor(&zparams);
return SW_OK;
}
static PHP_METHOD(swoole_redis_server, start)
{
int ret;
if (SwooleGS->start > 0)
{
swoole_php_error(E_WARNING, "Server is running. Unable to execute swoole_server::start.");
RETURN_FALSE;
}
swServer *serv = swoole_get_object(getThis());
php_swoole_register_callback(serv);
serv->onReceive = redis_onReceive;
format_buffer = swString_new(SW_BUFFER_SIZE_STD);
if (!format_buffer)
{
swoole_php_fatal_error(E_ERROR, "[1] swString_new(%d) failed.", SW_BUFFER_SIZE_STD);
RETURN_FALSE;
}
zval *zsetting = sw_zend_read_property(swoole_server_class_entry_ptr, getThis(), ZEND_STRL("setting"), 1 TSRMLS_CC);
if (zsetting == NULL || ZVAL_IS_NULL(zsetting))
{
SW_ALLOC_INIT_ZVAL(zsetting);
array_init(zsetting);
zend_update_property(swoole_server_class_entry_ptr, getThis(), ZEND_STRL("setting"), zsetting TSRMLS_CC);
}
#ifdef HT_ALLOW_COW_VIOLATION
HT_ALLOW_COW_VIOLATION(Z_ARRVAL_P(zsetting));
#endif
add_assoc_bool(zsetting, "open_http_protocol", 0);
add_assoc_bool(zsetting, "open_mqtt_protocol", 0);
add_assoc_bool(zsetting, "open_eof_check", 0);
add_assoc_bool(zsetting, "open_length_check", 0);
add_assoc_bool(zsetting, "open_redis_protocol", 0);
serv->listen_list->open_http_protocol = 0;
serv->listen_list->open_mqtt_protocol = 0;
serv->listen_list->open_eof_check = 0;
serv->listen_list->open_length_check = 0;
serv->listen_list->open_redis_protocol = 1;
php_swoole_server_before_start(serv, getThis() TSRMLS_CC);
ret = swServer_start(serv);
if (ret < 0)
{
swoole_php_fatal_error(E_ERROR, "server failed to start. Error: %s", sw_error);
RETURN_LONG(ret);
}
RETURN_TRUE;
}
static PHP_METHOD(swoole_redis_server, setHandler)
{
char *command;
zend_size_t command_len;
zval *zcallback;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &command, &command_len, &zcallback) == FAILURE)
{
return;
}
if (command_len <= 0 || command_len >= SW_REDIS_MAX_COMMAND_SIZE)
{
swoole_php_fatal_error(E_ERROR, "invalid command.");
RETURN_FALSE;
}
#ifdef PHP_SWOOLE_CHECK_CALLBACK
char *func_name = NULL;
#ifdef SW_COROUTINE
if (func_cache_array.array == NULL)
{
func_cache_array.array = ecalloc(32, sizeof(zend_fcall_info_cache *));
func_cache_array.size = 32;
func_cache_array.count = 0;
}
zend_fcall_info_cache *func_cache = emalloc(sizeof(zend_fcall_info_cache));
if (!sw_zend_is_callable_ex(zcallback, NULL, 0, &func_name, NULL, func_cache, NULL TSRMLS_CC))
#else
if (!sw_zend_is_callable(zcallback, 0, &func_name TSRMLS_CC))
#endif
{
swoole_php_fatal_error(E_ERROR, "function '%s' is not callable", func_name);
efree(func_name);
return;
}
efree(func_name);
#endif
char _command[SW_REDIS_MAX_COMMAND_SIZE];
int length = snprintf(_command, sizeof(_command), "_handler_%s", command);
php_strtolower(_command, length);
#ifdef SW_COROUTINE
int func_cache_index = func_cache_array.count;
func_cache_array.array[func_cache_index] = func_cache;
func_cache_array.count++;
if (func_cache_array.count == func_cache_array.size)
{
func_cache_array.size *= 2;
func_cache_array.array = ecalloc(func_cache_array.size, sizeof(zend_fcall_info_cache *));
}
sw_zval_add_ref(&zcallback);
zend_update_property_long(swoole_redis_server_class_entry_ptr, getThis(), _command, length, func_cache_index TSRMLS_CC);
#else
php_strtolower(_command, length);
zend_update_property(swoole_redis_server_class_entry_ptr, getThis(), _command, length, zcallback TSRMLS_CC);
#endif
RETURN_TRUE;
}
static PHP_METHOD(swoole_redis_server, format)
{
long type;
zval *value = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|z", &type, &value) == FAILURE)
{
return;
}
char message[256];
int length;
zval *item;
if (type == SW_REDIS_REPLY_NIL)
{
SW_RETURN_STRINGL(SW_REDIS_RETURN_NIL, sizeof(SW_REDIS_RETURN_NIL)-1, 1);
}
else if (type == SW_REDIS_REPLY_STATUS)
{
if (value)
{
convert_to_string(value);
length = snprintf(message, sizeof(message), "+%*s\r\n", Z_STRLEN_P(value), Z_STRVAL_P(value));
}
else
{
length = snprintf(message, sizeof(message), "+%s\r\n", "OK");
}
SW_RETURN_STRINGL(message, length, 1);
}
else if (type == SW_REDIS_REPLY_ERROR)
{
if (value)
{
convert_to_string(value);
length = snprintf(message, sizeof(message), "-%*s\r\n", Z_STRLEN_P(value), Z_STRVAL_P(value));
}
else
{
length = snprintf(message, sizeof(message), "-%s\r\n", "ERR");
}
SW_RETURN_STRINGL(message, length, 1);
}
else if (type == SW_REDIS_REPLY_INT)
{
if (!value)
{
goto no_value;
}
convert_to_long(value);
length = snprintf(message, sizeof(message), ":%d\r\n", Z_LVAL_P(value));
SW_RETURN_STRINGL(message, length, 1);
}
else if (type == SW_REDIS_REPLY_STRING)
{
if (!value)
{
no_value:
swoole_php_fatal_error(E_WARNING, "require more parameters.");
RETURN_FALSE;
}
convert_to_string(value);
if (Z_STRLEN_P(value) > SW_REDIS_MAX_STRING_SIZE || Z_STRLEN_P(value) < 1)
{
swoole_php_fatal_error(E_WARNING, "invalid string size.");
RETURN_FALSE;
}
swString_clear(format_buffer);
length = snprintf(message, sizeof(message), "$%d\r\n", Z_STRLEN_P(value));
swString_append_ptr(format_buffer, message, length);
swString_append_ptr(format_buffer, Z_STRVAL_P(value), Z_STRLEN_P(value));
swString_append_ptr(format_buffer, SW_CRLF, SW_CRLF_LEN);
SW_RETURN_STRINGL(format_buffer->str, format_buffer->length, 1);
}
else if (type == SW_REDIS_REPLY_SET)
{
if (!value)
{
goto no_value;
}
if (Z_TYPE_P(value) != IS_ARRAY)
{
swoole_php_fatal_error(E_WARNING, "the second parameter should be an array.");
}
swString_clear(format_buffer);
length = snprintf(message, sizeof(message), "*%d\r\n", zend_hash_num_elements(Z_ARRVAL_P(value)));
swString_append_ptr(format_buffer, message, length);
SW_HASHTABLE_FOREACH_START(Z_ARRVAL_P(value), item)
#if PHP_MAJOR_VERSION >= 7
zval _copy;
if (Z_TYPE_P(item) != IS_STRING)
{
_copy = *item;
zval_copy_ctor(&_copy);
item = &_copy;
}
#endif
convert_to_string(item);
length = snprintf(message, sizeof(message), "$%d\r\n", Z_STRLEN_P(item));
swString_append_ptr(format_buffer, message, length);
swString_append_ptr(format_buffer, Z_STRVAL_P(item), Z_STRLEN_P(item));
swString_append_ptr(format_buffer, SW_CRLF, SW_CRLF_LEN);
#if PHP_MAJOR_VERSION >= 7
if (item == &_copy)
{
zval_dtor(item);
}
#endif
SW_HASHTABLE_FOREACH_END();
SW_RETURN_STRINGL(format_buffer->str, format_buffer->length, 1);
}
else if (type == SW_REDIS_REPLY_MAP)
{
if (!value)
{
goto no_value;
}
if (Z_TYPE_P(value) != IS_ARRAY)
{
swoole_php_fatal_error(E_WARNING, "the second parameter should be an array.");
}
swString_clear(format_buffer);
length = snprintf(message, sizeof(message), "*%d\r\n", 2 * zend_hash_num_elements(Z_ARRVAL_P(value)));
swString_append_ptr(format_buffer, message, length);
char *key;
uint32_t keylen;
int keytype;
SW_HASHTABLE_FOREACH_START2(Z_ARRVAL_P(value), key, keylen, keytype, item)
if (key == NULL || keylen <= 0)
{
continue;
}
#if PHP_MAJOR_VERSION >= 7
zval _copy;
if (Z_TYPE_P(item) != IS_STRING)
{
_copy = *item;
zval_copy_ctor(&_copy);
item = &_copy;
}
#endif
convert_to_string(item);
length = snprintf(message, sizeof(message), "$%d\r\n%s\r\n$%d\r\n", keylen, key, Z_STRLEN_P(item));
swString_append_ptr(format_buffer, message, length);
swString_append_ptr(format_buffer, Z_STRVAL_P(item), Z_STRLEN_P(item));
swString_append_ptr(format_buffer, SW_CRLF, SW_CRLF_LEN);
#if PHP_MAJOR_VERSION >= 7
if (item == &_copy)
{
zval_dtor(item);
}
#endif
(void) keytype;
SW_HASHTABLE_FOREACH_END();
SW_RETURN_STRINGL(format_buffer->str, format_buffer->length, 1);
}
else
{
swoole_php_error(E_WARNING, "Unknown type[%ld]", type);
RETURN_FALSE;
}
}