X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=plom_socket_io.py;h=ebde3c18f5f37af6beeef0ff025593d106ddc1bc;hb=2966f4632c6c80b5a1a279a908831150092ad09f;hp=07d41ce8bd909bb5e53fd23c829e0b1f7685239f;hpb=f5287b7235704555925ed2a24113258fe61b40c1;p=plomrogue2-experiments
diff --git a/plom_socket_io.py b/plom_socket_io.py
index 07d41ce..ebde3c1 100644
--- a/plom_socket_io.py
+++ b/plom_socket_io.py
@@ -1,3 +1,7 @@
+class BrokenSocketConnection(Exception):
+ pass
+
+
def send(socket, message):
"""Send message via socket, encoded and delimited the way recv() expects.
@@ -26,9 +30,17 @@ def send(socket, message):
data = escaped_message.encode()
totalsent = 0
while totalsent < len(data):
- sent = socket.send(data[totalsent:])
- if sent == 0:
- raise RuntimeError('socket connection broken')
+ socket_broken = False
+ try:
+ sent = socket.send(data[totalsent:])
+ socket_broken = sent == 0
+ except OSError as err:
+ if err.errno == 9: # "Bad file descriptor", when connection broken
+ socket_broken = True
+ else:
+ raise err
+ if socket_broken:
+ raise BrokenSocketConnection
totalsent = totalsent + sent
@@ -48,7 +60,6 @@ def recv(socket):
meaningful way; that's why we do our own message segmentation with $ as a
delimiter.
"""
- quit = False
esc = False
data = b''
msg = b''