From 7a45e7d51ea979180c329c6a8f66936a2074b515 Mon Sep 17 00:00:00 2001 From: klonfish Date: Mon, 26 Dec 2016 23:53:30 +0100 Subject: [PATCH 1/7] Added exception handling on timeout --- wii-pair/wiiboard.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/wii-pair/wiiboard.py b/wii-pair/wiiboard.py index 7be6711..6e3997d 100644 --- a/wii-pair/wiiboard.py +++ b/wii-pair/wiiboard.py @@ -70,9 +70,7 @@ class Wiiboard: try: self.datasocket = bluetooth.BluetoothSocket(bluetooth.L2CAP) - self.datasocket.settimeout(2) self.controlsocket = bluetooth.BluetoothSocket(bluetooth.L2CAP) - self.controlsocket.settimeout(2) except ValueError: raise Exception("Error: Bluetooth not found") @@ -88,7 +86,9 @@ class Wiiboard: print("Non existant address") return self.datasocket.connect((address, 0x13)) + self.datasocket.settimeout(2) self.controlsocket.connect((address, 0x11)) + self.controlsocket.settimeout(2) if self.datasocket and self.controlsocket: print("Connected to Wiiboard at address " + address) self.status = "Connected" @@ -181,7 +181,7 @@ class Wiiboard: # Thread that listens for incoming data def receivethread(self): while self.status == "Connected": - if True: + try: data = self.datasocket.recv(25) intype = data[1] if intype == INPUT_STATUS: @@ -202,6 +202,9 @@ class Wiiboard: else: print("ACK to data write received") + except bluetooth.btcommon.BluetoothError: + print('Error! Disconnecting') + self.disconnect() self.status = "Disconnected" self.disconnect() From 4a9238c110974b4b1cda2efa10e6f1af71aa6f9c Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 00:28:06 +0100 Subject: [PATCH 2/7] Handling of connection reset error --- wii-pair/daemon.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/wii-pair/daemon.py b/wii-pair/daemon.py index f538089..44b9f7e 100755 --- a/wii-pair/daemon.py +++ b/wii-pair/daemon.py @@ -173,26 +173,29 @@ try: conn, addr = s.accept() while True: - m = conn.recv(1) + try: + m = conn.recv(1) - 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) - conn.send(r) + conn.send(r) + except ConnectionResetError: + continue except (KeyboardInterrupt, SystemExit): t1.stop = True t2.stop = True From 2bd6661ef4bfe9dffe7515f429cf8ccf53f38262 Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 00:33:13 +0100 Subject: [PATCH 3/7] Maybe better andling of connection reset error --- wii-pair/daemon.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wii-pair/daemon.py b/wii-pair/daemon.py index 44b9f7e..62cf0f6 100755 --- a/wii-pair/daemon.py +++ b/wii-pair/daemon.py @@ -173,7 +173,6 @@ try: conn, addr = s.accept() while True: - try: m = conn.recv(1) if len(m) == 0: @@ -193,9 +192,10 @@ try: v = int(round(w.pos_y*100)) r += struct.pack('b', v) - conn.send(r) - except ConnectionResetError: - continue + try: + conn.send(r) + except ConnectionResetError: + break except (KeyboardInterrupt, SystemExit): t1.stop = True t2.stop = True From 69b8086996b83c66ae5d39abd105b38564df4b55 Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 00:41:47 +0100 Subject: [PATCH 4/7] 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 From 39ef561bdb9838e05cdd034fc2c3c964d13adc47 Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 01:22:46 +0100 Subject: [PATCH 5/7] Using select for network communication --- wii-pair/daemon.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/wii-pair/daemon.py b/wii-pair/daemon.py index e93e0fe..7bcba47 100755 --- a/wii-pair/daemon.py +++ b/wii-pair/daemon.py @@ -4,6 +4,7 @@ import os, math, random import time import socket import struct +import select from threading import Thread import bluetooth import wiiboard @@ -169,18 +170,25 @@ s.bind(('', 4711)) s.listen(1) try: + conns = [] while True: - conn, addr = s.accept() + rlist, wlist, xlist = select.select([s]+conns, [], []) + if s in rlist: + conn, addr = s.accept() + conns.append(conn) + rlist.remove(s) - while True: + for sock in rlist: try: - m = conn.recv(1) + m = sock.recv(1) except ConnectionResetError: - break + conns.remove(sock) + continue if len(m) == 0: conn.close() - break + conns.remove(sock) + continue r = b'' for w in wiis: @@ -196,9 +204,11 @@ try: r += struct.pack('b', v) try: - conn.send(r) + sock.send(r) except ConnectionResetError: - break + conns.remove(sock) + continue + except (KeyboardInterrupt, SystemExit): t1.stop = True t2.stop = True From 7bb376d5164d5774c16c40d6777f91710312fea7 Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 01:33:02 +0100 Subject: [PATCH 6/7] Using xlist to remove closed sockets --- wii-pair/daemon.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/wii-pair/daemon.py b/wii-pair/daemon.py index 7bcba47..f53481b 100755 --- a/wii-pair/daemon.py +++ b/wii-pair/daemon.py @@ -172,12 +172,15 @@ s.listen(1) try: conns = [] while True: - rlist, wlist, xlist = select.select([s]+conns, [], []) + rlist, wlist, xlist = select.select([s]+conns, [], conns) if s in rlist: conn, addr = s.accept() conns.append(conn) rlist.remove(s) + for sock in xlist: + conns.remove(sock) + for sock in rlist: try: m = sock.recv(1) @@ -185,11 +188,6 @@ try: conns.remove(sock) continue - if len(m) == 0: - conn.close() - conns.remove(sock) - continue - r = b'' for w in wiis: if w.pos_x is None: From 45d4c8aefb9072d74ea89f133ea4f00d41f89f1e Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 01:37:39 +0100 Subject: [PATCH 7/7] Pokemon exception handling for sockets --- wii-pair/daemon.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wii-pair/daemon.py b/wii-pair/daemon.py index f53481b..990e275 100755 --- a/wii-pair/daemon.py +++ b/wii-pair/daemon.py @@ -184,7 +184,7 @@ try: for sock in rlist: try: m = sock.recv(1) - except ConnectionResetError: + except Exception: conns.remove(sock) continue @@ -203,7 +203,7 @@ try: try: sock.send(r) - except ConnectionResetError: + except Exception: conns.remove(sock) continue