From b437731c7f17821e33a66700ec5f5e07bbcce6c3 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 7 Apr 2019 19:46:30 +0000 Subject: [PATCH] Improve pinging It actually resets the ping check now. Also, I'm now using the event callbacks for sending the ping-reply, which only seems appropriate. --- yaboli/connection.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/yaboli/connection.py b/yaboli/connection.py index eaf5c54..7c3d8d6 100644 --- a/yaboli/connection.py +++ b/yaboli/connection.py @@ -64,15 +64,15 @@ class Connection: - "reconnecting" : No arguments - "reconnected" : No arguments - "disconnecting" : No arguments - - "on_": the packet, parsed as JSON + - "": the packet, parsed as JSON Events ending with "-ing" ("reconnecting", "disconnecting") are fired at the beginning of the process they represent. Events ending with "-ed" ("connected", "reconnected") are fired after the process they represent has finished. - Examples for the last category of events include "on_message-event", - "on_part-event" and "on_ping". + Examples for the last category of events include "message-event", + "part-event" and "ping". """ # Maximum duration between euphoria's ping messages. Euphoria usually sends @@ -114,6 +114,8 @@ class Connection: self._awaiting_replies: Optional[Dict[str, asyncio.Future[Any]]] = None self._ping_check: Optional[asyncio.Task[None]] = None + self.register_event("ping-event", self._ping_pong) + def register_event(self, event: str, callback: Callable[..., Awaitable[None]] @@ -438,16 +440,13 @@ class Connection: # Then, send the corresponding event packet_type = packet["type"] - self._events.fire(f"on_{packet_type}", packet) + self._events.fire(packet_type, packet) - # Finally, if it's a ping command, reply as per - # http://api.euphoria.io/#ping - if packet_type == "ping-event": - logger.debug("Pong!") - asyncio.create_task(self._send_if_possible( - "ping-reply", - {"time": packet["data"]["time"]} - )) + # Finally, reset the ping check + logger.debug("Resetting ping check") + self._ping_check.cancel() + self._ping_check = asyncio.create_task( + self._disconnect_in(self.PING_TIMEOUT)) async def _send_if_possible(self, packet_type: str, data: Any,) -> None: """ @@ -462,6 +461,13 @@ class Connection: except IncorrectStateException: logger.debug("Could not send (disconnecting or already disconnected)") + async def _ping_pong(self, packet): + # Implements http://api.euphoria.io/#ping and is called as "ping-event" + # callback + logger.debug("Pong!") + await self._send_if_possible("ping-reply", + {"time": packet["data"]["time"]}) + async def send(self, packet_type: str, data: Any,