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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions rustplus/api/base_rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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 @@ -174,7 +175,9 @@ async def send_wakeup_request(self) -> None:

self.remote.ignored_responses.append(app_request.seq)

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

async def switch_server(
self,
Expand Down Expand Up @@ -214,6 +217,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 +250,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 +261,8 @@ async def switch_server(
if connect:
await self.connect()

self.lock.release()

def command(
self,
coro: Callable = None,
Expand Down
4 changes: 4 additions & 0 deletions rustplus/api/remote/camera/camera_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,20 @@ async def send_combined_movement(
cam_input.mouseDelta.CopyFrom(vector)
app_request.cameraInput.CopyFrom(cam_input)

await self.rust_socket.lock.acquire()
await self.rust_socket.remote.send_message(app_request)
self.rust_socket.remote.ignored_responses.append(app_request.seq)
self.rust_socket.lock.release()

async def exit_camera(self) -> None:
await self.rust_socket._handle_ratelimit()
app_request = self.rust_socket._generate_protobuf()
app_request.cameraUnsubscribe.CopyFrom(AppEmpty())

await self.rust_socket.lock.acquire()
await self.rust_socket.remote.send_message(app_request)
self.rust_socket.remote.ignored_responses.append(app_request.seq)
self.rust_socket.lock.release()

self._open = False
self._last_packet = None
Expand Down
12 changes: 12 additions & 0 deletions rustplus/api/remote/rust_remote_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ async def get_response(

else:

await self.api.lock.acquire()
await self.send_message(app_request)
self.api.lock.release()
olijeffers0n marked this conversation as resolved.
Show resolved Hide resolved
await asyncio.sleep(0.1)
attempts = 0

Expand All @@ -141,7 +143,9 @@ async def get_response(
attempts += 1

else:
await self.api.lock.acquire()
await self.send_message(app_request)
self.api.lock.release()
await asyncio.sleep(1)
attempts = 0

Expand Down Expand Up @@ -179,7 +183,9 @@ async def get_response(
self.ratelimiter.get_estimated_delay_time(self.server_id, cost)
)

await self.api.lock.acquire()
await self.send_message(app_request)
self.api.lock.release()
response = await self.get_response(seq, app_request)

elif self.ws.error_present(response.response.error.error) and error_check:
Expand All @@ -201,7 +207,9 @@ async def get_entity_info(self: RustRemote, eid):
app_request.entityId = eid
app_request.getEntityInfo.CopyFrom(AppEmpty())

await self.api.lock.acquire()
await self.send_message(app_request)
self.api.lock.release()

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

Expand Down Expand Up @@ -235,7 +243,9 @@ async def subscribe_to_camera(
subscribe.cameraId = entity_id
app_request.cameraSubscribe.CopyFrom(subscribe)

await self.api.lock.acquire()
await self.send_message(app_request)
self.api.lock.release()

if ignore_response:
self.ignored_responses.append(app_request.seq)
Expand All @@ -248,8 +258,10 @@ async def create_camera_manager(self, cam_id) -> CameraManager:
if self.camera_manager._cam_id == cam_id:
return self.camera_manager

await self.api.lock.acquire()
app_request = await self.subscribe_to_camera(cam_id)
app_message = await self.get_response(app_request.seq, app_request)
self.api.lock.release()

self.camera_manager = CameraManager(
self.api, cam_id, app_message.response.cameraSubscribeInfo
Expand Down
13 changes: 5 additions & 8 deletions rustplus/api/remote/rustws.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from datetime import datetime
from threading import Thread
from typing import Optional
from typing import Optional, TypeVar
import websocket

from .camera.structures import RayPacket
Expand All @@ -21,11 +21,14 @@
CLOSED = 3


RMI = TypeVar("RMI", bound="RustRemoteInterface")


class RustWebsocket(websocket.WebSocket):
def __init__(
self,
server_id: ServerID,
remote,
remote: RMI,
use_proxy,
magic_value,
use_test_server,
Expand Down Expand Up @@ -290,12 +293,6 @@ def is_entity_broadcast(app_message) -> bool:
def is_team_broadcast(app_message) -> bool:
return str(app_message.broadcast.teamChanged) != ""

async def _retry_failed_request(self, app_request: AppRequest) -> None:
"""
Resends an AppRequest to the server if it has failed
"""
await self.send_message(app_request)

@staticmethod
def get_proto_cost(app_request) -> int:
"""
Expand Down
22 changes: 22 additions & 0 deletions rustplus/api/rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ async def get_time(self) -> RustTime:
app_request = self._generate_protobuf()
app_request.getTime.CopyFrom(AppEmpty())

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

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

Expand All @@ -91,7 +93,9 @@ async def send_team_message(self, message: str) -> None:

self.remote.ignored_responses.append(app_request.seq)

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

async def get_info(self) -> RustInfo:

Expand All @@ -100,7 +104,9 @@ async def get_info(self) -> RustInfo:
app_request = self._generate_protobuf()
app_request.getInfo.CopyFrom(AppEmpty())

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

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

Expand All @@ -113,7 +119,9 @@ async def get_team_chat(self) -> List[RustChatMessage]:
app_request = self._generate_protobuf()
app_request.getTeamChat.CopyFrom(AppEmpty())

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

messages = (
await self.remote.get_response(app_request.seq, app_request)
Expand All @@ -128,7 +136,9 @@ async def get_team_info(self) -> RustTeamInfo:
app_request = self._generate_protobuf()
app_request.getTeamInfo.CopyFrom(AppEmpty())

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

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

Expand All @@ -141,7 +151,9 @@ async def get_markers(self) -> List[RustMarker]:
app_request = self._generate_protobuf()
app_request.getMapMarkers.CopyFrom(AppEmpty())

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

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

Expand All @@ -156,7 +168,9 @@ async def get_raw_map_data(self) -> RustMap:
app_request = self._generate_protobuf()
app_request.getMap.CopyFrom(AppEmpty())

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

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

Expand Down Expand Up @@ -188,7 +202,9 @@ async def get_map(
app_request = self._generate_protobuf()
app_request.getMap.CopyFrom(AppEmpty())

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

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

Expand Down Expand Up @@ -293,7 +309,9 @@ async def get_entity_info(self, eid: int = None) -> RustEntityInfo:
app_request.entityId = eid
app_request.getEntityInfo.CopyFrom(AppEmpty())

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

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

Expand All @@ -313,7 +331,9 @@ async def _update_smart_device(self, eid: int, value: bool) -> None:

self.remote.ignored_responses.append(app_request.seq)

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

async def turn_on_smart_switch(self, eid: int = None) -> None:

Expand Down Expand Up @@ -344,7 +364,9 @@ async def promote_to_team_leader(self, steam_id: int = None) -> None:

self.remote.ignored_responses.append(app_request.seq)

await self.lock.acquire()
await self.remote.send_message(app_request)
self.lock.release()

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

Expand Down