Mercurial > touhou
comparison pytouhou/network.py @ 483:216107767b8a
Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Thu, 29 Dec 2011 01:10:34 +0100 |
parents | f6e44398afa5 |
children | 699c8e8c7738 |
comparison
equal
deleted
inserted
replaced
482:f6e44398afa5 | 483:216107767b8a |
---|---|
1 import socket | 1 import socket |
2 import struct | 2 import struct |
3 from select import select | 3 from select import select |
4 import time | |
4 | 5 |
5 MSG_STRUCT = struct.Struct('!IHHB') | 6 MSG_STRUCT = struct.Struct('!IHH') |
6 | 7 |
7 class Network(object): | 8 class Network(object): |
8 def __init__(self, port=8080, dest=None, selected_player=0): | 9 def __init__(self, port=8080, dest=None, selected_player=0): |
9 self.frame = 0 | 10 self.frame = 0 |
10 self.keystate = 0 | 11 self.keystate = 0 |
18 | 19 |
19 | 20 |
20 def read_messages(self): | 21 def read_messages(self): |
21 messages = [] | 22 messages = [] |
22 | 23 |
23 rlist, wlist, xlist = select([self.sock], [], [], 1./60.) | 24 start_time = time.time() |
25 delta = 1./60. | |
26 | |
27 rlist, wlist, xlist = select([self.sock], [], [], delta) | |
24 while rlist: | 28 while rlist: |
25 msg, addr = rlist[0].recvfrom(MSG_STRUCT.size) | 29 msg, addr = rlist[0].recvfrom(MSG_STRUCT.size) |
26 # Check whether the message comes from the right address | 30 # Check whether the message comes from the right address |
27 if self.remote_addr is None or addr == self.remote_addr: | 31 if self.remote_addr is None or addr == self.remote_addr: |
28 self.remote_addr = addr | 32 self.remote_addr = addr |
29 | 33 |
30 frame, keystate, old_keystate, checksum = MSG_STRUCT.unpack(msg) | 34 frame, keystate, old_keystate = MSG_STRUCT.unpack(msg) |
31 | 35 |
32 # Check for well-formedness | 36 # Check for well-formedness |
33 if checksum == sum(ord(c) for c in msg[:-1]) & 0xFF: | 37 if frame in (self.frame, self.frame + 1): |
34 messages.append((frame, keystate, old_keystate, checksum)) | 38 messages.append((frame, keystate, old_keystate)) |
35 else: | 39 else: |
36 print('Mismatch', self.remote_addr, addr) | 40 print('Mismatch', self.remote_addr, addr) |
37 | 41 |
38 rlist, wlist, xlist = select(rlist, [], [], 0) | 42 delta = 0 if messages else max(0, 1./60. - (time.time() - start_time)) |
43 rlist, wlist, xlist = select(rlist, [], [], delta) | |
44 | |
45 #TODO: self.send_message() | |
39 | 46 |
40 return messages | 47 return messages |
41 | 48 |
42 | 49 |
43 def send_message(self): | 50 def send_message(self): |
44 frame, keystate, old_keystate = self.frame, self.keystate, self.old_keystate | 51 frame, keystate, old_keystate = self.frame, self.keystate, self.old_keystate |
45 if self.remote_addr is not None: | 52 if self.remote_addr is not None: |
46 checksum = frame + (frame >> 8) + (frame >> 16) + (frame >> 24) | 53 self.sock.sendto(MSG_STRUCT.pack(frame, keystate, old_keystate), self.remote_addr) |
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 | 54 |
52 | 55 |
53 def run_game_iter(self, game, keystate, other_keystate): | 56 def run_game_iter(self, game, keystate, other_keystate): |
54 keystates = [other_keystate, other_keystate] | 57 keystates = [other_keystate, other_keystate] |
55 keystates[self.selected_player] = keystate | 58 keystates[self.selected_player] = keystate |
61 self.old_keystate, self.keystate = self.keystate, keystate | 64 self.old_keystate, self.keystate = self.keystate, keystate |
62 self.frame = game.frame | 65 self.frame = game.frame |
63 | 66 |
64 self.send_message() | 67 self.send_message() |
65 | 68 |
66 for frame, keystate, old_keystate, checksum in self.read_messages(): | 69 for frame, keystate, old_keystate in self.read_messages(): |
67 if frame == game.frame: | 70 if frame == game.frame: |
68 self.run_game_iter(game, self.keystate, keystate) | 71 self.run_game_iter(game, self.keystate, keystate) |
69 elif frame == game.frame + 1: | 72 elif frame == game.frame + 1: |
70 self.run_game_iter(game, self.keystate, old_keystate) | 73 self.run_game_iter(game, self.keystate, old_keystate) |
71 | 74 |