2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-25 03:08:27 +08:00

comm_tcp: enable TCP keepalive on host side as well

This commit is contained in:
Sebastien Bourdeauducq 2015-08-15 16:03:00 +08:00
parent 0be0b199c1
commit dee844510c

View File

@ -1,5 +1,6 @@
import logging
import socket
import sys
from artiq.coredevice.comm_generic import CommGeneric
@ -7,6 +8,22 @@ from artiq.coredevice.comm_generic import CommGeneric
logger = logging.getLogger(__name__)
def set_keepalive(sock, after_idle, interval, max_fails):
if sys.platform.startswith("linux"):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, after_idle)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, interval)
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, max_fails)
elif sys.platform.startswith("win") or sys.platform.startswith("cygwin"):
# setting max_fails is not supported, typically ends up being 5 or 10
# depending on Windows version
sock.ioctl(socket.SIO_KEEPALIVE_VALS,
(1, after_idle*1000, interval*1000))
else:
logger.warning("TCP keepalive not supported on platform '%s', ignored",
sys.platform)
class Comm(CommGeneric):
def __init__(self, dmgr, host, port=1381):
self.host = host
@ -16,6 +33,7 @@ class Comm(CommGeneric):
if hasattr(self, "socket"):
return
self.socket = socket.create_connection((self.host, self.port))
set_keepalive(self.socket, 3, 2, 3)
logger.debug("connected to host %s on port %d", self.host, self.port)
self.write(b"ARTIQ coredev\n")