@@ -47,6 +47,12 @@ def __init__(self, type: int, data: bytes):
4747 self .data = data
4848
4949
50+ class MsgHandlingResult (IntEnum ):
51+ TERMINATED = 0
52+ NO_INCOMING_MSGS = 1
53+ MORE_MSGS_QUEUED = 2
54+
55+
5056class SocketRelay :
5157 """
5258 The SocketRelay provides an abstraction over the direct communication with
@@ -250,10 +256,13 @@ def run(self, *, background_thread: bool = False):
250256 else :
251257 self ._running = True
252258 while self ._running and self .is_connected :
253- self ._running , _ = self .handle_incoming_messages (blocking = True )
259+ self ._running = (
260+ self .handle_incoming_messages (blocking = True )
261+ != MsgHandlingResult .TERMINATED
262+ )
254263 self ._running = False
255264
256- def handle_incoming_messages (self , blocking : bool = False ) -> tuple [ bool , bool ] :
265+ def handle_incoming_messages (self , blocking : bool = False ) -> MsgHandlingResult :
257266 """
258267 Empties queue of incoming messages (should be called regularly, see `run`).
259268 Optionally blocking, ensuring that at least one message will be handled.
@@ -268,30 +277,32 @@ def handle_incoming_messages(self, blocking: bool = False) -> tuple[bool, bool]:
268277 self .socket .setblocking (blocking )
269278 incoming_message = self .read_message ()
270279 try :
271- return self .handle_incoming_message (incoming_message ), True
280+ return self .handle_incoming_message (incoming_message )
272281 except flat .InvalidFlatbuffer as e :
273282 self .logger .error (
274283 "Error while unpacking message of type %s (%s bytes): %s" ,
275284 incoming_message .type .name ,
276285 len (incoming_message .data ),
277286 e ,
278287 )
279- return False , False
288+ return MsgHandlingResult . TERMINATED
280289 except Exception as e :
281290 self .logger .error (
282291 "Unexpected error while handling message of type %s: %s" ,
283292 incoming_message .type .name ,
284293 e ,
285294 )
286- return False , False
295+ return MsgHandlingResult . TERMINATED
287296 except BlockingIOError :
288297 # No incoming messages and blocking==False
289- return True , False
298+ return MsgHandlingResult . NO_INCOMING_MSGS
290299 except :
291300 self .logger .error ("SocketRelay disconnected unexpectedly!" )
292- return False , False
301+ return MsgHandlingResult . TERMINATED
293302
294- def handle_incoming_message (self , incoming_message : SocketMessage ):
303+ def handle_incoming_message (
304+ self , incoming_message : SocketMessage
305+ ) -> MsgHandlingResult :
295306 """
296307 Handles a messages by passing it to the relevant handlers.
297308 Returns True if the message was NOT a shutdown request (i.e. NONE).
@@ -302,7 +313,7 @@ def handle_incoming_message(self, incoming_message: SocketMessage):
302313
303314 match incoming_message .type :
304315 case SocketDataType .NONE :
305- return False
316+ return MsgHandlingResult . TERMINATED
306317 case SocketDataType .GAME_PACKET :
307318 if len (self .packet_handlers ) > 0 :
308319 packet = flat .GamePacket .unpack (incoming_message .data )
@@ -340,7 +351,7 @@ def handle_incoming_message(self, incoming_message: SocketMessage):
340351 case _:
341352 pass
342353
343- return True
354+ return MsgHandlingResult . MORE_MSGS_QUEUED
344355
345356 def disconnect (self ):
346357 if not self .is_connected :
0 commit comments