From 69b8086996b83c66ae5d39abd105b38564df4b55 Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 00:41:47 +0100 Subject: [PATCH] Maybe even better andling of connection reset error --- wii-pair/daemon.py | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/wii-pair/daemon.py b/wii-pair/daemon.py index 62cf0f6..e93e0fe 100755 --- a/wii-pair/daemon.py +++ b/wii-pair/daemon.py @@ -173,29 +173,32 @@ try: conn, addr = s.accept() while True: + try: m = conn.recv(1) + except ConnectionResetError: + break - if len(m) == 0: - conn.close() - break + if len(m) == 0: + conn.close() + break - r = b'' - for w in wiis: - if w.pos_x is None: - v = -128 - else: - v = int(round(w.pos_x*100)) - r += struct.pack('b', v) - if w.pos_y is None: - v = -128 - else: - v = int(round(w.pos_y*100)) - r += struct.pack('b', v) + r = b'' + for w in wiis: + if w.pos_x is None: + v = -128 + else: + v = int(round(w.pos_x*100)) + r += struct.pack('b', v) + if w.pos_y is None: + v = -128 + else: + v = int(round(w.pos_y*100)) + r += struct.pack('b', v) - try: - conn.send(r) - except ConnectionResetError: - break + try: + conn.send(r) + except ConnectionResetError: + break except (KeyboardInterrupt, SystemExit): t1.stop = True t2.stop = True