Skip to content

Commit

Permalink
Merge pull request #84 from SiLab-Bonn/sitcp_timeout
Browse files Browse the repository at this point in the history
ENH: define connection timeout to prevent hanging and set to 1 second
  • Loading branch information
DavidLP authored Jun 8, 2018
2 parents ac5164b + e801965 commit 99f77a3
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions basil/TL/SiTcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,23 @@ def init(self):
raise ValueError('Parameter \'ip\' missing.')
if 'udp_port' not in self._init:
raise ValueError('Parameter \'udp_port\' missing.')

connect_timeout = self._init.get('connect_timeout', 1)

self._sock_udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._sock_udp.settimeout(connect_timeout)
self._sock_udp.connect((self._init['ip'], self._init['udp_port']))
self._sock_udp.settimeout(None) # https://stackoverflow.com/questions/3432102/python-socket-connection-timeout
# using select to monitor socket status, therefore the socket is set to blocking (default)
self._sock_udp.setblocking(0)
# start readout thread if TCP connection is set
if 'tcp_connection' in self._init and self._init['tcp_connection']:
if 'tcp_port' not in self._init:
raise ValueError('Parameter \'tcp_port\' missing.')
self._sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._sock_tcp.settimeout(connect_timeout)
self._sock_tcp.connect((self._init['ip'], self._init['tcp_port']))
self._sock_tcp.settimeout(None) # https://stackoverflow.com/questions/3432102/python-socket-connection-timeout
# using select to monitor socket status, therefore the socket is set to blocking (default)
self._sock_tcp.setblocking(0)
self._tcp_readout_thread = Thread(target=self._tcp_readout, name='TcpReadoutThread', kwargs={})
Expand Down

0 comments on commit 99f77a3

Please sign in to comment.