Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
olijeffers0n committed Jun 14, 2023
2 parents 91f66fa + 5d8cc65 commit 08dd1d2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
### Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting Ollie on discord at `Ollie#0175`. All
reported by contacting Ollie on discord at `ollieee_`. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Expand Down
9 changes: 8 additions & 1 deletion rustplus/api/base_rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,17 @@ def _generate_protobuf(self) -> AppRequest:
return app_request

async def connect(
self, retries: int = float("inf"), delay: int = 20, on_failure=None
self, retries: int = float("inf"), delay: int = 20, on_failure=None, on_success=None
) -> None:
"""
Attempts to open a connection to the rust game server specified in the constructor
:param retries: The number of times to attempt reconnecting. Defaults to infinite.
:param delay: The delay (in seconds) between reconnection attempts.
:param on_failure: Optional function to be called when connecting fails.
:param on_success: Optional function to be called when connecting succeeds.
:return: None
"""
EventLoopManager.set_loop(
Expand All @@ -138,6 +144,7 @@ async def connect(
retries=retries,
delay=delay,
on_failure=on_failure,
on_success=on_success
)
await self.heartbeat.start_beat()
except ConnectionRefusedError:
Expand Down
3 changes: 2 additions & 1 deletion rustplus/api/remote/rust_remote_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ def __init__(
self.pending_entity_subscriptions = []
self.camera_manager: Union[CameraManager, None] = None

async def connect(self, retries, delay, on_failure=None) -> None:
async def connect(self, retries, delay, on_failure=None, on_success=None) -> None:
self.ws = RustWebsocket(
server_id=self.server_id,
remote=self,
use_proxy=self.use_proxy,
magic_value=self.magic_value,
use_test_server=self.use_test_server,
on_failure=on_failure,
on_success=on_success,
delay=delay,
)
await self.ws.connect(retries=retries)
Expand Down
20 changes: 16 additions & 4 deletions rustplus/api/remote/rustws.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(
magic_value,
use_test_server,
on_failure,
on_success,
delay,
):
self.connection: Union[WebSocketClientProtocol, None] = None
Expand All @@ -46,6 +47,7 @@ def __init__(
self.use_test_server = use_test_server
self.outgoing_conversation_messages = []
self.on_failure = on_failure
self.on_success = on_success
self.delay = delay

async def connect(
Expand Down Expand Up @@ -79,14 +81,24 @@ async def connect(
address += f"?v={str(self.magic_value)}"
self.connection = await connect(address, close_timeout=0, ping_interval=None)
self.connected_time = time.time()

if self.on_success is not None:
try:
if asyncio.iscoroutinefunction(self.on_success):
await self.on_success()
else:
self.on_success()
except Exception as e:
self.logger.warning(e)
break

except Exception as exception:
print_error = True

if not isinstance(exception, KeyboardInterrupt):
# Run the failure callback
try:
if self.on_failure is not None:
if self.on_failure is not None:
try:
if asyncio.iscoroutinefunction(self.on_failure):
val = await self.on_failure()
else:
Expand All @@ -95,8 +107,8 @@ async def connect(
if val is not None:
print_error = val

except Exception as e:
self.logger.warning(e)
except Exception as e:
self.logger.warning(e)

if print_error:
self.logger.warning(
Expand Down

0 comments on commit 08dd1d2

Please sign in to comment.