diff --git a/hiredis.c b/hiredis.c index 629f43e..4a30da6 100644 --- a/hiredis.c +++ b/hiredis.c @@ -620,9 +620,9 @@ void redisFree(redisContext *c) { free(c); } -int redisFreeKeepFd(redisContext *c) { - int fd = c->fd; - c->fd = -1; +redisFD redisFreeKeepFd(redisContext *c) { + redisFD fd = c->fd; + c->fd = REDIS_INVALID_FD; redisFree(c); return fd; } @@ -746,7 +746,7 @@ redisContext *redisConnectUnixNonBlock(const char *path) { return redisConnectWithOptions(&options); } -redisContext *redisConnectFd(int fd) { +redisContext *redisConnectFd(redisFD fd) { redisOptions options = {0}; options.type = REDIS_CONN_USERFD; options.endpoint.fd = fd; diff --git a/hiredis.h b/hiredis.h index 4025869..3e138cb 100644 --- a/hiredis.h +++ b/hiredis.h @@ -133,6 +133,9 @@ struct redisSsl; */ #define REDIS_OPT_NOAUTOFREE 0x04 +typedef int redisFD; +#define REDIS_INVALID_FD -1 + typedef struct { /* * the type of connection to use. This also indicates which @@ -155,7 +158,7 @@ typedef struct { /** * use this field to have hiredis operate an already-open * file descriptor */ - int fd; + redisFD fd; } endpoint; } redisOptions; @@ -175,7 +178,7 @@ typedef struct { typedef struct redisContext { int err; /* Error flags, 0 when there is no error */ char errstr[128]; /* String representation of error when applicable */ - int fd; + redisFD fd; int flags; char *obuf; /* Write buffer */ redisReader *reader; /* Protocol reader */ @@ -212,7 +215,7 @@ redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port, redisContext *redisConnectUnix(const char *path); redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv); redisContext *redisConnectUnixNonBlock(const char *path); -redisContext *redisConnectFd(int fd); +redisContext *redisConnectFd(redisFD fd); /** * Secure the connection using SSL. This should be done before any command is @@ -235,7 +238,7 @@ int redisReconnect(redisContext *c); int redisSetTimeout(redisContext *c, const struct timeval tv); int redisEnableKeepAlive(redisContext *c); void redisFree(redisContext *c); -int redisFreeKeepFd(redisContext *c); +redisFD redisFreeKeepFd(redisContext *c); int redisBufferRead(redisContext *c); int redisBufferWrite(redisContext *c, int *done); diff --git a/net.c b/net.c index a1913cd..87b7fbb 100644 --- a/net.c +++ b/net.c @@ -58,9 +58,9 @@ void __redisSetError(redisContext *c, int type, const char *str); void redisNetClose(redisContext *c) { - if (c && c->fd >= 0) { + if (c && c->fd != REDIS_INVALID_FD) { close(c->fd); - c->fd = -1; + c->fd = REDIS_INVALID_FD; } } @@ -117,8 +117,8 @@ static int redisSetReuseAddr(redisContext *c) { } static int redisCreateSocket(redisContext *c, int type) { - int s; - if ((s = socket(type, SOCK_STREAM, 0)) == -1) { + redisFD s; + if ((s = socket(type, SOCK_STREAM, 0)) == REDIS_INVALID_FD) { __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); return REDIS_ERR; } @@ -158,7 +158,7 @@ static int redisSetBlocking(redisContext *c, int blocking) { int redisKeepAlive(redisContext *c, int interval) { int val = 1; - int fd = c->fd; + redisFD fd = c->fd; if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1){ __redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); @@ -322,7 +322,8 @@ int redisContextSetTimeout(redisContext *c, const struct timeval tv) { static int _redisContextConnectTcp(redisContext *c, const char *addr, int port, const struct timeval *timeout, const char *source_addr) { - int s, rv, n; + redisFD s; + int rv, n; char _port[6]; /* strlen("65535"); */ struct addrinfo hints, *servinfo, *bservinfo, *p, *b; int blocking = (c->flags & REDIS_BLOCK); @@ -391,7 +392,7 @@ static int _redisContextConnectTcp(redisContext *c, const char *addr, int port, } for (p = servinfo; p != NULL; p = p->ai_next) { addrretry: - if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1) + if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == REDIS_INVALID_FD) continue; c->fd = s;