diff --git a/src/client/transport.rs b/src/client/transport.rs index 796e86a..7d7cdcc 100644 --- a/src/client/transport.rs +++ b/src/client/transport.rs @@ -1,5 +1,4 @@ //! An IRC transport that wraps an IRC-framed stream to provide automatic PING replies. -use std::io; use std::sync::{Arc, RwLock, RwLockReadGuard}; use std::time::Instant; @@ -50,9 +49,7 @@ where fn poll(&mut self) -> Poll, Self::Error> { if self.last_ping.elapsed().as_secs() >= self.ping_timeout { self.close()?; - Err( - io::Error::new(io::ErrorKind::ConnectionReset, "Ping timed out.").into(), - ) + Err(error::ErrorKind::PingTimeout.into()) } else { loop { match try_ready!(self.inner.poll()) { @@ -77,11 +74,25 @@ where type SinkError = error::Error; fn start_send(&mut self, item: Self::SinkItem) -> StartSend { - Ok(self.inner.start_send(item)?) + if self.last_ping.elapsed().as_secs() >= self.ping_timeout { + self.close()?; + Err(error::ErrorKind::PingTimeout.into()) + } else { + Ok(self.inner.start_send(item)?) + } } fn poll_complete(&mut self) -> Poll<(), Self::SinkError> { - Ok(self.inner.poll_complete()?) + if self.last_ping.elapsed().as_secs() >= self.ping_timeout { + self.close()?; + Err(error::ErrorKind::PingTimeout.into()) + } else { + Ok(self.inner.poll_complete()?) + } + } + + fn close(&mut self) -> Poll<(), Self::SinkError> { + self.inner.close() } } diff --git a/src/error.rs b/src/error.rs index 1fcb4c3..7f74cbe 100644 --- a/src/error.rs +++ b/src/error.rs @@ -30,6 +30,7 @@ error_chain! { display("Failed to parse an IRC subcommand.") } + /// Failed to parse a mode correctly. ModeParsingFailed { description("Failed to parse a mode correctly.") display("Failed to parse a mode correctly.") @@ -46,5 +47,11 @@ error_chain! { description("An error occured causing a mutex for a logged transport to be poisoned.") display("An error occured causing a mutex for a logged transport to be poisoned.") } + + /// Connection timed out due to no ping response. + PingTimeout { + description("The connection timed out due to no ping response.") + display("The connection timed out due to no ping response.") + } } }