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
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ src_libbitcoin_network_la_SOURCES = \
src/channels/channel.cpp \
src/channels/channel_http.cpp \
src/channels/channel_peer.cpp \
src/channels/channel_rpc.cpp \
src/channels/channel_ws.cpp \
src/config/address.cpp \
src/config/authority.cpp \
Expand Down Expand Up @@ -160,6 +161,7 @@ test_libbitcoin_network_test_SOURCES = \
test/channels/channel.cpp \
test/channels/channel_http.cpp \
test/channels/channel_peer.cpp \
test/channels/channel_rpc.cpp \
test/channels/channel_ws.cpp \
test/config/address.cpp \
test/config/authority.cpp \
Expand Down Expand Up @@ -302,6 +304,7 @@ include_bitcoin_network_channels_HEADERS = \
include/bitcoin/network/channels/channel.hpp \
include/bitcoin/network/channels/channel_http.hpp \
include/bitcoin/network/channels/channel_peer.hpp \
include/bitcoin/network/channels/channel_rpc.hpp \
include/bitcoin/network/channels/channel_ws.hpp \
include/bitcoin/network/channels/channels.hpp

Expand Down Expand Up @@ -450,6 +453,7 @@ include_bitcoin_network_protocols_HEADERS = \
include/bitcoin/network/protocols/protocol_ping_106.hpp \
include/bitcoin/network/protocols/protocol_ping_60001.hpp \
include/bitcoin/network/protocols/protocol_reject_70002.hpp \
include/bitcoin/network/protocols/protocol_rpc.hpp \
include/bitcoin/network/protocols/protocol_seed_209.hpp \
include/bitcoin/network/protocols/protocol_version_106.hpp \
include/bitcoin/network/protocols/protocol_version_70001.hpp \
Expand Down
2 changes: 2 additions & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/channels/channel.cpp"
"../../src/channels/channel_http.cpp"
"../../src/channels/channel_peer.cpp"
"../../src/channels/channel_rpc.cpp"
"../../src/channels/channel_ws.cpp"
"../../src/config/address.cpp"
"../../src/config/authority.cpp"
Expand Down Expand Up @@ -366,6 +367,7 @@ if (with-tests)
"../../test/channels/channel.cpp"
"../../test/channels/channel_http.cpp"
"../../test/channels/channel_peer.cpp"
"../../test/channels/channel_rpc.cpp"
"../../test/channels/channel_ws.cpp"
"../../test/config/address.cpp"
"../../test/config/authority.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<ClCompile Include="..\..\..\..\test\channels\channel.cpp" />
<ClCompile Include="..\..\..\..\test\channels\channel_http.cpp" />
<ClCompile Include="..\..\..\..\test\channels\channel_peer.cpp" />
<ClCompile Include="..\..\..\..\test\channels\channel_rpc.cpp" />
<ClCompile Include="..\..\..\..\test\channels\channel_ws.cpp" />
<ClCompile Include="..\..\..\..\test\config\address.cpp">
<ObjectFileName>$(IntDir)test_config_address.obj</ObjectFileName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@
<ClCompile Include="..\..\..\..\test\channels\channel_peer.cpp">
<Filter>src\channels</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\channels\channel_rpc.cpp">
<Filter>src\channels</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\channels\channel_ws.cpp">
<Filter>src\channels</Filter>
</ClCompile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
<ClCompile Include="..\..\..\..\src\channels\channel.cpp" />
<ClCompile Include="..\..\..\..\src\channels\channel_http.cpp" />
<ClCompile Include="..\..\..\..\src\channels\channel_peer.cpp" />
<ClCompile Include="..\..\..\..\src\channels\channel_rpc.cpp" />
<ClCompile Include="..\..\..\..\src\channels\channel_ws.cpp" />
<ClCompile Include="..\..\..\..\src\config\address.cpp">
<ObjectFileName>$(IntDir)src_config_address.obj</ObjectFileName>
Expand Down Expand Up @@ -241,6 +242,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\channels\channel.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\channels\channel_http.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\channels\channel_peer.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\channels\channel_rpc.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\channels\channel_ws.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\channels\channels.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\config\address.hpp" />
Expand Down Expand Up @@ -336,6 +338,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\protocols\protocol_ping_106.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\protocols\protocol_ping_60001.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\protocols\protocol_reject_70002.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\protocols\protocol_rpc.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\protocols\protocol_seed_209.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\protocols\protocol_version_106.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\network\protocols\protocol_version_70001.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
<ClCompile Include="..\..\..\..\src\channels\channel_peer.cpp">
<Filter>src\channels</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\channels\channel_rpc.cpp">
<Filter>src\channels</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\channels\channel_ws.cpp">
<Filter>src\channels</Filter>
</ClCompile>
Expand Down Expand Up @@ -473,6 +476,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\channels\channel_peer.hpp">
<Filter>include\bitcoin\network\channels</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\channels\channel_rpc.hpp">
<Filter>include\bitcoin\network\channels</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\channels\channel_ws.hpp">
<Filter>include\bitcoin\network\channels</Filter>
</ClInclude>
Expand Down Expand Up @@ -758,6 +764,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\network\protocols\protocol_reject_70002.hpp">
<Filter>include\bitcoin\network\protocols</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\protocols\protocol_rpc.hpp">
<Filter>include\bitcoin\network\protocols</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\network\protocols\protocol_seed_209.hpp">
<Filter>include\bitcoin\network\protocols</Filter>
</ClInclude>
Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include <bitcoin/network/channels/channel.hpp>
#include <bitcoin/network/channels/channel_http.hpp>
#include <bitcoin/network/channels/channel_peer.hpp>
#include <bitcoin/network/channels/channel_rpc.hpp>
#include <bitcoin/network/channels/channel_ws.hpp>
#include <bitcoin/network/channels/channels.hpp>
#include <bitcoin/network/config/address.hpp>
Expand Down Expand Up @@ -133,6 +134,7 @@
#include <bitcoin/network/protocols/protocol_ping_106.hpp>
#include <bitcoin/network/protocols/protocol_ping_60001.hpp>
#include <bitcoin/network/protocols/protocol_reject_70002.hpp>
#include <bitcoin/network/protocols/protocol_rpc.hpp>
#include <bitcoin/network/protocols/protocol_seed_209.hpp>
#include <bitcoin/network/protocols/protocol_version_106.hpp>
#include <bitcoin/network/protocols/protocol_version_70001.hpp>
Expand Down
49 changes: 49 additions & 0 deletions include/bitcoin/network/channels/channel_rpc.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NETWORK_CHANNELS_CHANNEL_RPC_HPP
#define LIBBITCOIN_NETWORK_CHANNELS_CHANNEL_RPC_HPP

