Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/phpredis/phpredis.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavlo Yatsukhnenko <yatsukhnenko@gmail.com>2022-10-31 16:39:56 +0300
committerMichael Grunder <michael.grunder@gmail.com>2022-11-01 03:01:35 +0300
commit2a0d1c1e6d10d53c3de510cef4310be912bec4c0 (patch)
tree0d68aa7bab611a4e0a652d16648fca5bcaee6eb9 /redis.c
parentcc2383f07666e6afefd7b58995fb607d9967d650 (diff)
Refactor PubSub command
Diffstat (limited to 'redis.c')
-rw-r--r--redis.c111
1 files changed, 1 insertions, 110 deletions
diff --git a/redis.c b/redis.c
index 6d3b6292..b305075f 100644
--- a/redis.c
+++ b/redis.c
@@ -2707,122 +2707,13 @@ PHP_METHOD(Redis, wait) {
REDIS_PROCESS_RESPONSE(redis_long_response);
}
-/* Construct a PUBSUB command */
-PHP_REDIS_API int
-redis_build_pubsub_cmd(RedisSock *redis_sock, char **ret, PUBSUB_TYPE type,
- zval *arg)
-{
- HashTable *ht_chan;
- zval *z_ele;
- smart_string cmd = {0};
-
- if (type == PUBSUB_CHANNELS) {
- if (arg) {
- /* With a pattern */
- return REDIS_SPPRINTF(ret, "PUBSUB", "sk", "CHANNELS", sizeof("CHANNELS") - 1,
- Z_STRVAL_P(arg), Z_STRLEN_P(arg));
- } else {
- /* No pattern */
- return REDIS_SPPRINTF(ret, "PUBSUB", "s", "CHANNELS", sizeof("CHANNELS") - 1);
- }
- } else if (type == PUBSUB_NUMSUB) {
- ht_chan = Z_ARRVAL_P(arg);
-
- // Add PUBSUB and NUMSUB bits
- redis_cmd_init_sstr(&cmd, zend_hash_num_elements(ht_chan)+1, "PUBSUB", sizeof("PUBSUB")-1);
- redis_cmd_append_sstr(&cmd, "NUMSUB", sizeof("NUMSUB")-1);
-
- /* Iterate our elements */
- ZEND_HASH_FOREACH_VAL(ht_chan, z_ele) {
- zend_string *zstr = zval_get_string(z_ele);
- redis_cmd_append_sstr_key(&cmd, ZSTR_VAL(zstr), ZSTR_LEN(zstr), redis_sock, NULL);
- zend_string_release(zstr);
- } ZEND_HASH_FOREACH_END();
-
- /* Set return */
- *ret = cmd.c;
- return cmd.len;
- } else if (type == PUBSUB_NUMPAT) {
- return REDIS_SPPRINTF(ret, "PUBSUB", "s", "NUMPAT", sizeof("NUMPAT") - 1);
- }
-
- /* Shouldn't ever happen */
- return -1;
-}
-
/*
* {{{ proto Redis::pubsub("channels", pattern);
* proto Redis::pubsub("numsub", Array channels);
* proto Redis::pubsub("numpat"); }}}
*/
PHP_METHOD(Redis, pubsub) {
- zval *object;
- RedisSock *redis_sock;
- char *keyword, *cmd;
- int cmd_len;
- size_t kw_len;
- PUBSUB_TYPE type;
- zval *arg = NULL;
-
- // Parse arguments
- if(zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(),
- "Os|z", &object, redis_ce, &keyword,
- &kw_len, &arg)==FAILURE)
- {
- RETURN_FALSE;
- }
-
- /* Validate our sub command keyword, and that we've got proper arguments */
- if(!strncasecmp(keyword, "channels", sizeof("channels"))) {
- /* One (optional) string argument */
- if(arg && Z_TYPE_P(arg) != IS_STRING) {
- RETURN_FALSE;
- }
- type = PUBSUB_CHANNELS;
- } else if(!strncasecmp(keyword, "numsub", sizeof("numsub"))) {
- /* One array argument */
- if(ZEND_NUM_ARGS() < 2 || Z_TYPE_P(arg) != IS_ARRAY ||
- zend_hash_num_elements(Z_ARRVAL_P(arg)) == 0)
- {
- RETURN_FALSE;
- }
- type = PUBSUB_NUMSUB;
- } else if(!strncasecmp(keyword, "numpat", sizeof("numpat"))) {
- type = PUBSUB_NUMPAT;
- } else {
- /* Invalid keyword */
- RETURN_FALSE;
- }
-
- /* Grab our socket context object */
- if ((redis_sock = redis_sock_get(object, 0)) == NULL) {
- RETURN_FALSE;
- }
-
- /* Construct our "PUBSUB" command */
- cmd_len = redis_build_pubsub_cmd(redis_sock, &cmd, type, arg);
-
- REDIS_PROCESS_REQUEST(redis_sock, cmd, cmd_len);
-
- if(type == PUBSUB_NUMSUB) {
- if (IS_ATOMIC(redis_sock)) {
- if(redis_mbulk_reply_zipped_keys_int(INTERNAL_FUNCTION_PARAM_PASSTHRU,
- redis_sock, NULL, NULL) < 0)
- {
- RETURN_FALSE;
- }
- }
- REDIS_PROCESS_RESPONSE(redis_mbulk_reply_zipped_keys_int);
- } else {
- if (IS_ATOMIC(redis_sock)) {
- if(redis_read_variant_reply(INTERNAL_FUNCTION_PARAM_PASSTHRU,
- redis_sock, NULL, NULL) < 0)
- {
- RETURN_FALSE;
- }
- }
- REDIS_PROCESS_RESPONSE(redis_read_variant_reply);
- }
+ REDIS_PROCESS_CMD(pubsub, redis_pubsub_response);
}
/* {{{ proto variant Redis::eval(string script, [array keys, long num_keys]) */