Skip to content

Commit

Permalink
Add a WebSocket debug mode (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
olijeffers0n authored Jan 28, 2024
1 parent 17dbb67 commit 98ac223
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rustplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@

__name__ = "rustplus"
__author__ = "olijeffers0n"
__version__ = "5.6.14"
__version__ = "5.6.15"
__support__ = "Discord: https://discord.gg/nQqJe8qvP8"
2 changes: 2 additions & 0 deletions rustplus/api/base_rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(
use_test_server: bool = False,
event_loop: asyncio.AbstractEventLoop = None,
rate_limiter: RateLimiter = None,
debug: bool = False,
) -> None:
if ip is None:
raise ValueError("Ip cannot be None")
Expand Down Expand Up @@ -65,6 +66,7 @@ def __init__(
api=self,
use_test_server=use_test_server,
rate_limiter=rate_limiter,
debug=debug,
)

if heartbeat is None:
Expand Down
8 changes: 8 additions & 0 deletions rustplus/api/remote/rust_remote_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
api=None,
use_test_server: bool = False,
rate_limiter: RateLimiter = None,
debug: bool = False,
) -> None:
self.server_id = server_id
self.api = api
Expand Down Expand Up @@ -65,6 +66,7 @@ def __init__(
self.use_test_server = use_test_server
self.pending_entity_subscriptions = []
self.camera_manager: Union[CameraManager, None] = None
self.debug = debug

async def connect(
self,
Expand All @@ -86,6 +88,7 @@ async def connect(
delay=delay,
on_success_args_kwargs=on_success_args_kwargs,
on_failure_args_kwargs=on_failure_args_kwargs,
debug=self.debug,
)
await self.ws.connect(retries=retries)

Expand All @@ -112,6 +115,11 @@ async def send_message(self, request: AppRequest) -> None:
if self.ws is None:
raise ClientNotConnectedError("No Current Websocket Connection")

if self.debug:
self.logger.info(
f"[RustPlus.py] Sending Message with seq {request.seq}: {request}"
)

self.pending_response_events[request.seq] = YieldingEvent()
await self.ws.send_message(request)

Expand Down
50 changes: 44 additions & 6 deletions rustplus/api/remote/rustws.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(
delay,
on_success_args_kwargs,
on_failure_args_kwargs,
debug: bool = False,
):
self.connection: Union[WebSocketClientProtocol, None] = None
self.task: Union[Task, None] = None
Expand All @@ -53,6 +54,7 @@ def __init__(
self.delay = delay
self.on_success_args_kwargs = on_success_args_kwargs
self.on_failure_args_kwargs = on_failure_args_kwargs
self.debug = debug

async def connect(
self, retries=float("inf"), ignore_open_value: bool = False
Expand Down Expand Up @@ -83,6 +85,10 @@ async def connect(
)
)
address += f"?v={str(self.magic_value)}"

if self.debug:
self.logger.info(f"[RustPlus.py] Connecting to {address}")

self.connection = await connect(
address,
close_timeout=0,
Expand All @@ -108,8 +114,9 @@ async def connect(
break

except Exception as exception:
print_error = True
self.logger.info(f"[RustPlus.py] {exception}")

print_error = True
if not isinstance(exception, KeyboardInterrupt):
# Run the failure callback
if self.on_failure is not None:
Expand Down Expand Up @@ -154,6 +161,9 @@ async def close(self) -> None:
self.task = None
self.connection_status = CLOSED

if self.debug:
self.logger.info(f"[RustPlus.py] Connection Closed")

async def send_message(self, message: AppRequest) -> None:
"""
Send the Protobuf to the server
Expand Down Expand Up @@ -214,6 +224,11 @@ async def run(self) -> None:
)

async def handle_message(self, app_message: AppMessage) -> None:
if self.debug:
self.logger.info(
f"[RustPlus.py] Received Message with seq {app_message.response.seq}: {app_message}"
)

if app_message.response.seq in self.remote.ignored_responses:
self.remote.ignored_responses.remove(app_message.response.seq)
return
Expand All @@ -225,31 +240,48 @@ async def handle_message(self, app_message: AppMessage) -> None:
if prefix is not None:
# This means it is a command

if self.debug:
self.logger.info(
f"[RustPlus.py] Attempting to run Command: {app_message}"
)

message = RustChatMessage(app_message.broadcast.team_message.message)
await self.remote.command_handler.run_command(message, prefix)

if self.is_entity_broadcast(app_message):
# This means that an entity has changed state

if self.debug:
self.logger.info(f"[RustPlus.py] Running Entity Event: {app_message}")

await EventHandler.run_entity_event(
app_message.broadcast.entity_changed.entity_id,
app_message,
self.server_id,
)

elif self.is_camera_broadcast(app_message):
if self.debug:
self.logger.info(f"[RustPlus.py] Running Camera Event: {app_message}")

if self.remote.camera_manager is not None:
await self.remote.camera_manager.add_packet(
RayPacket(app_message.broadcast.camera_rays)
)

elif self.is_team_broadcast(app_message):
if self.debug:
self.logger.info(f"[RustPlus.py] Running Team Event: {app_message}")

# This means that the team of the current player has changed
await EventHandler.run_team_event(app_message, self.server_id)

elif self.is_message(app_message):
# This means that a message has been sent to the team chat

if self.debug:
self.logger.info(f"[RustPlus.py] Running Chat Event: {app_message}")

steam_id = int(app_message.broadcast.team_message.message.steam_id)
message = str(app_message.broadcast.team_message.message.message)

Expand Down Expand Up @@ -278,16 +310,22 @@ async def handle_message(self, app_message: AppMessage) -> None:
else:
self.outgoing_conversation_messages.remove(message)

# Conversation API end
# Conversation API end

await EventHandler.run_chat_event(app_message, self.server_id)

else:
# This means that it wasn't sent by the server and is a message from the server in response to an action
event: YieldingEvent = self.remote.pending_response_events[
app_message.response.seq
]
event.set_with_value(app_message)
event: YieldingEvent = self.remote.pending_response_events.get(
app_message.response.seq, None
)
if event is not None:
if self.debug:
self.logger.info(
f"[RustPlus.py] Running Response Event: {app_message}"
)

event.set_with_value(app_message)

def get_prefix(self, message: str) -> Optional[str]:
if self.remote.use_commands:
Expand Down
2 changes: 2 additions & 0 deletions rustplus/api/rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(
use_test_server: bool = False,
event_loop: asyncio.AbstractEventLoop = None,
rate_limiter: RateLimiter = None,
debug: bool = False,
) -> None:
super().__init__(
ip=ip,
Expand All @@ -69,6 +70,7 @@ def __init__(
use_test_server=use_test_server,
event_loop=event_loop,
rate_limiter=rate_limiter,
debug=debug,
)

async def get_time(self) -> RustTime:
Expand Down

0 comments on commit 98ac223

Please sign in to comment.