#include <memory>
#include <bitcoin/network/channels/channel.hpp>
#include <bitcoin/network/define.hpp>
#include <bitcoin/network/log/log.hpp>
#include <bitcoin/network/net/net.hpp>

namespace libbitcoin {
namespace network {

/// rpc over tcp channel.
class BCT_API channel_rpc
: public channel
{
public:
typedef std::shared_ptr<channel_rpc> ptr;

inline channel_rpc(const logger& log, const socket::ptr& socket,
uint64_t identifier, const settings_t& settings,
const options_t& options) NOEXCEPT
: channel(log, socket, identifier, settings, options)
{
}
};

} // namespace network
} // namespace libbitcoin

#endif
1 change: 1 addition & 0 deletions include/bitcoin/network/channels/channels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <bitcoin/network/channels/channel.hpp>
#include <bitcoin/network/channels/channel_http.hpp>
#include <bitcoin/network/channels/channel_peer.hpp>
#include <bitcoin/network/channels/channel_rpc.hpp>
#include <bitcoin/network/channels/channel_ws.hpp>

#endif
2 changes: 1 addition & 1 deletion include/bitcoin/network/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ BCT_API bool asio_is_canceled(const boost_code& ec) NOEXCEPT;
BCT_API code asio_to_error_code(const boost_code& ec) NOEXCEPT;

/// 1:1 mapping of boost::beast:http::error to network (or error::unknown).
BCT_API code beast_to_error_code(const boost_code& ec) NOEXCEPT;
BCT_API code http_to_error_code(const boost_code& ec) NOEXCEPT;

} // namespace error
} // namespace network
Expand Down
6 changes: 6 additions & 0 deletions include/bitcoin/network/impl/messages/json_body.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ void CLASS::reader::finish(boost_code& ec) NOEXCEPT
parser_.reset();
}

TEMPLATE
bool CLASS::reader::done() const NOEXCEPT
{
return parser_.done();
}

// json::body<>::writer
// ----------------------------------------------------------------------------

Expand Down
5 changes: 2 additions & 3 deletions include/bitcoin/network/messages/json_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct body
virtual void init(const http::length_type& length, boost_code& ec) NOEXCEPT;
virtual size_t put(const buffer_type& buffer, boost_code& ec) NOEXCEPT;
virtual void finish(boost_code& ec) NOEXCEPT;
virtual bool done() const NOEXCEPT;

protected:
value_type& value_;
Expand All @@ -86,6 +87,7 @@ struct body
class writer
{
public:
static constexpr size_t default_buffer = 4096;
using const_buffers_type = asio::const_buffer;
using out_buffer = http::get_buffer<const_buffers_type>;

Expand All @@ -108,9 +110,6 @@ struct body
protected:
value_type& value_;
boost::json::serializer serializer_;

private:
static constexpr size_t default_buffer = 4096;
};
};

