Mercurial > touhou
comparison pytouhou/network.py @ 484:699c8e8c7738
Don't stick with the remote user unless the game has started, avoid skipping to prevent desynchro
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Thu, 29 Dec 2011 10:35:54 +0100 |
parents | 216107767b8a |
children | c099802e2435 |
comparison
equal
deleted
inserted
replaced
483:216107767b8a | 484:699c8e8c7738 |
---|---|
16 self.remote_addr = dest | 16 self.remote_addr = dest |
17 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | 17 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) |
18 self.sock.bind(('', port)) | 18 self.sock.bind(('', port)) |
19 | 19 |
20 | 20 |
21 def read_messages(self): | 21 def read_message(self): |
22 messages = [] | 22 message = None |
23 | 23 |
24 start_time = time.time() | 24 start_time = time.time() |
25 delta = 1./60. | 25 delta = 1./60. |
26 | 26 |
27 rlist, wlist, xlist = select([self.sock], [], [], delta) | 27 rlist, wlist, xlist = select([self.sock], [], [], delta) |
28 while rlist: | 28 while rlist: |
29 msg, addr = rlist[0].recvfrom(MSG_STRUCT.size) | 29 msg, addr = rlist[0].recvfrom(MSG_STRUCT.size) |
30 # Check whether the message comes from the right address | 30 # Check whether the message comes from the right address |
31 if self.remote_addr is None or addr == self.remote_addr: | 31 if self.frame == 0 or addr == self.remote_addr: |
32 self.remote_addr = addr | 32 self.remote_addr = addr |
33 | 33 |
34 frame, keystate, old_keystate = MSG_STRUCT.unpack(msg) | 34 frame, keystate, old_keystate = MSG_STRUCT.unpack(msg) |
35 | 35 |
36 # Check for well-formedness | 36 # Check for well-formedness |
37 if frame in (self.frame, self.frame + 1): | 37 if frame in (self.frame, self.frame + 1): |
38 messages.append((frame, keystate, old_keystate)) | 38 message = (frame, keystate, old_keystate) |
39 else: | 39 else: |
40 print('Mismatch', self.remote_addr, addr) | 40 print('Mismatch', self.remote_addr, addr) |
41 | 41 |
42 delta = 0 if messages else max(0, 1./60. - (time.time() - start_time)) | 42 # If no valid message has been read, wait for one as long as possible |
43 # else, read as much as we can without blocking. | |
44 delta = 0 if message else max(0, 1./60. - (time.time() - start_time)) | |
43 rlist, wlist, xlist = select(rlist, [], [], delta) | 45 rlist, wlist, xlist = select(rlist, [], [], delta) |
44 | 46 |
45 #TODO: self.send_message() | 47 return message |
46 | |
47 return messages | |
48 | 48 |
49 | 49 |
50 def send_message(self): | 50 def send_message(self): |
51 frame, keystate, old_keystate = self.frame, self.keystate, self.old_keystate | 51 frame, keystate, old_keystate = self.frame, self.keystate, self.old_keystate |
52 if self.remote_addr is not None: | 52 if self.remote_addr is not None: |
64 self.old_keystate, self.keystate = self.keystate, keystate | 64 self.old_keystate, self.keystate = self.keystate, keystate |
65 self.frame = game.frame | 65 self.frame = game.frame |
66 | 66 |
67 self.send_message() | 67 self.send_message() |
68 | 68 |
69 for frame, keystate, old_keystate in self.read_messages(): | 69 # Follow one valid update |
70 message = self.read_message() | |
71 if message: | |
72 frame, keystate, old_keystate = message | |
70 if frame == game.frame: | 73 if frame == game.frame: |
71 self.run_game_iter(game, self.keystate, keystate) | 74 self.run_game_iter(game, self.keystate, keystate) |
72 elif frame == game.frame + 1: | 75 elif frame == game.frame + 1: |
73 self.run_game_iter(game, self.keystate, old_keystate) | 76 self.run_game_iter(game, self.keystate, old_keystate) |
74 | 77 |