Introduce a redisFD type

The redisFD type should be equal to the system native socket file
desciptor type (for POSIX, this is a plain int).

We also introduce the REDIS_INVALID_FD value, which maps to -1 on POSIX
systems.
This commit is contained in:
m 2019-03-31 18:05:32 +02:00 committed by Marcus Geelnard
parent 1788f41f16
commit e84086cb92
3 changed files with 19 additions and 15 deletions

View File

@ -620,9 +620,9 @@ void redisFree(redisContext *c) {
free(c); free(c);
} }
int redisFreeKeepFd(redisContext *c) { redisFD redisFreeKeepFd(redisContext *c) {
int fd = c->fd; redisFD fd = c->fd;
c->fd = -1; c->fd = REDIS_INVALID_FD;
redisFree(c); redisFree(c);
return fd; return fd;
} }
@ -746,7 +746,7 @@ redisContext *redisConnectUnixNonBlock(const char *path) {
return redisConnectWithOptions(&options); return redisConnectWithOptions(&options);
} }
redisContext *redisConnectFd(int fd) { redisContext *redisConnectFd(redisFD fd) {
redisOptions options = {0}; redisOptions options = {0};
options.type = REDIS_CONN_USERFD; options.type = REDIS_CONN_USERFD;
options.endpoint.fd = fd; options.endpoint.fd = fd;

View File

@ -133,6 +133,9 @@ struct redisSsl;
*/ */
#define REDIS_OPT_NOAUTOFREE 0x04 #define REDIS_OPT_NOAUTOFREE 0x04
typedef int redisFD;
#define REDIS_INVALID_FD -1
typedef struct { typedef struct {
/* /*
* the type of connection to use. This also indicates which * 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 * use this field to have hiredis operate an already-open
* file descriptor */ * file descriptor */
int fd; redisFD fd;
} endpoint; } endpoint;
} redisOptions; } redisOptions;
@ -175,7 +178,7 @@ typedef struct {
typedef struct redisContext { typedef struct redisContext {
int err; /* Error flags, 0 when there is no error */ int err; /* Error flags, 0 when there is no error */
char errstr[128]; /* String representation of error when applicable */ char errstr[128]; /* String representation of error when applicable */
int fd; redisFD fd;
int flags; int flags;
char *obuf; /* Write buffer */ char *obuf; /* Write buffer */
redisReader *reader; /* Protocol reader */ redisReader *reader; /* Protocol reader */
@ -212,7 +215,7 @@ redisContext *redisConnectBindNonBlockWithReuse(const char *ip, int port,
redisContext *redisConnectUnix(const char *path); redisContext *redisConnectUnix(const char *path);
redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv); redisContext *redisConnectUnixWithTimeout(const char *path, const struct timeval tv);
redisContext *redisConnectUnixNonBlock(const char *path); 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 * 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 redisSetTimeout(redisContext *c, const struct timeval tv);
int redisEnableKeepAlive(redisContext *c); int redisEnableKeepAlive(redisContext *c);
void redisFree(redisContext *c); void redisFree(redisContext *c);
int redisFreeKeepFd(redisContext *c); redisFD redisFreeKeepFd(redisContext *c);
int redisBufferRead(redisContext *c); int redisBufferRead(redisContext *c);
int redisBufferWrite(redisContext *c, int *done); int redisBufferWrite(redisContext *c, int *done);

15
net.c
View File

@ -58,9 +58,9 @@
void __redisSetError(redisContext *c, int type, const char *str); void __redisSetError(redisContext *c, int type, const char *str);
void redisNetClose(redisContext *c) { void redisNetClose(redisContext *c) {
if (c && c->fd >= 0) { if (c && c->fd != REDIS_INVALID_FD) {
close(c->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) { static int redisCreateSocket(redisContext *c, int type) {
int s; redisFD s;
if ((s = socket(type, SOCK_STREAM, 0)) == -1) { if ((s = socket(type, SOCK_STREAM, 0)) == REDIS_INVALID_FD) {
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
return REDIS_ERR; return REDIS_ERR;
} }
@ -158,7 +158,7 @@ static int redisSetBlocking(redisContext *c, int blocking) {
int redisKeepAlive(redisContext *c, int interval) { int redisKeepAlive(redisContext *c, int interval) {
int val = 1; int val = 1;
int fd = c->fd; redisFD fd = c->fd;
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1){ if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1){
__redisSetError(c,REDIS_ERR_OTHER,strerror(errno)); __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, static int _redisContextConnectTcp(redisContext *c, const char *addr, int port,
const struct timeval *timeout, const struct timeval *timeout,
const char *source_addr) { const char *source_addr) {
int s, rv, n; redisFD s;
int rv, n;
char _port[6]; /* strlen("65535"); */ char _port[6]; /* strlen("65535"); */
struct addrinfo hints, *servinfo, *bservinfo, *p, *b; struct addrinfo hints, *servinfo, *bservinfo, *p, *b;
int blocking = (c->flags & REDIS_BLOCK); 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) { for (p = servinfo; p != NULL; p = p->ai_next) {
addrretry: 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; continue;
c->fd = s; c->fd = s;