Expand Down
10 changes: 5 additions & 5 deletions include/bitcoin/network/net/proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class BCT_API proxy
public:
typedef std::shared_ptr<proxy> ptr;
typedef subscriber<> stop_subscriber;
typedef rpc::request_body::value_type rpc_in_value;
typedef rpc::response_body::value_type rpc_out_value;

DELETE_COPY_MOVE(proxy);

Expand Down Expand Up @@ -126,19 +128,17 @@ class BCT_API proxy
count_handler&& handler) NOEXCEPT;

/// Send a complete TCP message to the remote endpoint.
virtual void write(const asio::const_buffer& payload,
virtual void write(const asio::const_buffer& buffer,
count_handler&& handler) NOEXCEPT;

/// TCP-RPC (e.g. electrum, stratum_v1).
/// -----------------------------------------------------------------------

/// Read full rpc request from the socket, handler posted to socket strand.
virtual void read(rpc::response_t& out,
count_handler&& handler) NOEXCEPT;
virtual void read(rpc_in_value& out, count_handler&& handler) NOEXCEPT;

/// Write full rpc response to the socket, handler posted to socket strand.
virtual void write(const rpc::response_t& in,
count_handler&& handler) NOEXCEPT;
virtual void write(rpc_out_value&& in, count_handler&& handler) NOEXCEPT;

/// HTTP (generic).
/// -----------------------------------------------------------------------
Expand Down
53 changes: 44 additions & 9 deletions include/bitcoin/network/net/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class BCT_API socket
{
public:
typedef std::shared_ptr<socket> ptr;
typedef rpc::request_body::value_type rpc_in_value;
typedef rpc::response_body::value_type rpc_out_value;

DELETE_COPY_MOVE(socket);

Expand Down Expand Up @@ -104,11 +106,11 @@ class BCT_API socket
/// -----------------------------------------------------------------------

/// Read full rpc request from the socket, handler posted to socket strand.
virtual void rpc_read(rpc::response_t& out,
virtual void rpc_read(rpc_in_value& request,
count_handler&& handler) NOEXCEPT;

/// Write full rpc response to the socket, handler posted to socket strand.
virtual void rpc_write(const rpc::response_t& model,
virtual void rpc_write(rpc_out_value&& response,
count_handler&& handler) NOEXCEPT;

/// HTTP (generic).
Expand Down Expand Up @@ -158,13 +160,46 @@ class BCT_API socket
/// The socket was upgraded to a websocket (requires strand).
virtual bool websocket() const NOEXCEPT;

/// Utility.
asio::socket& get_transport() NOEXCEPT;
void logx(const std::string& context, const boost_code& ec) const NOEXCEPT;

private:
struct read_rpc
{
typedef std::shared_ptr<read_rpc> ptr;
using rpc_reader = rpc::request_body::reader;

read_rpc(rpc_in_value& request_) NOEXCEPT
: value{}, reader{ value }
{
request_ = value;
}

rpc_in_value value;
rpc_reader reader;
};

struct write_rpc
{
typedef std::shared_ptr<write_rpc> ptr;
using rpc_writer = rpc::response_body::writer;
using out_buffer = rpc_writer::out_buffer;

write_rpc(rpc_out_value&& response) NOEXCEPT
: value{ std::move(response) }, writer{ value }
{
}

rpc_out_value value;
rpc_writer writer;
};

// stop
// ------------------------------------------------------------------------

void do_stop() NOEXCEPT;
void do_async_stop() NOEXCEPT;
asio::socket& get_transport() NOEXCEPT;

// wait
// ------------------------------------------------------------------------
Expand All @@ -186,11 +221,9 @@ class BCT_API socket
const count_handler& handler) NOEXCEPT;

// tcp (rpc)
void do_rpc_read(
std::reference_wrapper<rpc::response_t> out,
void do_rpc_read(boost_code ec, size_t total, const read_rpc::ptr& in,
const count_handler& handler) NOEXCEPT;
void do_rpc_write(
const std::reference_wrapper<const rpc::response_t>& in,
void do_rpc_write(boost_code ec, size_t total, const write_rpc::ptr& out,
const count_handler& handler) NOEXCEPT;

// http (generic)
Expand Down Expand Up @@ -228,8 +261,10 @@ class BCT_API socket
const count_handler& handler) NOEXCEPT;

// tcp (rpc)
void handle_rpc_tcp(const boost_code& ec, size_t size,
const count_handler& handler) NOEXCEPT;
void handle_rpc_read(boost_code ec, size_t size, size_t total,
const read_rpc::ptr& in, const count_handler& handler) NOEXCEPT;
void handle_rpc_write(boost_code ec, size_t size, size_t total,
const write_rpc::ptr& out, const count_handler& handler) NOEXCEPT;

// http (generic)
void handle_http_read(const boost_code& ec, size_t size,
Expand Down
Loading
Loading