summaryrefslogtreecommitdiff
path: root/src/anet.c
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-10-22 15:23:21 +0200
committerantirez <antirez@gmail.com>2014-10-22 15:23:21 +0200
commit2309f15d89e94babb2e9ef29225a40d822828379 (patch)
treebbd5859e899b60da0a27964a665b6d5ac2fff5cc /src/anet.c
parent456003af25fc7d78867116749a2014611f3e4f64 (diff)
downloadredis-2309f15d89e94babb2e9ef29225a40d822828379.tar.gz
anet.c: new API anetSendTimeout().
Diffstat (limited to 'src/anet.c')
-rw-r--r--src/anet.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/anet.c b/src/anet.c
index 9be32fda4..1e5d85495 100644
--- a/src/anet.c
+++ b/src/anet.c
@@ -34,6 +34,7 @@
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
+#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
@@ -178,6 +179,20 @@ int anetTcpKeepAlive(char *err, int fd)
return ANET_OK;
}
+/* Set the socket send timeout (SO_SNDTIMEO socket option) to the specified
+ * number of milliseconds, or disable it if the 'ms' argument is zero. */
+int anetSendTimeout(char *err, int fd, long long ms) {
+ struct timeval tv;
+
+ tv.tv_sec = ms/1000;
+ tv.tv_usec = (ms%1000)*1000;
+ if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) == -1) {
+ anetSetError(err, "setsockopt SO_SNDTIMEO: %s", strerror(errno));
+ return ANET_ERR;
+ }
+ return ANET_OK;
+}
+
/* anetGenericResolve() is called by anetResolve() and anetResolveIP() to
* do the actual work. It resolves the hostname "host" and set the string
* representation of the IP address into the buffer pointed by "ipbuf".