changeset 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
files pytouhou/network.py
diffstat 1 files changed, 15 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/network.py
+++ b/pytouhou/network.py
@@ -1,8 +1,9 @@
 import socket
 import struct
 from select import select
+import time
 
-MSG_STRUCT = struct.Struct('!IHHB')
+MSG_STRUCT = struct.Struct('!IHH')
 
 class Network(object):
     def __init__(self, port=8080, dest=None, selected_player=0):
@@ -20,22 +21,28 @@ class Network(object):
     def read_messages(self):
         messages = []
 
-        rlist, wlist, xlist = select([self.sock], [], [], 1./60.)
+        start_time = time.time()
+        delta = 1./60.
+
+        rlist, wlist, xlist = select([self.sock], [], [], delta)
         while rlist:
             msg, addr = rlist[0].recvfrom(MSG_STRUCT.size)
             # Check whether the message comes from the right address
             if self.remote_addr is None or addr == self.remote_addr:
                 self.remote_addr = addr
 
-                frame, keystate, old_keystate, checksum = MSG_STRUCT.unpack(msg)
+                frame, keystate, old_keystate = MSG_STRUCT.unpack(msg)
 
                 # Check for well-formedness
-                if checksum == sum(ord(c) for c in msg[:-1]) & 0xFF:
-                    messages.append((frame, keystate, old_keystate, checksum))
+                if frame in (self.frame, self.frame + 1):
+                    messages.append((frame, keystate, old_keystate))
             else:
                 print('Mismatch', self.remote_addr, addr)
 
-            rlist, wlist, xlist = select(rlist, [], [], 0)
+            delta = 0 if messages else max(0, 1./60. - (time.time() - start_time))
+            rlist, wlist, xlist = select(rlist, [], [], delta)
+
+            #TODO: self.send_message()
 
         return messages
 
@@ -43,11 +50,7 @@ class Network(object):
     def send_message(self):
         frame, keystate, old_keystate = self.frame, self.keystate, self.old_keystate
         if self.remote_addr is not None:
-            checksum = frame + (frame >> 8) + (frame >> 16) + (frame >> 24)
-            checksum += keystate + (keystate >> 8)
-            checksum += old_keystate + (old_keystate >> 8)
-            checksum &= 0xFF
-            self.sock.sendto(MSG_STRUCT.pack(frame, keystate, old_keystate, checksum), self.remote_addr)
+            self.sock.sendto(MSG_STRUCT.pack(frame, keystate, old_keystate), self.remote_addr)
 
 
     def run_game_iter(self, game, keystate, other_keystate):
@@ -63,7 +66,7 @@ class Network(object):
 
         self.send_message()
 
-        for frame, keystate, old_keystate, checksum in self.read_messages():
+        for frame, keystate, old_keystate in self.read_messages():
             if frame == game.frame:
                 self.run_game_iter(game, self.keystate, keystate)
             elif frame == game.frame + 1: