Skip to content

Commit

Permalink
Add redisSetReuseAddr(c, fd) static function.
Browse files Browse the repository at this point in the history
Extract setting SO_REUSEADDR socket option into separate function
so the same code can be more easily used by redisCreateSocket and
other functions.
  • Loading branch information
geoffgarside committed Jun 17, 2011
1 parent 5f5b3d9 commit b4664b4
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions net.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,24 @@ static void __redisSetErrorFromErrno(redisContext *c, int type, const char *pref
__redisSetError(c,type,buf);
}

static int redisSetReuseAddr(redisContext *c, int fd) {
int on = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
close(fd);
return REDIS_ERR;
}
return REDIS_OK;
}

static int redisCreateSocket(redisContext *c, int type) {
int s, on = 1;
int s;
if ((s = socket(type, SOCK_STREAM, 0)) == -1) {
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
return REDIS_ERR;
}
if (type == AF_INET) {
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) {
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
close(s);
if (redisSetReuseAddr(c,s) == REDIS_ERR) {
return REDIS_ERR;
}
}
Expand Down

0 comments on commit b4664b4

Please sign in to comment.