corrected the TCP keepalive code for WinSock
This commit is contained in:
Binary file not shown.
+23
-8
@@ -49,6 +49,7 @@
|
||||
#ifdef _WIN32
|
||||
#include "win32_Interop/win32fixes.h"
|
||||
#define ANET_NOTUSED(V) V
|
||||
#include <Mstcpip.h>
|
||||
#endif
|
||||
|
||||
#include "anet.h"
|
||||
@@ -81,20 +82,34 @@ int anetNonBlock(char *err, int fd)
|
||||
return ANET_OK;
|
||||
}
|
||||
|
||||
/* Set TCP keep alive option to detect dead peers. The interval option
|
||||
* is only used for Linux as we are using Linux-specific APIs to set
|
||||
* the probe send time, interval, and count. */
|
||||
int anetKeepAlive(char *err, int fd, int interval)
|
||||
{
|
||||
/* Set TCP keep alive option to detect dead peers. */
|
||||
int anetKeepAlive(char *err, int fd, int interval) {
|
||||
#ifdef _WIN32
|
||||
int val = 1;
|
||||
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1)
|
||||
{
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1) {
|
||||
anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno));
|
||||
return ANET_ERR;
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
struct tcp_keepalive alive;
|
||||
DWORD dwBytesRet = 0;
|
||||
alive.onoff = TRUE;
|
||||
alive.keepalivetime = interval * 1000;
|
||||
/* According to http://msdn.microsoft.com/en-us/library/windows/desktop/ee470551(v=vs.85).aspx
|
||||
On Windows Vista and later, the number of keep-alive probes (data retransmissions) is set to 10 and cannot be changed.
|
||||
So we set the keep alive interval as interval/10, as 10 probes will be send before
|
||||
detecting an error
|
||||
*/
|
||||
val = interval/10;
|
||||
if (val == 0) val = 1;
|
||||
alive.keepaliveinterval = val*1000;
|
||||
if(WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
|
||||
NULL, 0, &dwBytesRet, NULL, NULL) == SOCKET_ERROR) {
|
||||
anetSetError(err, "WSAIotcl(SIO_KEEPALIVE_VALS) failed with error code %d\n", WSAGetLastError());
|
||||
return ANET_ERR;
|
||||
}
|
||||
#else
|
||||
/* Default settings are more or less garbage, with the keepalive time
|
||||
* set to 7200 by default on Linux. Modify settings to make the feature
|
||||
* actually useful. */
|
||||
|
||||
Reference in New Issue
Block a user