Skip to content

Commit fc0d2d8

Browse files
committed
Implement json-rpc over tcp socket/proxy methods.
1 parent 7e1484e commit fc0d2d8

File tree

8 files changed

+190
-98
lines changed

8 files changed

+190
-98
lines changed

include/bitcoin/network/error.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ BCT_API bool asio_is_canceled(const boost_code& ec) NOEXCEPT;
238238
BCT_API code asio_to_error_code(const boost_code& ec) NOEXCEPT;
239239

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

243243
} // namespace error
244244
} // namespace network

include/bitcoin/network/impl/messages/json_body.ipp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ void CLASS::reader::finish(boost_code& ec) NOEXCEPT
114114
parser_.reset();
115115
}
116116

117+
TEMPLATE
118+
bool CLASS::reader::done() const NOEXCEPT
119+
{
120+
return parser_.done();
121+
}
122+
117123
// json::body<>::writer
118124
// ----------------------------------------------------------------------------
119125

include/bitcoin/network/messages/json_body.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct body
7373
virtual void init(const http::length_type& length, boost_code& ec) NOEXCEPT;
7474
virtual size_t put(const buffer_type& buffer, boost_code& ec) NOEXCEPT;
7575
virtual void finish(boost_code& ec) NOEXCEPT;
76+
virtual bool done() const NOEXCEPT;
7677

7778
protected:
7879
value_type& value_;
@@ -86,6 +87,7 @@ struct body
8687
class writer
8788
{
8889
public:
90+
static constexpr size_t default_buffer = 4096;
8991
using const_buffers_type = asio::const_buffer;
9092
using out_buffer = http::get_buffer<const_buffers_type>;
9193

@@ -108,9 +110,6 @@ struct body
108110
protected:
109111
value_type& value_;
110112
boost::json::serializer serializer_;
111-
112-
private:
113-
static constexpr size_t default_buffer = 4096;
114113
};
115114
};
116115

include/bitcoin/network/net/proxy.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class BCT_API proxy
4242
public:
4343
typedef std::shared_ptr<proxy> ptr;
4444
typedef subscriber<> stop_subscriber;
45+
typedef rpc::request_body::value_type rpc_in_value;
46+
typedef rpc::response_body::value_type rpc_out_value;
4547

4648
DELETE_COPY_MOVE(proxy);
4749

@@ -126,19 +128,17 @@ class BCT_API proxy
126128
count_handler&& handler) NOEXCEPT;
127129

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

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

135137
/// Read full rpc request from the socket, handler posted to socket strand.
136-
virtual void read(rpc::response_t& out,
137-
count_handler&& handler) NOEXCEPT;
138+
virtual void read(rpc_in_value& out, count_handler&& handler) NOEXCEPT;
138139

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

143143
/// HTTP (generic).
144144
/// -----------------------------------------------------------------------

