Mercurial > touhou
annotate pytouhou/network.py @ 481:ef2aca7738d5
Fix synchro bug
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Wed, 28 Dec 2011 21:09:55 +0100 |
parents | d666856acb52 |
children | f6e44398afa5 |
rev | line source |
---|---|
475 | 1 import socket |
2 import struct | |
3 from select import select | |
4 | |
5 MSG_STRUCT = struct.Struct('!IHHB') | |
6 | |
7 class Network(object): | |
479
1de67f332f00
Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents:
478
diff
changeset
|
8 def __init__(self, port=8080, dest=None, selected_player=0): |
475 | 9 self.frame = 0 |
10 self.keystate = 0 | |
11 self.old_keystate = 0 | |
12 | |
479
1de67f332f00
Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents:
478
diff
changeset
|
13 self.selected_player = selected_player |
1de67f332f00
Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents:
478
diff
changeset
|
14 |
475 | 15 self.remote_addr = dest |
16 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
17 self.sock.bind(('', port)) | |
18 | |
19 | |
20 def read_messages(self): | |
21 messages = [] | |
22 | |
477
e71b1bcf952a
Wait a bit for incoming messages...
Thibaut Girka <thib@sitedethib.com>
parents:
475
diff
changeset
|
23 rlist, wlist, xlist = select([self.sock], [], [], 1./60.) |
475 | 24 while rlist: |
25 msg, addr = rlist[0].recvfrom(MSG_STRUCT.size) | |
26 # Check whether the message comes from the right address | |
27 if self.remote_addr is None or addr == self.remote_addr: | |
28 self.remote_addr = addr | |
29 | |
30 frame, keystate, old_keystate, checksum = MSG_STRUCT.unpack(msg) | |
31 | |
32 # Check for well-formedness | |
33 if checksum == sum(ord(c) for c in msg[:-1]) & 0xFF: | |
34 messages.append((frame, keystate, old_keystate, checksum)) | |
35 else: | |
36 print('Mismatch', self.remote_addr, addr) | |
37 | |
38 rlist, wlist, xlist = select(rlist, [], [], 0) | |
39 | |
40 return messages | |
41 | |
42 | |
43 def send_message(self): | |
44 frame, keystate, old_keystate = self.frame, self.keystate, self.old_keystate | |
45 if self.remote_addr is not None: | |
46 checksum = frame + (frame >> 8) + (frame >> 16) + (frame >> 24) | |
47 checksum += keystate + (keystate >> 8) | |
48 checksum += old_keystate + (old_keystate >> 8) | |
49 checksum &= 0xFF | |
50 self.sock.sendto(MSG_STRUCT.pack(frame, keystate, old_keystate, checksum), self.remote_addr) | |
51 | |
52 | |
479
1de67f332f00
Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents:
478
diff
changeset
|
53 def run_game_iter(self, game, keystate, other_keystate): |
1de67f332f00
Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents:
478
diff
changeset
|
54 keystates = [other_keystate, other_keystate] |
1de67f332f00
Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents:
478
diff
changeset
|
55 keystates[self.selected_player] = keystate |
1de67f332f00
Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents:
478
diff
changeset
|
56 game.run_iter(keystates) |
1de67f332f00
Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents:
478
diff
changeset
|
57 |
1de67f332f00
Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents:
478
diff
changeset
|
58 |
475 | 59 def run_iter(self, game, keystate): |
60 if self.frame < game.frame: | |
61 self.old_keystate, self.keystate = self.keystate, keystate | |
62 self.frame = game.frame | |
63 | |
480
d666856acb52
Hopefully improve performance
Thibaut Girka <thib@sitedethib.com>
parents:
479
diff
changeset
|
64 self.send_message() |
d666856acb52
Hopefully improve performance
Thibaut Girka <thib@sitedethib.com>
parents:
479
diff
changeset
|
65 |
475 | 66 for frame, keystate, old_keystate, checksum in self.read_messages(): |
67 if frame == game.frame: | |
479
1de67f332f00
Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents:
478
diff
changeset
|
68 self.run_game_iter(game, self.keystate, keystate) |
475 | 69 elif frame == game.frame + 1: |
481 | 70 self.run_game_iter(game, self.old_keystate, old_keystate) |
475 | 71 |