From e15030cc6e952225b3388915e90950788784c481 Mon Sep 17 00:00:00 2001 From: BreakWa11 Date: Mon, 1 Aug 2016 13:19:24 +0800 Subject: [PATCH] set KEEPIDLE on windows/mac osx --- shadowsocks/tcprelay.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/shadowsocks/tcprelay.py b/shadowsocks/tcprelay.py index a167e43..e118a4b 100644 --- a/shadowsocks/tcprelay.py +++ b/shadowsocks/tcprelay.py @@ -565,10 +565,21 @@ class TCPRelayHandler(object): else: remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) remote_sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) + if self._is_local: + val_idle = 60 + val_intvl = 5 + else: + val_idle = 120 + val_intvl = 20 if platform.system() in ['Linux']: - remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 120) - remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 20) + remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, val_idle) + remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, val_intvl) remote_sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 5) + elif platform.system() in ['Windows']: + remote_sock.ioctl(socket.SIO_KEEPALIVE_VALS, (1, 1000 * val_idle, 1000 * val_intvl)) + elif platform.system() in ["Darwin"]: #OSX + TCP_KEEPALIVE = 0x10 + sock.setsockopt(socket.SOL_TCP, TCP_KEEPALIVE, val_intvl * 2) if not self._is_local: bind_addr = ''