Skip to content

Commit

Permalink
(issue #7) Do some clean up when server sockets close
Browse files Browse the repository at this point in the history
Clients connecting to pyDriveWire over a socket can terminate suddenly.  If
the clients opened Virtual Serial channels those can remain open and may
cause problems when the client reconnects.  The fix adds a callback
mechanism to DWServerSocket and DWSimpleSocket when the connection closes.
The callback is set to the Server's cmdInit method.  This resets all the
open disk files and closes all of the Virtual Serial channels.
  • Loading branch information
n6il committed May 11, 2020
1 parent 72f1be9 commit 4fb0b24
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
19 changes: 14 additions & 5 deletions dwsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ def _read(self, count=256):
(ri, _, _) = select.select([self.conn.fileno()], [], [], 1)
except Exception as e:
print(str(e))
raise Exception("Connection closed")
raise Exception("%s: _read (1): Connection closed" % self)
self._close()
if any(ri):
# print "dwsocket: reading"
data = self.conn.recv(count)
# else:
# print "dwsocket: waiting"
if data == '':
raise Exception("Connection closed")
raise Exception("%s: _read (2): Connection closed" % self)
self._close()
# if data:
# print "r",data
Expand All @@ -73,7 +73,7 @@ def _write(self, data):
(_, wi, _) = select.select([], [self.conn.fileno()], [], 1)
except Exception as e:
print(str(e))
raise ("Connection closed")
raise Exception("%s: _write: Connection closed" % self)
self._close()
n = -1
if any(wi):
Expand Down Expand Up @@ -155,10 +155,11 @@ def _cleanup(self):


class DWSocketServer(DWSocket):
def __init__(self, host='localhost', port=6809, debug=False):
def __init__(self, host='localhost', port=6809, debug=False, closedCb=None):
DWSocket.__init__(self, host=host, port=port, debug=debug)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind(('0.0.0.0', self.port))
self.closedCb = closedCb

def accept(self):
while not self.abort:
Expand Down Expand Up @@ -186,6 +187,9 @@ def _read(self, count=256):
print(str(e))
self.conn.close()
self.conn = None
if self.closedCb:
print("%s: Calling callback: %s" % (self, self.closedCb))
self.closedCb(self)
return data


Expand Down Expand Up @@ -249,7 +253,8 @@ def __init__(
port=6809,
conn=None,
reconnect=False,
debug=False):
debug=False,
closedCb=None):
self.host = host
self.port = int(port)
self.conn = conn
Expand All @@ -262,6 +267,7 @@ def __init__(
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
self.reconnect = reconnect
self.debug = debug
self.closedCb = closedCb

def name(self):
return "%s %s:%s" % (self.__class__, self.host, self.port)
Expand Down Expand Up @@ -295,6 +301,9 @@ def read(self, n=1, timeout=None):
while not self.abort and d == '' and self.reconnect:
print("socket: %s: Disconnected" % (self))
self.close()
if self.closedCb:
print("%s: Calling callback: %s" % (self, self.closedCb))
self.closedCb(self)
print(
"socket: %s: Reconnecting to %s:%s" %
(self, self.host, self.port))
Expand Down
4 changes: 4 additions & 0 deletions pyDriveWire.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import platform
import tempfile

from dwconstants import *

VERSION = 'v0.5c'

defaultConfigValues = {
Expand Down Expand Up @@ -388,6 +390,8 @@ def CreateServer(args, instance, instances, lock):
for cmd in cmds:
print parser.parse(cmd)

if isinstance(conn, DWSocket) or isinstance(conn, DWSimpleSocket):
conn.closedCb = lambda x: dws.cmdInit(OP_INIT)
return dws


Expand Down

0 comments on commit 4fb0b24

Please sign in to comment.