feat: improve performance on receive message
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import asyncio
|
||||
import websockets
|
||||
import json
|
||||
import queue # refer to https://docs.python.org/3/library/queue.html for multi-threading
|
||||
import queue # refer to https://docs.python.org/3/library/queue.html for cross threading communication
|
||||
import uuid
|
||||
from typing import Dict
|
||||
from threading import Thread
|
||||
@@ -22,8 +22,7 @@ class MultiplayerService():
|
||||
@classmethod
|
||||
def enter_game(cls, game_id: str) -> None:
|
||||
cls._current_game_id = game_id
|
||||
_NetworkConnectionService._join_game = True
|
||||
pass
|
||||
_NetworkConnectionService._join_game = True # TODO: change this to a function
|
||||
|
||||
@classmethod
|
||||
def send_piece(cls, piece: "PieceDto") -> None:
|
||||
@@ -31,7 +30,9 @@ class MultiplayerService():
|
||||
|
||||
@classmethod
|
||||
def try_receive_piece(cls) -> "PieceDto":
|
||||
return cls._receive_piece_queue.get() if not cls._receive_piece_queue.empty() else None
|
||||
result = cls._receive_piece_queue.get()
|
||||
cls._receive_piece_queue.task_done()
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def quit(cls) -> None:
|
||||
@@ -42,6 +43,7 @@ class _NetworkConnectionService():
|
||||
_websocket = None
|
||||
_is_closed = False
|
||||
_join_game = False
|
||||
_pending_receive_task = None
|
||||
|
||||
@classmethod
|
||||
async def init(cls) -> None:
|
||||
@@ -59,7 +61,7 @@ class _NetworkConnectionService():
|
||||
|
||||
await cls._try_enter_game()
|
||||
await cls._try_send_piece()
|
||||
# await cls._try_receive_message()
|
||||
await cls._try_receive_message()
|
||||
|
||||
# if conection is closed, exit loop
|
||||
if cls._is_closed:
|
||||
@@ -75,10 +77,6 @@ class _NetworkConnectionService():
|
||||
"clientId": MultiplayerService._client_id,\
|
||||
"gameId": MultiplayerService._current_game_id})
|
||||
await cls._websocket.send(json_message)
|
||||
|
||||
json_response = await cls._websocket.recv()
|
||||
print(json_response)
|
||||
|
||||
cls._join_game = False
|
||||
|
||||
@classmethod
|
||||
@@ -102,13 +100,24 @@ class _NetworkConnectionService():
|
||||
@classmethod
|
||||
async def _try_receive_message(cls) -> None:
|
||||
try:
|
||||
async for message in cls._websocket:
|
||||
data = json.loads(message)
|
||||
task = cls._pending_receive_task if cls._pending_receive_task else asyncio.create_task(cls._websocket.recv())
|
||||
done, pending = await asyncio.wait({task}, timeout=8e-3) # TODO experiment with the timeout
|
||||
|
||||
if task in done:
|
||||
data = json.loads(await task)
|
||||
print(data)
|
||||
# if message type is receive_piece, put it in the receive queue
|
||||
|
||||
if data["type"] == "wait_for_opponent":
|
||||
print("Wait for my opponent!")
|
||||
if data["type"] == "start_game":
|
||||
print("Start the game!")
|
||||
if data["type"] == "receive_piece":
|
||||
# convert Dict to PieceDto
|
||||
print("Receive a piece!")
|
||||
MultiplayerService._receive_piece_queue.put(PieceDto.create(data["piece"]))
|
||||
|
||||
cls._pending_receive_task = None
|
||||
elif len(pending):
|
||||
cls._pending_receive_task = pending.pop()
|
||||
finally:
|
||||
pass # TODO handle connection closed exception
|
||||
|
||||
|
||||
Reference in New Issue
Block a user