Mercurial > touhou
comparison pytouhou/network.py @ 487:711c75115675
Various netplay-related fixes.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sat, 28 Sep 2013 20:11:41 +0200 |
parents | c099802e2435 |
children | d1f0bb0b7a17 |
comparison
equal
deleted
inserted
replaced
486:2f53be1b2f60 | 487:711c75115675 |
---|---|
1 import socket | 1 import socket |
2 import struct | 2 from struct import Struct |
3 from select import select | 3 from select import select |
4 import time | 4 from time import time |
5 | 5 |
6 MSG_STRUCT = struct.Struct('!HHH') | 6 from pytouhou.utils.helpers import get_logger |
7 | |
8 logger = get_logger(__name__) | |
9 | |
10 MSG_STRUCT = Struct('!HHH') | |
7 | 11 |
8 class Network(object): | 12 class Network(object): |
9 def __init__(self, port=8080, dest=None, selected_player=0): | 13 def __init__(self, port=8080, dest=None, selected_player=0): |
10 self.frame = 0 | 14 self.frame = 0 |
11 self.keystate = 0 | 15 self.keystate = 0 |
20 | 24 |
21 | 25 |
22 def read_message(self): | 26 def read_message(self): |
23 message = None | 27 message = None |
24 | 28 |
25 start_time = time.time() | 29 start_time = time() |
26 delta = 1./60. | 30 delta = 1./60. |
27 | 31 |
28 rlist, wlist, xlist = select([self.sock], [], [], delta) | 32 rlist, _, _ = select([self.sock], [], [], delta) |
29 while rlist: | 33 while rlist: |
30 msg, addr = rlist[0].recvfrom(MSG_STRUCT.size) | 34 msg, addr = rlist[0].recvfrom(MSG_STRUCT.size) |
31 # Check whether the message comes from the right address | 35 # Check whether the message comes from the right address |
32 if self.frame == 0 or addr == self.remote_addr: | 36 if self.frame == 0 or addr == self.remote_addr: |
33 self.remote_addr = addr | 37 self.remote_addr = addr |
36 | 40 |
37 # Check for well-formedness | 41 # Check for well-formedness |
38 if frame in (self.frame, self.frame + 1): | 42 if frame in (self.frame, self.frame + 1): |
39 message = (frame, keystate, old_keystate) | 43 message = (frame, keystate, old_keystate) |
40 else: | 44 else: |
41 print('Mismatch', self.remote_addr, addr) | 45 logger.error('Mismatch, got a message from %s, waiting for %s.', self.remote_addr, addr) |
42 | 46 |
43 # If no valid message has been read, wait for one as long as possible | 47 # If no valid message has been read, wait for one as long as possible |
44 # else, read as much as we can without blocking. | 48 # else, read as much as we can without blocking. |
45 delta = 0 if message else max(0, 1./60. - (time.time() - start_time)) | 49 delta = 0 if message else max(0, 1./60. - (time() - start_time)) |
46 rlist, wlist, xlist = select(rlist, [], [], delta) | 50 rlist, _, _ = select(rlist, [], [], delta) |
47 | 51 |
48 return message | 52 return message |
49 | 53 |
50 | 54 |
51 def send_message(self): | 55 def send_message(self): |
52 frame, keystate, old_keystate = self.frame, self.keystate, self.old_keystate | |
53 if self.remote_addr is not None: | 56 if self.remote_addr is not None: |
54 self.sock.sendto(MSG_STRUCT.pack(frame, keystate, old_keystate), self.remote_addr) | 57 self.sock.sendto(MSG_STRUCT.pack(self.frame, self.keystate, self.old_keystate), self.remote_addr) |
55 | 58 |
56 | 59 |
57 def run_game_iter(self, game, keystate, other_keystate): | 60 def run_game_iter(self, game, keystate, other_keystate): |
58 keystates = [other_keystate, other_keystate] | 61 keystates = [other_keystate, other_keystate] |
59 keystates[self.selected_player] = keystate | 62 keystates[self.selected_player] = keystate |
83 self.remote_keystate = old_keystate | 86 self.remote_keystate = old_keystate |
84 else: | 87 else: |
85 raise Exception #TODO | 88 raise Exception #TODO |
86 self.run_game_iter(game, self.keystate, self.remote_keystate) | 89 self.run_game_iter(game, self.keystate, self.remote_keystate) |
87 elif game.frame > 2: | 90 elif game.frame > 2: |
88 print('ARGH') | 91 logger.warn('Message not received in time, dropping frame.') |
89 | 92 |
90 | 93 |