annotate 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
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
483
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
4 import time
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5
483
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
6 MSG_STRUCT = struct.Struct('!IHH')
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 class Network(object):
479
1de67f332f00 Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents: 478
diff changeset
9 def __init__(self, port=8080, dest=None, selected_player=0):
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 self.frame = 0
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 self.keystate = 0
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 self.old_keystate = 0
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13
479
1de67f332f00 Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents: 478
diff changeset
14 self.selected_player = selected_player
1de67f332f00 Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents: 478
diff changeset
15
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 self.remote_addr = dest
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 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
18 self.sock.bind(('', port))
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 def read_messages(self):
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 messages = []
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23
483
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
24 start_time = time.time()
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
25 delta = 1./60.
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
26
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
27 rlist, wlist, xlist = select([self.sock], [], [], delta)
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28 while rlist:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 msg, addr = rlist[0].recvfrom(MSG_STRUCT.size)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 # Check whether the message comes from the right address
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 if self.remote_addr is None or addr == self.remote_addr:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 self.remote_addr = addr
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33
483
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
34 frame, keystate, old_keystate = MSG_STRUCT.unpack(msg)
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 # Check for well-formedness
483
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
37 if frame in (self.frame, self.frame + 1):
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
38 messages.append((frame, keystate, old_keystate))
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 else:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40 print('Mismatch', self.remote_addr, addr)
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41
483
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
42 delta = 0 if messages else max(0, 1./60. - (time.time() - start_time))
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
43 rlist, wlist, xlist = select(rlist, [], [], delta)
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
44
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
45 #TODO: self.send_message()
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 return messages
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50 def send_message(self):
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 frame, keystate, old_keystate = self.frame, self.keystate, self.old_keystate
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 if self.remote_addr is not None:
483
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
53 self.sock.sendto(MSG_STRUCT.pack(frame, keystate, old_keystate), self.remote_addr)
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55
479
1de67f332f00 Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents: 478
diff changeset
56 def run_game_iter(self, game, keystate, other_keystate):
1de67f332f00 Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents: 478
diff changeset
57 keystates = [other_keystate, other_keystate]
1de67f332f00 Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents: 478
diff changeset
58 keystates[self.selected_player] = keystate
1de67f332f00 Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents: 478
diff changeset
59 game.run_iter(keystates)
1de67f332f00 Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents: 478
diff changeset
60
1de67f332f00 Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents: 478
diff changeset
61
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62 def run_iter(self, game, keystate):
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 if self.frame < game.frame:
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 self.old_keystate, self.keystate = self.keystate, keystate
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 self.frame = game.frame
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66
480
d666856acb52 Hopefully improve performance
Thibaut Girka <thib@sitedethib.com>
parents: 479
diff changeset
67 self.send_message()
d666856acb52 Hopefully improve performance
Thibaut Girka <thib@sitedethib.com>
parents: 479
diff changeset
68
483
216107767b8a Remove useless checksum, and wait as long as possible for valid packets (do not stop waiting on invalid packets)
Thibaut Girka <thib@sitedethib.com>
parents: 482
diff changeset
69 for frame, keystate, old_keystate in self.read_messages():
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 if frame == game.frame:
479
1de67f332f00 Fix several synchro issues
Thibaut Girka <thib@sitedethib.com>
parents: 478
diff changeset
71 self.run_game_iter(game, self.keystate, keystate)
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 elif frame == game.frame + 1:
482
f6e44398afa5 Fix a synchro issue... again...
Thibaut Girka <thib@sitedethib.com>
parents: 481
diff changeset
73 self.run_game_iter(game, self.keystate, old_keystate)
475
9d4d52793eca Experimental netplay! Yay!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74