diff --git a/include/bitcoin/network/net/channel.hpp b/include/bitcoin/network/net/channel.hpp index dcf0832a4..1a59a995b 100644 --- a/include/bitcoin/network/net/channel.hpp +++ b/include/bitcoin/network/net/channel.hpp @@ -82,7 +82,10 @@ class BCT_API channel /// Idempotent, may be called multiple times. void stop(const code& ec) NOEXCEPT override; - /// Resume reading from the socket (requires strand). + /// Pause reading from the socket, stops timers (requires strand). + void pause() NOEXCEPT override; + + /// Resume reading from the socket, starts timers (requires strand). void resume() NOEXCEPT override; /// The channel does not "speak" to peers (e.g. seed connection). @@ -124,9 +127,11 @@ class BCT_API channel bool is_handshaked() const NOEXCEPT; void do_stop(const code& ec) NOEXCEPT; + void stop_expiration() NOEXCEPT; void start_expiration() NOEXCEPT; void handle_expiration(const code& ec) NOEXCEPT; + void stop_inactivity() NOEXCEPT; void start_inactivity() NOEXCEPT; void handle_inactivity(const code& ec) NOEXCEPT; diff --git a/include/bitcoin/network/protocols/protocol.hpp b/include/bitcoin/network/protocols/protocol.hpp index c4febf249..08b5a8db2 100644 --- a/include/bitcoin/network/protocols/protocol.hpp +++ b/include/bitcoin/network/protocols/protocol.hpp @@ -150,9 +150,12 @@ class BCT_API protocol /// Stop the channel. virtual void stop(const code& ec) NOEXCEPT; - /// Pause the channel (strand required). + /// Pause reading from the socket, stops timers (requires strand). virtual void pause() NOEXCEPT; + /// Resume reading from the socket, starts timers (requires strand). + virtual void resume() NOEXCEPT; + /// Properties. /// ----------------------------------------------------------------------- diff --git a/src/net/channel.cpp b/src/net/channel.cpp index 687a13018..e5b5440e0 100644 --- a/src/net/channel.cpp +++ b/src/net/channel.cpp @@ -89,8 +89,8 @@ void channel::stop(const code& ec) NOEXCEPT void channel::do_stop(const code&) NOEXCEPT { BC_ASSERT_MSG(stranded(), "strand"); - inactivity_->stop(); - expiration_->stop(); + stop_expiration(); + stop_inactivity(); } // Pause/resume (paused upon create). @@ -99,6 +99,15 @@ void channel::do_stop(const code&) NOEXCEPT // Timers are set for handshake and reset upon protocol start. // Version protocols may have more restrictive completion timeouts. // A restarted timer invokes completion handler with error::operation_canceled. + +void channel::pause() NOEXCEPT +{ + BC_ASSERT_MSG(stranded(), "strand"); + stop_expiration(); + stop_inactivity(); + proxy::pause(); +} + void channel::resume() NOEXCEPT { BC_ASSERT_MSG(stranded(), "strand"); @@ -219,9 +228,15 @@ void channel::signal_activity() NOEXCEPT // Timers. // ---------------------------------------------------------------------------- // TODO: build DoS protection around rate_limit_, backlog(), total(), and time. - -// Called from start or strand. // A restarted timer invokes completion handler with error::operation_canceled. +// Called from start or strand. + +void channel::stop_expiration() NOEXCEPT +{ + BC_ASSERT_MSG(stranded(), "strand"); + expiration_->stop(); +} + void channel::start_expiration() NOEXCEPT { BC_ASSERT_MSG(stranded(), "strand"); @@ -253,7 +268,12 @@ void channel::handle_expiration(const code& ec) NOEXCEPT stop(error::channel_expired); } -// Called from start or strand. +void channel::stop_inactivity() NOEXCEPT +{ + BC_ASSERT_MSG(stranded(), "strand"); + inactivity_->stop(); +} + void channel::start_inactivity() NOEXCEPT { BC_ASSERT_MSG(stranded(), "strand"); diff --git a/src/net/proxy.cpp b/src/net/proxy.cpp index 7ab613ff6..712191d94 100644 --- a/src/net/proxy.cpp +++ b/src/net/proxy.cpp @@ -169,8 +169,7 @@ void proxy::read_heading() NOEXCEPT { BC_ASSERT_MSG(stranded(), "strand"); - // Terminates the read loop (cannot be resumed). - // Pauses the read loop (can be resumed), does not pause timer. + // Both terminate read loop, paused can be resumed, stopped cannot. if (stopped() || paused()) return; diff --git a/src/protocols/protocol.cpp b/src/protocols/protocol.cpp index 61e339e1c..c4dfef07d 100644 --- a/src/protocols/protocol.cpp +++ b/src/protocols/protocol.cpp @@ -95,6 +95,13 @@ void protocol::pause() NOEXCEPT channel_->pause(); } +// Resumes reads from the socket following pause. +void protocol::resume() NOEXCEPT +{ + BC_ASSERT_MSG(stranded(), "stranded"); + channel_->resume(); +} + // Properties. // ---------------------------------------------------------------------------- // The public properties may be accessed outside the strand, except during