From 4c27a9513daf454159ed91965e9f8347f37d9607 Mon Sep 17 00:00:00 2001 From: Thomas Hurst Date: Tue, 17 Nov 2020 05:04:37 +0000 Subject: [PATCH] Disable Pinger until after MOTD Some servers ignore PING commands until after login, so clients would otherwise always time out, as mentioned in #207, #214, and #218. With this change I am able to reliably connect to ircu servers. --- src/client/transport.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/client/transport.rs b/src/client/transport.rs index 0a86d125..aac776ee 100644 --- a/src/client/transport.rs +++ b/src/client/transport.rs @@ -20,12 +20,14 @@ use tokio_util::codec::Framed; use crate::{ client::data::Config, error, - proto::{Command, IrcCodec, Message}, + proto::{Command, Response, IrcCodec, Message}, }; /// Pinger-based futures helper. struct Pinger { tx: UnboundedSender, + // Whether this pinger pings. + enabled: bool, /// The amount of time to wait before timing out from no ping response. ping_timeout: Duration, /// The instant that the last ping was sent to the server. @@ -42,6 +44,7 @@ impl Pinger { Self { tx, + enabled: false, ping_timeout, ping_deadline: None, ping_interval: time::interval(ping_time), @@ -51,6 +54,10 @@ impl Pinger { /// Handle an incoming message. fn handle_message(&mut self, message: &Message) -> error::Result<()> { match message.command { + Command::Response(Response::RPL_ENDOFMOTD, _) + | Command::Response(Response::ERR_NOMOTD, _) => { + self.enabled = true; + } // On receiving a `PING` message from the server, we automatically respond with // the appropriate `PONG` message to keep the connection alive for transport. Command::PING(ref data, _) => { @@ -109,8 +116,10 @@ impl Future for Pinger { } if let Poll::Ready(_) = Pin::new(&mut self.as_mut().ping_interval).poll_next(cx) { - self.as_mut().send_ping()?; - self.as_mut().set_deadline(); + if self.enabled { + self.as_mut().send_ping()?; + self.as_mut().set_deadline(); + } } Poll::Pending