Add anetSetReuseAddr(err, fd) static function.

Extract setting SO_REUSEADDR socket option into separate function
so the same code can be more easily used by anetCreateSocket and
other functions.
This commit is contained in:
Geoff Garside 2011-06-17 00:55:00 +01:00 committed by antirez
parent 13c44e7b07
commit ac940caf4f

View File

@ -186,8 +186,19 @@ int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len)
return ANET_OK; return ANET_OK;
} }
static int anetSetReuseAddr(char *err, int fd) {
int yes = 1;
/* Make sure connection-intensive things like the redis benckmark
* will be able to close/open sockets a zillion of times */
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno));
return ANET_ERR;
}
return ANET_OK;
}
static int anetCreateSocket(char *err, int domain) { static int anetCreateSocket(char *err, int domain) {
int s, on = 1; int s;
if ((s = socket(domain, SOCK_STREAM, 0)) == -1) { if ((s = socket(domain, SOCK_STREAM, 0)) == -1) {
anetSetError(err, "creating socket: %s", strerror(errno)); anetSetError(err, "creating socket: %s", strerror(errno));
return ANET_ERR; return ANET_ERR;
@ -195,8 +206,8 @@ static int anetCreateSocket(char *err, int domain) {
/* Make sure connection-intensive things like the redis benchmark /* Make sure connection-intensive things like the redis benchmark
* will be able to close/open sockets a zillion of times */ * will be able to close/open sockets a zillion of times */
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) { if (anetSetReuseAddr(err,s) == ANET_ERR) {
anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno)); close(s);
return ANET_ERR; return ANET_ERR;
} }
return s; return s;