X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=plomrogue%2Fio.py;h=438b8b6e1301e4da584127382629791ce87756b6;hb=253f4ce3544c6e9982ba5296c4b28f5fc1888db5;hp=252fac18d3c3fba5a5f6308f535e165746f20b7d;hpb=df9a8d0a788b29913dae3eec4ef8113e2d8e9a41;p=plomrogue2 diff --git a/plomrogue/io.py b/plomrogue/io.py index 252fac1..438b8b6 100644 --- a/plomrogue/io.py +++ b/plomrogue/io.py @@ -16,20 +16,24 @@ class GameIO(): def loop(self, q): """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. + 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) - if connection_id in potential_flooders: - if int(time.time() * 100) == potential_flooders[connection_id]: + 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] = int(time.time() * 100) - self.handle_input(connection_id, command) + 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() @@ -38,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):