From e6d997a96f9af10f08c9c9e52fb545420650b05b Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Mon, 27 Jun 2011 23:42:18 +0200 Subject: Extract function to check a socket for errors --- net.c | 37 ++++++++++++++++++++----------------- net.h | 1 + 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/net.c b/net.c index 98eee5d..35b6ad2 100644 --- a/net.c +++ b/net.c @@ -117,8 +117,6 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval * struct timeval to; struct timeval *toptr = NULL; fd_set wfd; - int err; - socklen_t errlen; /* Only use timeout when not NULL. */ if (timeout != NULL) { @@ -143,21 +141,6 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval * return REDIS_ERR; } - err = 0; - errlen = sizeof(err); - if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) { - __redisSetErrorFromErrno(c,REDIS_ERR_IO,"getsockopt(SO_ERROR)"); - close(fd); - return REDIS_ERR; - } - - if (err) { - errno = err; - __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); - close(fd); - return REDIS_ERR; - } - return REDIS_OK; } @@ -166,6 +149,26 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval * return REDIS_ERR; } +int redisCheckSocketError(redisContext *c, int fd) { + int err = 0; + socklen_t errlen = sizeof(err); + + if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) { + __redisSetErrorFromErrno(c,REDIS_ERR_IO,"getsockopt(SO_ERROR)"); + close(fd); + return REDIS_ERR; + } + + if (err) { + errno = err; + __redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL); + close(fd); + return REDIS_ERR; + } + + return REDIS_OK; +} + int redisContextSetTimeout(redisContext *c, struct timeval tv) { if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) { __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)"); diff --git a/net.h b/net.h index f9d3755..eb8a0a1 100644 --- a/net.h +++ b/net.h @@ -39,6 +39,7 @@ #define AF_LOCAL AF_UNIX #endif +int redisCheckSocketError(redisContext *c, int fd); int redisContextSetTimeout(redisContext *c, struct timeval tv); int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout); int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout); -- cgit v1.2.3