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 @@ -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)) @@ -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: @@ -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

Loading…
Cancel
Save