From f4e0129fa9c127700bd2193b696d9051f391b2ef Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 11 Apr 2016 12:23:04 +0200 Subject: [PATCH] Modules: RedisModule_ReplyWithCallReply(). --- src/module.c | 10 +++++++++- src/modules/API.md | 1 + src/modules/helloworld.c | 19 +++++++++++++++++++ src/redismodule.h | 2 ++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/module.c b/src/module.c index 2dd6c9b63..507c3e6ee 100644 --- a/src/module.c +++ b/src/module.c @@ -87,7 +87,7 @@ typedef struct RedisModuleCommandProxy RedisModuleCommandProxy; /* Reply of RM_Call() function. The function is filled in a lazy * way depending on the function called on the reply structure. By default - * only the type and proto are filled. */ + * only the type, proto and protolen are filled. */ struct RedisModuleCallReply { RedisModuleCtx *ctx; int type; /* REDISMODULE_REPLY_... */ @@ -470,6 +470,13 @@ int RM_ReplyWithNull(RedisModuleCtx *ctx) { return REDISMODULE_OK; } +/* Reply exactly what a Redis command returned us with RM_Call(). */ +int RM_ReplyWithCallReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply) { + sds proto = sdsnewlen(reply->proto, reply->protolen); + addReplySds(ctx->client,proto); + return REDISMODULE_OK; +} + /* -------------------------------------------------------------------------- * Commands replication API * -------------------------------------------------------------------------- */ @@ -1204,6 +1211,7 @@ void moduleRegisterCoreAPI(void) { REGISTER_API(ReplyWithString); REGISTER_API(ReplyWithStringBuffer); REGISTER_API(ReplyWithNull); + REGISTER_API(ReplyWithCallReply); REGISTER_API(GetSelectedDb); REGISTER_API(SelectDb); REGISTER_API(OpenKey); diff --git a/src/modules/API.md b/src/modules/API.md index 3a0903c8f..0dcd3ffac 100644 --- a/src/modules/API.md +++ b/src/modules/API.md @@ -694,3 +694,4 @@ Command implementations, on keys position request, must reply with `REDISMODULE_KEYPOS_OK` to signal the request was processed, otherwise Cluster returns an error for those module commands that are not able to describe the position of keys. + diff --git a/src/modules/helloworld.c b/src/modules/helloworld.c index 09e2f1b24..477a12727 100644 --- a/src/modules/helloworld.c +++ b/src/modules/helloworld.c @@ -51,6 +51,21 @@ int HelloPushCall_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, in return REDISMODULE_OK; } +/* HELLO.PUSH.CALL2 + * This is exaxctly as HELLO.PUSH.CALL, but shows how we can reply to the + * client using directly a reply object that Call() returned. */ +int HelloPushCall2_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) +{ + if (argc != 3) return RedisModule_WrongArity(ctx); + + RedisModuleCallReply *reply; + + reply = RedisModule_Call(ctx,"RPUSH","ss",argv[1],argv[2]); + RedisModule_ReplyWithCallReply(ctx,reply); + RedisModule_FreeCallReply(reply); + return REDISMODULE_OK; +} + /* HELLO.LIST.SUM.LEN returns the total length of all the items inside * a Redis list, by using the high level Call() API. * This command is an example of the array reply access. */ @@ -306,6 +321,10 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx) { HelloPushCall_RedisCommand) == REDISMODULE_ERR) return REDISMODULE_ERR; + if (RedisModule_CreateCommand(ctx,"hello.push.call2", + HelloPushCall2_RedisCommand) == REDISMODULE_ERR) + return REDISMODULE_ERR; + if (RedisModule_CreateCommand(ctx,"hello.list.sum.len", HelloListSumLen_RedisCommand) == REDISMODULE_ERR) return REDISMODULE_ERR; diff --git a/src/redismodule.h b/src/redismodule.h index 4daba2a4d..a4d427396 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -86,6 +86,7 @@ int REDISMODULE_API_FUNC(RedisModule_ReplyWithArray)(RedisModuleCtx *ctx, int le int REDISMODULE_API_FUNC(RedisModule_ReplyWithStringBuffer)(RedisModuleCtx *ctx, const char *buf, size_t len); int REDISMODULE_API_FUNC(RedisModule_ReplyWithString)(RedisModuleCtx *ctx, RedisModuleString *str); int REDISMODULE_API_FUNC(RedisModule_ReplyWithNull)(RedisModuleCtx *ctx); +int REDISMODULE_API_FUNC(RedisModule_ReplyWithCallReply)(RedisModuleCtx *ctx, RedisModuleCallReply *reply); int REDISMODULE_API_FUNC(RedisModule_StringToLongLong)(RedisModuleString *str, long long *ll); void REDISMODULE_API_FUNC(RedisModule_AutoMemory)(RedisModuleCtx *ctx); int REDISMODULE_API_FUNC(RedisModule_Replicate)(RedisModuleCtx *ctx, const char *cmdname, const char *fmt, ...); @@ -111,6 +112,7 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int REDISMODULE_GET_API(ReplyWithStringBuffer); REDISMODULE_GET_API(ReplyWithString); REDISMODULE_GET_API(ReplyWithNull); + REDISMODULE_GET_API(ReplyWithCallReply); REDISMODULE_GET_API(GetSelectedDb); REDISMODULE_GET_API(SelectDb); REDISMODULE_GET_API(OpenKey); -- GitLab