From 27839e5ecb562d9b79e740e2e20f7a6db7270a66 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 16 Jul 2014 17:47:17 +0200 Subject: [PATCH] Variadic PING with support for Pub/Sub. PING can now be called with an additional arugment, behaving exactly like the ECHO command. PING can now also be called in Pub/Sub mode (with one more more subscriptions to channels / patterns) in order to trigger the delivery of an asynchronous pong message with the optional payload. This fixes issue #420. --- src/redis.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/redis.c b/src/redis.c index 026e2f0e..978e4731 100644 --- a/src/redis.c +++ b/src/redis.c @@ -224,7 +224,7 @@ struct redisCommand redisCommandTable[] = { {"scan",scanCommand,-2,"rR",0,NULL,0,0,0,0,0}, {"dbsize",dbsizeCommand,1,"rF",0,NULL,0,0,0,0,0}, {"auth",authCommand,2,"rsltF",0,NULL,0,0,0,0,0}, - {"ping",pingCommand,1,"rtF",0,NULL,0,0,0,0,0}, + {"ping",pingCommand,-1,"rtF",0,NULL,0,0,0,0,0}, {"echo",echoCommand,2,"rF",0,NULL,0,0,0,0,0}, {"save",saveCommand,1,"ars",0,NULL,0,0,0,0,0}, {"bgsave",bgsaveCommand,1,"ar",0,NULL,0,0,0,0,0}, @@ -2398,8 +2398,28 @@ void authCommand(redisClient *c) { } } +/* The PING command. It works in a different way if the client is in + * in Pub/Sub mode. */ void pingCommand(redisClient *c) { - addReply(c,shared.pong); + /* The command takes zero or one arguments. */ + if (c->argc > 2) { + addReply(c,shared.syntaxerr); + return; + } + + if (c->flags & REDIS_PUBSUB) { + addReply(c,shared.mbulkhdr[2]); + addReplyBulkCBuffer(c,"pong",4); + if (c->argc == 1) + addReplyBulkCBuffer(c,"",0); + else + addReplyBulk(c,c->argv[1]); + } else { + if (c->argc == 1) + addReply(c,shared.pong); + else + addReplyBulk(c,c->argv[1]); + } } void echoCommand(redisClient *c) {