Skip to content

Commit 3932e07

Browse files
committed
Move set/reset_request into protocol_http.
1 parent c1a1f63 commit 3932e07

File tree

9 files changed

+86
-51
lines changed

9 files changed

+86
-51
lines changed

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ src_libbitcoin_node_la_SOURCES = \
7171
src/protocols/protocol_header_out_31800.cpp \
7272
src/protocols/protocol_header_out_70012.cpp \
7373
src/protocols/protocol_html.cpp \
74+
src/protocols/protocol_http.cpp \
7475
src/protocols/protocol_observer.cpp \
7576
src/protocols/protocol_peer.cpp \
7677
src/protocols/protocol_performer.cpp \

builds/cmake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ add_library( ${CANONICAL_LIB_NAME}
283283
"../../src/protocols/protocol_header_out_31800.cpp"
284284
"../../src/protocols/protocol_header_out_70012.cpp"
285285
"../../src/protocols/protocol_html.cpp"
286+
"../../src/protocols/protocol_http.cpp"
286287
"../../src/protocols/protocol_observer.cpp"
287288
"../../src/protocols/protocol_peer.cpp"
288289
"../../src/protocols/protocol_performer.cpp"

builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
<ClCompile Include="..\..\..\..\src\protocols\protocol_header_out_31800.cpp" />
157157
<ClCompile Include="..\..\..\..\src\protocols\protocol_header_out_70012.cpp" />
158158
<ClCompile Include="..\..\..\..\src\protocols\protocol_html.cpp" />
159+
<ClCompile Include="..\..\..\..\src\protocols\protocol_http.cpp" />
159160
<ClCompile Include="..\..\..\..\src\protocols\protocol_observer.cpp" />
160161
<ClCompile Include="..\..\..\..\src\protocols\protocol_peer.cpp" />
161162
<ClCompile Include="..\..\..\..\src\protocols\protocol_performer.cpp" />

builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@
168168
<ClCompile Include="..\..\..\..\src\protocols\protocol_html.cpp">
169169
<Filter>src\protocols</Filter>
170170
</ClCompile>
171+
<ClCompile Include="..\..\..\..\src\protocols\protocol_http.cpp">
172+
<Filter>src\protocols</Filter>
173+
</ClCompile>
171174
<ClCompile Include="..\..\..\..\src\protocols\protocol_observer.cpp">
172175
<Filter>src\protocols</Filter>
173176
</ClCompile>

include/bitcoin/node/protocols/protocol_bitcoind_rpc.hpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,22 @@ namespace node {
3030

3131
class BCN_API protocol_bitcoind_rpc
3232
: public node::protocol_http,
33+
public network::protocol_http,
3334
protected network::tracker<protocol_bitcoind_rpc>
3435
{
3536
public:
37+
// Replace base class channel_t (network::channel_http).
38+
using channel_t = node::channel_http;
39+
3640
typedef std::shared_ptr<protocol_bitcoind_rpc> ptr;
3741
using rpc_interface = interface::bitcoind_rpc;
3842
using rpc_dispatcher = network::rpc::dispatcher<rpc_interface>;
3943

4044
inline protocol_bitcoind_rpc(const auto& session,
4145
const network::channel::ptr& channel,
4246
const options_t& options) NOEXCEPT
43-
: node::protocol_http(session, channel, options),
47+
: node::protocol_http(session, channel),
48+
network::protocol_http(session, channel, options),
4449
network::tracker<protocol_bitcoind_rpc>(session->log)
4550
{
4651
}
@@ -59,10 +64,6 @@ class BCN_API protocol_bitcoind_rpc
5964
void handle_receive_post(const code& ec,
6065
const post::cptr& post) NOEXCEPT override;
6166

62-
// Provide the request for serialization, keeping it out of dispatch.
63-
void set_request(const network::http::request_cptr& request) NOEXCEPT;
64-
const network::http::request& get_request() const NOEXCEPT;
65-
6667
/// Handlers.
6768
bool handle_get_best_block_hash(const code& ec,
6869
rpc_interface::get_best_block_hash) NOEXCEPT;
@@ -113,12 +114,8 @@ class BCN_API protocol_bitcoind_rpc
113114
// Send the response.
114115
void send_json(boost::json::value&& model, size_t size_hint) NOEXCEPT;
115116

116-
// This is thread safe.
117-
////const options_t& options_;
118-
119-
// These are protected by strand.
117+
// This is protected by strand.
120118
rpc_dispatcher rpc_dispatcher_{};
121-
network::http::request_cptr request_{};
122119
};
123120

124121
} // namespace node

include/bitcoin/node/protocols/protocol_html.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include <bitcoin/node/channels/channels.hpp>
2323
#include <bitcoin/node/define.hpp>
24-
#include <bitcoin/node/protocols/protocol.hpp>
24+
#include <bitcoin/node/protocols/protocol_http.hpp>
2525
#include <bitcoin/node/settings.hpp>
2626

2727
namespace libbitcoin {
@@ -32,7 +32,7 @@ namespace node {
3232
/// derives from protocol_http (as required). So any html server is able to
3333
/// operate as a websocket server.
3434
class BCN_API protocol_html
35-
: public node::protocol,
35+
: public node::protocol_http,
3636
public network::protocol_ws,
3737
protected network::tracker<protocol_html>
3838
{
@@ -44,7 +44,7 @@ class BCN_API protocol_html
4444
inline protocol_html(const auto& session,
4545
const network::channel::ptr& channel,
4646
const options_t& options) NOEXCEPT
47-
: node::protocol(session, channel),
47+
: node::protocol_http(session, channel),
4848
network::protocol_ws(session, channel, options),
4949
options_(options),
5050
network::tracker<protocol_html>(session->log)

include/bitcoin/node/protocols/protocol_http.hpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,29 @@
2424
#include <bitcoin/node/define.hpp>
2525
#include <bitcoin/node/protocols/protocol.hpp>
2626

27-
// Only session.hpp.
28-
#include <bitcoin/node/sessions/session.hpp>
29-
3027
namespace libbitcoin {
3128
namespace node {
3229

3330
/// Abstract base for HTTP protocols, thread safe.
3431
class BCN_API protocol_http
35-
: public node::protocol,
36-
public network::protocol_http
32+
: public node::protocol
3733
{
38-
public:
39-
// Replace base class channel_t (network::channel_http).
40-
using channel_t = node::channel_http;
41-
4234
protected:
4335
inline protocol_http(const auto& session,
44-
const network::channel::ptr& channel,
45-
const options_t& options) NOEXCEPT
46-
: node::protocol(session, channel),
47-
network::protocol_http(session, channel, options),
48-
channel_(std::static_pointer_cast<node::channel_tcp>(channel)),
49-
session_(session)
36+
const network::channel::ptr& channel) NOEXCEPT
37+
: node::protocol(session, channel)
5038
{
5139
}
5240

53-
private:
54-
// This derived channel requires stranded calls, base is thread safe.
55-
const node::channel_tcp::ptr channel_;
41+
/// Cache request for serialization (requires strand).
42+
void set_request(const network::http::request_cptr& request) NOEXCEPT;
5643

57-
// This is thread safe.
58-
const node::session::ptr session_;
44+
/// Obtain cached request and clear cache (requires strand).
45+
network::http::request_cptr reset_request() NOEXCEPT;
46+
47+
private:
48+
// This is protected by strand.
49+
network::http::request_cptr request_{};
5950
};
6051

6152
} // namespace node

src/protocols/protocol_bitcoind_rpc.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ void protocol_bitcoind_rpc::start() NOEXCEPT
6767
SUBSCRIBE_BITCOIND(handle_scan_tx_out_set, _1, _2, _3, _4);
6868
SUBSCRIBE_BITCOIND(handle_verify_chain, _1, _2, _3, _4);
6969
SUBSCRIBE_BITCOIND(handle_verify_tx_out_set, _1, _2, _3);
70-
protocol_http::start();
70+
network::protocol_http::start();
7171
}
7272

7373
void protocol_bitcoind_rpc::stopping(const code& ec) NOEXCEPT
7474
{
7575
BC_ASSERT(stranded());
7676
rpc_dispatcher_.stop(ec);
77-
protocol_http::stopping(ec);
77+
network::protocol_http::stopping(ec);
7878
}
7979

8080
// Dispatch.
@@ -299,29 +299,17 @@ void protocol_bitcoind_rpc::send_json(boost::json::value&& model,
299299
size_t size_hint) NOEXCEPT
300300
{
301301
BC_ASSERT(stranded());
302-
const auto& request = get_request();
302+
const auto request = reset_request();
303303
constexpr auto json = media_type::application_json;
304-
response response{ status::ok, request.version() };
305-
add_common_headers(response, request);
306-
add_access_control_headers(response, request);
304+
response response{ status::ok, request->version() };
305+
add_common_headers(response, *request);
306+
add_access_control_headers(response, *request);
307307
response.set(field::content_type, from_media_type(json));
308308
response.body() = { std::move(model), size_hint };
309309
response.prepare_payload();
310310
SEND(std::move(response), handle_complete, _1, error::success);
311311
}
312312

313-
void protocol_bitcoind_rpc::set_request(const request_cptr& request) NOEXCEPT
314-
{
315-
BC_ASSERT(request);
316-
request_ = request;
317-
}
318-
319-
const request& protocol_bitcoind_rpc::get_request() const NOEXCEPT
320-
{
321-
BC_ASSERT(request_);
322-
return *request_;
323-
}
324-
325313
BC_POP_WARNING()
326314
BC_POP_WARNING()
327315
BC_POP_WARNING()

src/protocols/protocol_http.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS)
3+
*
4+
* This file is part of libbitcoin.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
#include <bitcoin/node/protocols/protocol_http.hpp>
20+
21+
#include <bitcoin/node/define.hpp>
22+
23+
namespace libbitcoin {
24+
namespace node {
25+
26+
using namespace network::http;
27+
28+
// Cache request for serialization, keeping it out of dispatch.
29+
void protocol_http::set_request(const request_cptr& request) NOEXCEPT
30+
{
31+
////BC_ASSERT(stranded());
32+
BC_ASSERT(request);
33+
request_ = request;
34+
}
35+
36+
// Returns default if not set, for safety (asserts correctness).
37+
request_cptr protocol_http::reset_request() NOEXCEPT
38+
{
39+
////BC_ASSERT(stranded());
40+
BC_ASSERT(request_);
41+
42+
if (request_)
43+
{
44+
auto copy = request_;
45+
request_.reset();
46+
return copy;
47+
}
48+
49+
return system::to_shared<request>();
50+
}
51+
52+
} // namespace node
53+
} // namespace libbitcoin

0 commit comments

Comments
 (0)