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
2 changes: 1 addition & 1 deletion include/bitcoin/network/channels/channel_rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class channel_rpc

/// Subscribe to request from client (requires strand).
/// Event handler is always invoked on the channel strand.
template <class Void, class Handler>
template <class Unused, class Handler>
inline void subscribe(Handler&& handler) NOEXCEPT
{
BC_ASSERT(stranded());
Expand Down
18 changes: 9 additions & 9 deletions include/bitcoin/network/messages/http_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ using string_value = http::string_body::value_type;
using json_value = http::json_body::value_type;
using body_value = std::variant
<
empty_value,
data_value,
file_value,
span_value,
buffer_value,
string_value,
json_value,
rpc::request,
rpc::response
empty_value, // 1 byte
data_value, // 40 bytes
file_value, // 32 bytes
span_value, // 16 bytes
buffer_value, // 24 bytes
string_value, // 40 bytes
json_value, // 48 bytes
rpc::request, // 248 bytes!
rpc::response // 360 bytes!
>;

/// body template for all known message types.
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/network/protocols/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ class BCT_API protocol

#define DECLARE_SEND() \
template <class Derived, class Message, typename Method, typename... Args> \
void send(Message&& message, Method&& method, Args&&... args) NOEXCEPT \
inline void send(Message&& message, Method&& method, Args&&... args) NOEXCEPT \
{ channel_->send(std::forward<Message>(message), BIND_SHARED(method, args)); }

#define DECLARE_SUBSCRIBE_CHANNEL() \
template <class Derived, class Message, typename Method, typename... Args> \
void subscribe_channel(Method&& method, Args&&... args) NOEXCEPT \
inline void subscribe_channel(Method&& method, Args&&... args) NOEXCEPT \
{ channel_->template subscribe<Message>(BIND_SHARED(method, args)); }

#define SEND(message, method, ...) \
Expand Down
65 changes: 64 additions & 1 deletion include/bitcoin/network/protocols/protocol_rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define LIBBITCOIN_NETWORK_PROTOCOL_RPC_HPP

#include <memory>
#include <utility>
#include <bitcoin/network/channels/channels.hpp>
#include <bitcoin/network/define.hpp>
#include <bitcoin/network/protocols/protocol.hpp>
Expand All @@ -33,6 +34,7 @@ class protocol_rpc
{
public:
typedef std::shared_ptr<protocol> ptr;
using protocol_t = protocol_rpc<Interface>;
using channel_t = channel_rpc<Interface>;
using options_t = channel_t::options_t;

Expand All @@ -44,9 +46,70 @@ class protocol_rpc
{
}

DECLARE_SEND()
DECLARE_SUBSCRIBE_CHANNEL()

template <class Derived, typename Method, typename... Args>
inline void send(network::rpc::response_t&& message, size_t size_hint,
Method&& method, Args&&... args) NOEXCEPT
{
channel_->send(std::move(message), size_hint,
std::bind(std::forward<Method>(method),
shared_from_base<Derived>(), std::forward<Args>(args)...));
}

// TODO: capture and correlate version/id.
inline void send_result(network::rpc::value_t&& value,
size_t size_hint) NOEXCEPT
{
using namespace network::rpc;
using namespace std::placeholders;
send<protocol_t>(
{
.jsonrpc = version::v2,
.id = 42,
////.error = {},
.result = std::move(value)
},
size_hint, &protocol_t::handle_complete, _1, error::success);
}

// TODO: capture and correlate version/id.
inline void send_error(const code& reason) NOEXCEPT
{
using namespace network::rpc;
using namespace std::placeholders;
const auto size_hint = two * reason.message().size();
send<protocol_t>(
{
.jsonrpc = version::v2,
.id = 42,
.error = result_t
{
.code = reason.value(),
.message = reason.message()
}
////.result = {}
},
size_hint, &protocol_t::handle_complete, _1, reason);
}

inline void handle_complete(const code& ec, const code& reason) NOEXCEPT
{
BC_ASSERT(stranded());

if (stopped(ec))
return;

if (reason)
{
stop(reason);
return;
}

// Continue read loop.
channel_->receive();
}

private:
// This is mostly thread safe, and used in a thread safe manner.
// pause/resume/paused/attach not invoked, setters limited to handshake.
Expand Down
Loading