From c385ae9d97bce7c6f9f47fd7d18f53fe576844b6 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 20 Aug 2022 19:56:34 +0200 Subject: [PATCH] Fix websocket pinging behaviour The websocket standard allows sending arbitrary pong frames at any time as a unidirectional heartbeat. With the connection's previous implementation of websocket pinging, this would result in a disconnect because the connection thought the server had missed its pong. The current implementation only results in false positives, not negatives, if the server accidentally sends an unprompted pong with the same payload as our last ping. It can be debated whether this is an actual *false* positive or just a plain positive though as the server is still alive. --- src/conn.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/conn.rs b/src/conn.rs index e921353..1c33dc1 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -170,9 +170,13 @@ struct State { packet_tx: mpsc::UnboundedSender, - // TODO An arbitrary pong frame may be sent unsolicited - last_ws_ping: Option, - last_ws_pong: Option>, + // The server may send a pong frame with arbitrary payload unprompted at any + // time (see RFC 6455 5.5.3). Because of this, we can't just remember the + // last pong payload. + ws_ping_counter: u64, + last_ws_ping: Option>, + last_ws_ping_replied_to: bool, + last_euph_ping: Option