Browse Source

Using select for network communication

master
klonfish 8 years ago committed by klonfish
parent
commit
39ef561bdb
  1. 24
      wii-pair/daemon.py

24
wii-pair/daemon.py

@ -4,6 +4,7 @@ import os, math, random
import time import time
import socket import socket
import struct import struct
import select
from threading import Thread from threading import Thread
import bluetooth import bluetooth
import wiiboard import wiiboard
@ -169,18 +170,25 @@ s.bind(('', 4711))
s.listen(1) s.listen(1)
try: try:
conns = []
while True: 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: try:
m = conn.recv(1) m = sock.recv(1)
except ConnectionResetError: except ConnectionResetError:
break conns.remove(sock)
continue
if len(m) == 0: if len(m) == 0:
conn.close() conn.close()
break conns.remove(sock)
continue
r = b'' r = b''
for w in wiis: for w in wiis:
@ -196,9 +204,11 @@ try:
r += struct.pack('b', v) r += struct.pack('b', v)
try: try:
conn.send(r) sock.send(r)
except ConnectionResetError: except ConnectionResetError:
break conns.remove(sock)
continue
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
t1.stop = True t1.stop = True
t2.stop = True t2.stop = True

Loading…
Cancel
Save