Skip to content

Commit

Permalink
Expose proxy/channel/protocol .resume like .pause.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Mar 16, 2024
1 parent fc7e7a0 commit 500ed60
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
7 changes: 6 additions & 1 deletion include/bitcoin/network/net/channel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 4 additions & 1 deletion include/bitcoin/network/protocols/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// -----------------------------------------------------------------------

Expand Down
30 changes: 25 additions & 5 deletions src/net/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 1 addition & 2 deletions src/net/proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions src/protocols/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 500ed60

Please sign in to comment.