feat: improve performance on receive message
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import websockets
|
import websockets
|
||||||
import json
|
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
|
import uuid
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
@@ -22,8 +22,7 @@ class MultiplayerService():
|
|||||||
@classmethod
|
@classmethod
|
||||||
def enter_game(cls, game_id: str) -> None:
|
def enter_game(cls, game_id: str) -> None:
|
||||||
cls._current_game_id = game_id
|
cls._current_game_id = game_id
|
||||||
_NetworkConnectionService._join_game = True
|
_NetworkConnectionService._join_game = True # TODO: change this to a function
|
||||||
pass
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def send_piece(cls, piece: "PieceDto") -> None:
|
def send_piece(cls, piece: "PieceDto") -> None:
|
||||||
@@ -31,7 +30,9 @@ class MultiplayerService():
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def try_receive_piece(cls) -> "PieceDto":
|
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
|
@classmethod
|
||||||
def quit(cls) -> None:
|
def quit(cls) -> None:
|
||||||
@@ -42,6 +43,7 @@ class _NetworkConnectionService():
|
|||||||
_websocket = None
|
_websocket = None
|
||||||
_is_closed = False
|
_is_closed = False
|
||||||
_join_game = False
|
_join_game = False
|
||||||
|
_pending_receive_task = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def init(cls) -> None:
|
async def init(cls) -> None:
|
||||||
@@ -59,7 +61,7 @@ class _NetworkConnectionService():
|
|||||||
|
|
||||||
await cls._try_enter_game()
|
await cls._try_enter_game()
|
||||||
await cls._try_send_piece()
|
await cls._try_send_piece()
|
||||||
# await cls._try_receive_message()
|
await cls._try_receive_message()
|
||||||
|
|
||||||
# if conection is closed, exit loop
|
# if conection is closed, exit loop
|
||||||
if cls._is_closed:
|
if cls._is_closed:
|
||||||
@@ -75,10 +77,6 @@ class _NetworkConnectionService():
|
|||||||
"clientId": MultiplayerService._client_id,\
|
"clientId": MultiplayerService._client_id,\
|
||||||
"gameId": MultiplayerService._current_game_id})
|
"gameId": MultiplayerService._current_game_id})
|
||||||
await cls._websocket.send(json_message)
|
await cls._websocket.send(json_message)
|
||||||
|
|
||||||
json_response = await cls._websocket.recv()
|
|
||||||
print(json_response)
|
|
||||||
|
|
||||||
cls._join_game = False
|
cls._join_game = False
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -102,13 +100,24 @@ class _NetworkConnectionService():
|
|||||||
@classmethod
|
@classmethod
|
||||||
async def _try_receive_message(cls) -> None:
|
async def _try_receive_message(cls) -> None:
|
||||||
try:
|
try:
|
||||||
async for message in cls._websocket:
|
task = cls._pending_receive_task if cls._pending_receive_task else asyncio.create_task(cls._websocket.recv())
|
||||||
data = json.loads(message)
|
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)
|
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":
|
if data["type"] == "receive_piece":
|
||||||
# convert Dict to PieceDto
|
print("Receive a piece!")
|
||||||
MultiplayerService._receive_piece_queue.put(PieceDto.create(data["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:
|
finally:
|
||||||
pass # TODO handle connection closed exception
|
pass # TODO handle connection closed exception
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user