X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=plom_socket_io.py;fp=plom_socket_io.py;h=ebde3c18f5f37af6beeef0ff025593d106ddc1bc;hb=822c68744ead480ba7a48b786148facc8ef1eb04;hp=07d41ce8bd909bb5e53fd23c829e0b1f7685239f;hpb=47dc6843988096102d5424d00c0e411a608792e2;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''