home · contact · privacy
Add comment explaining game state sending interval.
[plomrogue2] / plomrogue / io.py
index 68f580ba3fa689b78075a175ed4a2e880128235f..8cba18d7a483664047b3601890fec3df2c4490a9 100644 (file)
@@ -17,19 +17,22 @@ class GameIO():
         """Handle commands coming through queue q, run game, send results back.
 
         As basic flood protection, Only accepts one command per connection per
         """Handle commands coming through queue q, run game, send results back.
 
         As basic flood protection, Only accepts one command per connection per
-        1/100 of a second.
+        1/100 of a second (currently commented out).
 
         """
         import time
         potential_flooders = {}
         while True:
             try:
 
         """
         import time
         potential_flooders = {}
         while True:
             try:
-                command, connection_id = q.get(timeout=0.001)
-                if connection_id in potential_flooders:
-                    if int(time.time() * 100) == potential_flooders[connection_id]:
-                        continue
-                potential_flooders[connection_id] = int(time.time() * 100)
-                self.handle_input(connection_id, command)
+                connection_id, command = q.get(timeout=0.001)
+
+                # FIXME: this would catch the init command flood
+                #if connection_id in potential_flooders:
+                #    if int(time.time() * 100) == potential_flooders[connection_id]:
+                #        continue
+                #potential_flooders[connection_id] = int(time.time() * 100)
+
+                self.handle_input(command, connection_id)
             except queue.Empty:
                 self.game.run_tick()
 
             except queue.Empty:
                 self.game.run_tick()
 
@@ -38,8 +41,20 @@ class GameIO():
 
         The game loop works sequentially through game commands received
         via self.queue from connected servers' clients."""
 
         The game loop works sequentially through game commands received
         via self.queue from connected servers' clients."""
+
         self.queue = queue.Queue()
         self.queue = queue.Queue()
+
+        # optionally use this for main thread profiling:
+        # import cProfile
+        # class ProfiledThread(threading.Thread):
+        #     def run(self):
+        #         profiler = cProfile.Profile()
+        #         profiler.runcall(threading.Thread.run, self)
+        #         print('profiled thread finished')
+        #         profiler.dump_stats('profile')
+        # c = ProfiledThread(target=self.loop, args=(self.queue,))
         c = threading.Thread(target=self.loop, args=(self.queue,))
         c = threading.Thread(target=self.loop, args=(self.queue,))
+
         c.start()
 
     def start_server(self, port, server_class, certfile=None, keyfile=None):
         c.start()
 
     def start_server(self, port, server_class, certfile=None, keyfile=None):
@@ -80,9 +95,9 @@ class GameIO():
                     command(*args, connection_id=connection_id)
                 elif god_mode:
                     command(*args)
                     command(*args, connection_id=connection_id)
                 elif god_mode:
                     command(*args)
-                    #if store and not hasattr(command, 'dont_save'):
-                    #    with open(self.game_file_name, 'a') as f:
-                    #        f.write(input_ + '\n')
+                    # if store and not hasattr(command, 'dont_save'):
+                    #     with open(self.game_file_name, 'a') as f:
+                    #         f.write(input_ + '\n')
         except ArgError as e:
             answer(connection_id, 'ARGUMENT_ERROR ' + quote(str(e)))
         except PlayError as e:
         except ArgError as e:
             answer(connection_id, 'ARGUMENT_ERROR ' + quote(str(e)))
         except PlayError as e:
@@ -99,7 +114,7 @@ class GameIO():
         """
         if connection_id:
             for server in self.servers:
         """
         if connection_id:
             for server in self.servers:
-                 if connection_id in server.clients:
+                if connection_id in server.clients:
                     client = server.clients[connection_id]
                     client.put(msg)
         else:
                     client = server.clients[connection_id]
                     client.put(msg)
         else: