Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock the WS #40

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion rustplus/api/base_rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(
self.marker_listener = MapEventListener(self)
self.use_test_server = use_test_server
self.event_loop = event_loop
self.lock = asyncio.Lock()

self.remote = RustRemote(
server_id=self.server_id,
Expand Down Expand Up @@ -173,7 +174,8 @@ async def send_wakeup_request(self) -> None:

await self.remote.add_ignored_response(app_request.seq)

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

async def switch_server(
self,
Expand Down Expand Up @@ -213,6 +215,8 @@ async def switch_server(
raise ValueError("PlayerToken cannot be None")

# disconnect before redefining
await self.lock.acquire()

await self.disconnect()

# Reset basic credentials
Expand Down Expand Up @@ -245,6 +249,7 @@ async def switch_server(
1,
self.ratelimit_refill,
)
del self.remote.conversation_factory
self.remote.conversation_factory = ConversationFactory(self)
# remove entity events
EntityEvent.handlers.unregister_all()
Expand All @@ -255,6 +260,8 @@ async def switch_server(
if connect:
await self.connect()

self.lock.release()

def command(
self,
coro: Callable = None,
Expand Down
10 changes: 6 additions & 4 deletions rustplus/api/remote/camera/camera_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,19 @@ async def send_combined_movement(
cam_input.mouse_delta = vector
app_request.camera_input = cam_input

await self.rust_socket.remote.send_message(app_request)
await self.rust_socket.remote.add_ignored_response(app_request.seq)
async with self.rust_socket.lock:
await self.rust_socket.remote.send_message(app_request)
await self.rust_socket.remote.add_ignored_response(app_request.seq)

async def exit_camera(self) -> None:
await self.rust_socket._handle_ratelimit()
app_request: AppRequest = self.rust_socket._generate_protobuf()
app_request.camera_unsubscribe = AppEmpty()
app_request.camera_unsubscribe._serialized_on_wire = True

await self.rust_socket.remote.send_message(app_request)
await self.rust_socket.remote.add_ignored_response(app_request.seq)
async with self.rust_socket.lock:
await self.rust_socket.remote.send_message(app_request)
await self.rust_socket.remote.add_ignored_response(app_request.seq)

self._open = False
self._last_packets.clear()
Expand Down
17 changes: 11 additions & 6 deletions rustplus/api/remote/rust_remote_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ async def get_response(
if response is not None:
break

await self.send_message(app_request)
async with self.api.lock:
await self.send_message(app_request)

if attempts % 150 == 0:
self.logger.info(
Expand Down Expand Up @@ -156,7 +157,8 @@ async def get_response(
await self.ratelimiter.get_estimated_delay_time(self.server_id, cost)
)

await self.send_message(app_request)
async with self.api.lock:
await self.send_message(app_request)
response = await self.get_response(seq, app_request)

elif self.ws.error_present(response.response.error.error) and error_check:
Expand All @@ -177,7 +179,8 @@ async def get_entity_info(remote: RustRemote, eid):
app_request.get_entity_info = AppEmpty()
app_request.get_entity_info._serialized_on_wire = True

await remote.send_message(app_request)
async with remote.api.lock:
await remote.send_message(app_request)

return await remote.get_response(app_request.seq, app_request, False)

Expand Down Expand Up @@ -210,7 +213,8 @@ async def subscribe_to_camera(
subscribe.camera_id = entity_id
app_request.camera_subscribe = subscribe

await self.send_message(app_request)
async with self.api.lock:
await self.send_message(app_request)

if ignore_response:
await self.add_ignored_response(app_request.seq)
Expand All @@ -222,8 +226,9 @@ async def create_camera_manager(self, cam_id) -> CameraManager:
if self.camera_manager._cam_id == cam_id:
return self.camera_manager

app_request = await self.subscribe_to_camera(cam_id)
app_message = await self.get_response(app_request.seq, app_request)
async with self.api.lock:
app_request = await self.subscribe_to_camera(cam_id)
app_message = await self.get_response(app_request.seq, app_request)

self.camera_manager = CameraManager(
self.api, cam_id, app_message.response.camera_subscribe_info
Expand Down
33 changes: 22 additions & 11 deletions rustplus/api/rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ async def get_time(self) -> RustTime:
app_request.get_time = AppEmpty()
app_request.get_time._serialized_on_wire = True

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

response = await self.remote.get_response(app_request.seq, app_request)

Expand All @@ -95,7 +96,8 @@ async def send_team_message(self, message: str) -> None:

await self.remote.add_ignored_response(app_request.seq)

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

async def get_info(self) -> RustInfo:
await self._handle_ratelimit()
Expand All @@ -104,7 +106,8 @@ async def get_info(self) -> RustInfo:
app_request.get_info = AppEmpty()
app_request.get_info._serialized_on_wire = True

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

response = await self.remote.get_response(app_request.seq, app_request)

Expand All @@ -117,7 +120,8 @@ async def get_team_chat(self) -> List[RustChatMessage]:
app_request.get_team_chat = AppEmpty()
app_request.get_team_chat._serialized_on_wire = True

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

messages = (
await self.remote.get_response(app_request.seq, app_request)
Expand All @@ -132,7 +136,8 @@ async def get_team_info(self) -> RustTeamInfo:
app_request.get_team_info = AppEmpty()
app_request.get_team_info._serialized_on_wire = True

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

app_message = await self.remote.get_response(app_request.seq, app_request)

Expand All @@ -145,7 +150,8 @@ async def get_markers(self) -> List[RustMarker]:
app_request.get_map_markers = AppEmpty()
app_request.get_map_markers._serialized_on_wire = True

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

app_message = await self.remote.get_response(app_request.seq, app_request)

Expand All @@ -160,7 +166,8 @@ async def get_raw_map_data(self) -> RustMap:
app_request.get_map = AppEmpty()
app_request.get_map._serialized_on_wire = True

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

app_message = await self.remote.get_response(app_request.seq, app_request)

Expand Down Expand Up @@ -192,7 +199,8 @@ async def get_map(
app_request.get_map = AppEmpty()
app_request.get_map._serialized_on_wire = True

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

app_message = await self.remote.get_response(app_request.seq, app_request)

Expand Down Expand Up @@ -297,7 +305,8 @@ async def get_entity_info(self, eid: int = None) -> RustEntityInfo:
app_request.get_entity_info = AppEmpty()
app_request.get_entity_info._serialized_on_wire = True

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

app_message = await self.remote.get_response(app_request.seq, app_request)

Expand All @@ -316,7 +325,8 @@ async def _update_smart_device(self, eid: int, value: bool) -> None:

await self.remote.add_ignored_response(app_request.seq)

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

async def turn_on_smart_switch(self, eid: int = None) -> None:
if eid is None:
Expand Down Expand Up @@ -344,7 +354,8 @@ async def promote_to_team_leader(self, steam_id: int = None) -> None:

await self.remote.add_ignored_response(app_request.seq)

await self.remote.send_message(app_request)
async with self.lock:
await self.remote.send_message(app_request)

async def get_current_events(self) -> List[RustMarker]:
return [
Expand Down