include/bitcoin/network/net/socket.hpp

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class BCT_API socket
4141
{
4242
public:
4343
typedef std::shared_ptr<socket> ptr;
44+
typedef rpc::request_body::value_type rpc_in_value;
45+
typedef rpc::response_body::value_type rpc_out_value;
4446

4547
DELETE_COPY_MOVE(socket);
4648

@@ -104,11 +106,11 @@ class BCT_API socket
104106
/// -----------------------------------------------------------------------
105107

106108
/// Read full rpc request from the socket, handler posted to socket strand.
107-
virtual void rpc_read(rpc::response_t& out,
109+
virtual void rpc_read(rpc_in_value& request,
108110
count_handler&& handler) NOEXCEPT;
109111

110112
/// Write full rpc response to the socket, handler posted to socket strand.
111-
virtual void rpc_write(const rpc::response_t& model,
113+
virtual void rpc_write(rpc_out_value&& response,
112114
count_handler&& handler) NOEXCEPT;
113115

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

163+
/// Utility.
164+
asio::socket& get_transport() NOEXCEPT;
165+
void logx(const std::string& context, const boost_code& ec) const NOEXCEPT;
166+
161167
private:
168+
struct read_rpc
169+
{
170+
typedef std::shared_ptr<read_rpc> ptr;
171+
using rpc_reader = rpc::request_body::reader;
172+
173+
read_rpc(rpc_in_value& request_) NOEXCEPT
174+
: value{}, reader{ value }
175+
{
176+
request_ = value;
177+
}
178+
179+
rpc_in_value value;
180+
rpc_reader reader;
181+
};
182+
183+
struct write_rpc
184+
{
185+
typedef std::shared_ptr<write_rpc> ptr;
186+
using rpc_writer = rpc::response_body::writer;
187+
using out_buffer = rpc_writer::out_buffer;
188+
189+
write_rpc(rpc_out_value&& response) NOEXCEPT
190+
: value{ std::move(response) }, writer{ value }
191+
{
192+
}
193+
194+
rpc_out_value value;
195+
rpc_writer writer;
196+
};
197+
162198
// stop
163199
// ------------------------------------------------------------------------
164200

165201
void do_stop() NOEXCEPT;
166202
void do_async_stop() NOEXCEPT;
167-
asio::socket& get_transport() NOEXCEPT;
168203

169204
// wait
170205
// ------------------------------------------------------------------------
@@ -186,11 +221,9 @@ class BCT_API socket
186221
const count_handler& handler) NOEXCEPT;
187222

188223
// tcp (rpc)
189-
void do_rpc_read(
190-
std::reference_wrapper<rpc::response_t> out,
224+
void do_rpc_read(boost_code ec, size_t total, const read_rpc::ptr& in,
191225
const count_handler& handler) NOEXCEPT;
192-
void do_rpc_write(
193-
const std::reference_wrapper<const rpc::response_t>& in,
226+
void do_rpc_write(boost_code ec, size_t total, const write_rpc::ptr& out,
194227
const count_handler& handler) NOEXCEPT;
195228

196229
// http (generic)
@@ -228,8 +261,10 @@ class BCT_API socket
228261
const count_handler& handler) NOEXCEPT;
229262

230263
// tcp (rpc)
231-
void handle_rpc_tcp(const boost_code& ec, size_t size,
232-
const count_handler& handler) NOEXCEPT;
264+
void handle_rpc_read(boost_code ec, size_t size, size_t total,
265+
const read_rpc::ptr& in, const count_handler& handler) NOEXCEPT;
266+
void handle_rpc_write(boost_code ec, size_t size, size_t total,
267+
const write_rpc::ptr& out, const count_handler& handler) NOEXCEPT;
233268

234269
// http (generic)
235270
void handle_http_read(const boost_code& ec, size_t size,

src/error.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ code asio_to_error_code(const boost_code& ec) NOEXCEPT
316316
return error::unknown;
317317
}
318318

319-
code beast_to_error_code(const boost_code& ec) NOEXCEPT
319+
code http_to_error_code(const boost_code& ec) NOEXCEPT
320320
{
321321
namespace beast = boost::beast::http;
322322

src/net/proxy.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,28 @@ void proxy::read(const asio::mutable_buffer& buffer,
194194
socket_->read(buffer, std::move(handler));
195195
}
196196

197-
void proxy::write(const asio::const_buffer& payload,
197+
void proxy::write(const asio::const_buffer& buffer,
198198
count_handler&& handler) NOEXCEPT
199199
{
200200
boost::asio::dispatch(strand(),
201201
std::bind(&proxy::do_write,
202-
shared_from_this(), payload, std::move(handler)));
202+
shared_from_this(), buffer, std::move(handler)));
203203
}
204204

205205
// TCP-RPC.
206206
// ----------------------------------------------------------------------------
207207

208-
void proxy::read(rpc::response_t& out, count_handler&& handler) NOEXCEPT
208+
void proxy::read(rpc_in_value& out, count_handler&& handler) NOEXCEPT
209209
{
210210
boost::asio::dispatch(strand(),
211211
std::bind(&proxy::waiting, shared_from_this()));
212212

213213
socket_->rpc_read(out, std::move(handler));
214214
}
215215

216-
void proxy::write(const rpc::response_t& in, count_handler&& handler) NOEXCEPT
216+
void proxy::write(rpc_out_value&& in, count_handler&& handler) NOEXCEPT
217217
{
218-
socket_->rpc_write(in, std::move(handler));
218+
socket_->rpc_write(std::move(in), std::move(handler));
219219

220220
// TODO: compose?
221221
////boost::asio::dispatch(strand(),

0 commit comments

Comments
 (0)