Commit e6d997a9 authored by Pieter Noordhuis's avatar Pieter Noordhuis
Browse files

Extract function to check a socket for errors

parent 026d5ae7
...@@ -117,8 +117,6 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval * ...@@ -117,8 +117,6 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *
struct timeval to; struct timeval to;
struct timeval *toptr = NULL; struct timeval *toptr = NULL;
fd_set wfd; fd_set wfd;
int err;
socklen_t errlen;
/* Only use timeout when not NULL. */ /* Only use timeout when not NULL. */
if (timeout != NULL) { if (timeout != NULL) {
...@@ -143,21 +141,6 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval * ...@@ -143,21 +141,6 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *
return REDIS_ERR; 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; return REDIS_OK;
} }
...@@ -166,6 +149,26 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval * ...@@ -166,6 +149,26 @@ static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *
return REDIS_ERR; 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) { int redisContextSetTimeout(redisContext *c, struct timeval tv) {
if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) { if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) {
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)"); __redisSetErrorFromErrno(c,REDIS_ERR_IO,"setsockopt(SO_RCVTIMEO)");
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#define AF_LOCAL AF_UNIX #define AF_LOCAL AF_UNIX
#endif #endif
int redisCheckSocketError(redisContext *c, int fd);
int redisContextSetTimeout(redisContext *c, struct timeval tv); int redisContextSetTimeout(redisContext *c, struct timeval tv);
int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout); int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout);
int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout); int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment