home · contact · privacy
Write out visible players' names in client map view.
[plomrogue2] / plomrogue / io.py
index 8e0f9bf530548572eb7c07d389d7188e19af47d1..438b8b6e1301e4da584127382629791ce87756b6 100644 (file)
@@ -14,11 +14,26 @@ class GameIO():
         self.servers = []
 
     def loop(self, q):
-        """Handle commands coming through queue q, run game, send results back."""
+        """Handle commands coming through queue q, run game, send results back.
+
+        As basic flood protection, only accepts ten commands per connection per
+        1/10 of a second.
+
+        """
+        import time
+        potential_flooders = {}
         while True:
             try:
-                command, connection_id = q.get(timeout=0.001)
-                self.handle_input(connection_id, command)
+                connection_id, command = q.get(timeout=0.001)
+                now = int(time.time() * 10)
+                if connection_id in potential_flooders and \
+                   potential_flooders[connection_id][0] == now:
+                    if potential_flooders[connection_id][1] > 10:
+                        continue
+                    potential_flooders[connection_id][1] += 1
+                else:
+                    potential_flooders[connection_id] = [now, 1]
+                self.handle_input(command, connection_id)
             except queue.Empty:
                 self.game.run_tick()
 
@@ -27,8 +42,20 @@ class GameIO():
 
         The game loop works sequentially through game commands received
         via self.queue from connected servers' clients."""
+
         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.start()
 
     def start_server(self, port, server_class, certfile=None, keyfile=None):
@@ -69,9 +96,9 @@ class GameIO():
                     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:
@@ -88,7 +115,7 @@ class GameIO():
         """
         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: