From 39ef561bdb9838e05cdd034fc2c3c964d13adc47 Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 01:22:46 +0100 Subject: [PATCH] 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