annotate pytouhou/network.py @ 475:9d4d52793eca

Experimental netplay! Yay!
author Thibaut Girka <thib@sitedethib.com>
date Wed, 28 Dec 2011 18:45:09 +0100
parents
children e71b1bcf952a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 import socket
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 import struct
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 from select import select
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 MSG_STRUCT = struct.Struct('!IHHB')
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 class Network(object):
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 def __init__(self, port=8080, dest=None):
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 self.frame = 0
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 self.keystate = 0
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 self.old_keystate = 0
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 self.remote_addr = dest
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14 self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 self.sock.setblocking(0)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 self.sock.bind(('', port))
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 def read_messages(self):
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 messages = []
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 rlist, wlist, xlist = select([self.sock], [], [], 0)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 while rlist:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 msg, addr = rlist[0].recvfrom(MSG_STRUCT.size)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 # Check whether the message comes from the right address
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 if self.remote_addr is None or addr == self.remote_addr:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 self.remote_addr = addr
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 frame, keystate, old_keystate, checksum = MSG_STRUCT.unpack(msg)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 # Check for well-formedness
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 if checksum == sum(ord(c) for c in msg[:-1]) & 0xFF:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 messages.append((frame, keystate, old_keystate, checksum))
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 else:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 print('Mismatch', self.remote_addr, addr)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 rlist, wlist, xlist = select(rlist, [], [], 0)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 return messages
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 def send_message(self):
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 frame, keystate, old_keystate = self.frame, self.keystate, self.old_keystate
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 if self.remote_addr is not None:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 checksum = frame + (frame >> 8) + (frame >> 16) + (frame >> 24)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 checksum += keystate + (keystate >> 8)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 checksum += old_keystate + (old_keystate >> 8)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 checksum &= 0xFF
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 self.sock.sendto(MSG_STRUCT.pack(frame, keystate, old_keystate, checksum), self.remote_addr)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 def run_iter(self, game, keystate):
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 if self.frame < game.frame:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 self.old_keystate, self.keystate = self.keystate, keystate
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 self.frame = game.frame
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 for frame, keystate, old_keystate, checksum in self.read_messages():
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 if frame == game.frame:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 game.run_iter([self.keystate, keystate])
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 elif frame == game.frame + 1:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 print('Skipped')
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62 game.run_iter([self.keystate, old_keystate])
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 game.run_iter([self.keystate, keystate])
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 self.send_message()
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66