Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions include/bitcoin/network/channels/channel_rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ class channel_rpc
{
}

/// Public senders, rpc version and identity added to responses.
inline void send_code(const code& ec) NOEXCEPT;
inline void send_error(rpc::result_t&& error) NOEXCEPT;
inline void send_result(rpc::value_t&& result, size_t size_hint) NOEXCEPT;
/// Senders, rpc version and identity added to responses (requires strand).
inline void send_code(const code& ec,
result_handler&& handler={}) NOEXCEPT;
inline void send_error(rpc::result_t&& error,
result_handler&& handler={}) NOEXCEPT;
inline void send_result(rpc::value_t&& result, size_t size_hint,
result_handler&& handler={}) NOEXCEPT;

/// Resume reading from the socket (requires strand).
inline void resume() NOEXCEPT override;
Expand Down Expand Up @@ -91,14 +94,11 @@ class channel_rpc
virtual inline void handle_receive(const code& ec, size_t bytes,
const rpc::request_cptr& request) NOEXCEPT;

/// Handle send complation, handler must invoke receive() unless stopping.
/// Handle send completion, invokes receive().
virtual inline void handle_send(const code& ec, size_t bytes,
const rpc::response_cptr& response,
const result_handler& handler) NOEXCEPT;

/// Invoked upon handle_send completion to restart receive().
virtual void handle_complete(const code& ec) NOEXCEPT;

private:
void log_message(const rpc::request& request,
size_t bytes) const NOEXCEPT;
Expand Down
30 changes: 12 additions & 18 deletions include/bitcoin/network/impl/channels/channel_rpc.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,31 @@ inline http::flat_buffer& CLASS::request_buffer() NOEXCEPT
// ----------------------------------------------------------------------------

TEMPLATE
void CLASS::send_code(const code& ec) NOEXCEPT
void CLASS::send_code(const code& ec, result_handler&& handler) NOEXCEPT
{
BC_ASSERT(stranded());
send_error({ .code = ec.value(), .message = ec.message() });
send_error({ .code = ec.value(), .message = ec.message() },
std::move(handler));
}

TEMPLATE
void CLASS::send_error(rpc::result_t&& error) NOEXCEPT
void CLASS::send_error(rpc::result_t&& error,
result_handler&& handler) NOEXCEPT
{
BC_ASSERT(stranded());
using namespace std::placeholders;
send({ .jsonrpc = version_, .id = identity_, .error = std::move(error) },
two * error.message.size(), std::bind(&CLASS::handle_complete,
shared_from_base<CLASS>(), _1));
two * error.message.size(), std::move(handler));
}

TEMPLATE
void CLASS::send_result(rpc::value_t&& result, size_t size_hint) NOEXCEPT
void CLASS::send_result(rpc::value_t&& result, size_t size_hint,
result_handler&& handler) NOEXCEPT
{
BC_ASSERT(stranded());
using namespace std::placeholders;
send({ .jsonrpc = version_, .id = identity_, .result = std::move(result) },
size_hint, std::bind(&CLASS::handle_complete,
shared_from_base<CLASS>(), _1));
size_hint, std::move(handler));
}

// protected
Expand Down Expand Up @@ -183,18 +184,11 @@ inline void CLASS::handle_send(const code& ec, size_t bytes,
BC_ASSERT(stranded());
if (ec) stop(ec);
log_message(*response, bytes);
handler(ec);
}

// protected
TEMPLATE
void CLASS::handle_complete(const code&) NOEXCEPT
{
BC_ASSERT(stranded());
if (stopped())
return;
// Typically a noop, but handshake may pause channel here.
handler(ec);

// Continue read loop.
// Continue read loop (does not unpause or restart channel).
receive();
}

Expand Down
15 changes: 8 additions & 7 deletions include/bitcoin/network/impl/protocols/protocol_rpc.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@ namespace libbitcoin {
namespace network {

TEMPLATE
inline void CLASS::send_code(const code& ec) NOEXCEPT
inline void CLASS::send_code(const code& ec, result_handler&& handler) NOEXCEPT
{
channel_->send_code(ec);
channel_->send_code(ec, std::move(handler));
}

TEMPLATE
inline void CLASS::send_error(rpc::result_t&& error) NOEXCEPT
inline void CLASS::send_error(rpc::result_t&& error,
result_handler&& handler) NOEXCEPT
{
channel_->send_error(std::move(error));
channel_->send_error(std::move(error), std::move(handler));
}

TEMPLATE
inline void CLASS::send_result(rpc::value_t&& result,
size_t size_hint) NOEXCEPT
inline void CLASS::send_result(rpc::value_t&& result, size_t size_hint,
result_handler&& handler) NOEXCEPT
{
channel_->send_result(std::move(result), size_hint);
channel_->send_result(std::move(result), size_hint, std::move(handler));
}

} // namespace network
Expand Down
10 changes: 6 additions & 4 deletions include/bitcoin/network/protocols/protocol_rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ class protocol_rpc
DECLARE_SUBSCRIBE_CHANNEL()

/// Senders (requires strand).
virtual inline void send_code(const code& ec) NOEXCEPT;
virtual inline void send_error(rpc::result_t&& error) NOEXCEPT;
virtual inline void send_result(rpc::value_t&& result,
size_t size_hint) NOEXCEPT;
virtual inline void send_code(const code& ec,
result_handler&& handler={}) NOEXCEPT;
virtual inline void send_error(rpc::result_t&& error,
result_handler&& handler={}) NOEXCEPT;
virtual inline void send_result(rpc::value_t&& result, size_t size_hint,
result_handler&& handler={}) NOEXCEPT;

private:
// This is mostly thread safe, and used in a thread safe manner.
Expand Down
Loading