From ae30d58ff91061e0b0f889f60a9ab2062fa4e9d0 Mon Sep 17 00:00:00 2001 From: Eddy Jansson Date: Mon, 24 Feb 2014 11:41:00 +0100 Subject: Add redisConnectFd() and redisFreeKeepFd() These allows for easier integration of hiredis with external code that wants to manage its fds, say for instance in a pool. Closes #223 --- hiredis.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'hiredis.c') diff --git a/hiredis.c b/hiredis.c index beed8e9..dcb13a5 100644 --- a/hiredis.c +++ b/hiredis.c @@ -1010,6 +1010,13 @@ void redisFree(redisContext *c) { free(c); } +int redisFreeKeepFd(redisContext *c) { + int fd = c->fd; + c->fd = -1; + redisFree(c); + return fd; +} + /* Connect to a Redis instance. On error the field error in the returned * context will be set to the return value of the error function. * When no set of reply functions is given, the default set will be used. */ @@ -1093,6 +1100,18 @@ redisContext *redisConnectUnixNonBlock(const char *path) { return c; } +redisContext *redisConnectFd(int fd) { + redisContext *c; + + c = redisContextInit(); + if (c == NULL) + return NULL; + + c->fd = fd; + c->flags |= REDIS_BLOCK | REDIS_CONNECTED; + return c; +} + /* Set read/write timeout on a blocking socket. */ int redisSetTimeout(redisContext *c, const struct timeval tv) { if (c->flags & REDIS_BLOCK) -- cgit v1.2.3