Skip to content

Commit c658241

Browse files
authored
Merge pull request #860 from evoskuil/master
Define session_tcp to simplify full_node.
2 parents ff28d2c + decebf6 commit c658241

File tree

12 files changed

+175
-26
lines changed

12 files changed

+175
-26
lines changed

Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ src_libbitcoin_node_la_SOURCES = \
7171
src/sessions/session.cpp \
7272
src/sessions/session_inbound.cpp \
7373
src/sessions/session_manual.cpp \
74-
src/sessions/session_outbound.cpp
74+
src/sessions/session_outbound.cpp \
75+
src/sessions/session_tcp.cpp
7576

7677
# local: test/libbitcoin-node-test
7778
#------------------------------------------------------------------------------
@@ -200,6 +201,7 @@ include_bitcoin_node_sessions_HEADERS = \
200201
include/bitcoin/node/sessions/session_inbound.hpp \
201202
include/bitcoin/node/sessions/session_manual.hpp \
202203
include/bitcoin/node/sessions/session_outbound.hpp \
204+
include/bitcoin/node/sessions/session_tcp.hpp \
203205
include/bitcoin/node/sessions/sessions.hpp
204206

205207
# files => ${bash_completiondir}

builds/cmake/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ add_library( ${CANONICAL_LIB_NAME}
283283
"../../src/sessions/session.cpp"
284284
"../../src/sessions/session_inbound.cpp"
285285
"../../src/sessions/session_manual.cpp"
286-
"../../src/sessions/session_outbound.cpp" )
286+
"../../src/sessions/session_outbound.cpp"
287+
"../../src/sessions/session_tcp.cpp" )
287288

288289
# ${CANONICAL_LIB_NAME} project specific include directory normalization for build.
289290
#------------------------------------------------------------------------------

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
<ClCompile Include="..\..\..\..\src\sessions\session_inbound.cpp" />
158158
<ClCompile Include="..\..\..\..\src\sessions\session_manual.cpp" />
159159
<ClCompile Include="..\..\..\..\src\sessions\session_outbound.cpp" />
160+
<ClCompile Include="..\..\..\..\src\sessions\session_tcp.cpp" />
160161
<ClCompile Include="..\..\..\..\src\settings.cpp" />
161162
</ItemGroup>
162163
<ItemGroup>
@@ -204,6 +205,7 @@
204205
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_inbound.hpp" />
205206
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_manual.hpp" />
206207
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_outbound.hpp" />
208+
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_tcp.hpp" />
207209
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\sessions.hpp" />
208210
<ClInclude Include="..\..\..\..\include\bitcoin\node\settings.hpp" />
209211
<ClInclude Include="..\..\..\..\include\bitcoin\node\version.hpp" />

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@
156156
<ClCompile Include="..\..\..\..\src\sessions\session_outbound.cpp">
157157
<Filter>src\sessions</Filter>
158158
</ClCompile>
159+
<ClCompile Include="..\..\..\..\src\sessions\session_tcp.cpp">
160+
<Filter>src\sessions</Filter>
161+
</ClCompile>
159162
<ClCompile Include="..\..\..\..\src\settings.cpp">
160163
<Filter>src</Filter>
161164
</ClCompile>
@@ -293,6 +296,9 @@
293296
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_outbound.hpp">
294297
<Filter>include\bitcoin\node\sessions</Filter>
295298
</ClInclude>
299+
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\session_tcp.hpp">
300+
<Filter>include\bitcoin\node\sessions</Filter>
301+
</ClInclude>
296302
<ClInclude Include="..\..\..\..\include\bitcoin\node\sessions\sessions.hpp">
297303
<Filter>include\bitcoin\node\sessions</Filter>
298304
</ClInclude>

include/bitcoin/node.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include <bitcoin/node/sessions/session_inbound.hpp>
6262
#include <bitcoin/node/sessions/session_manual.hpp>
6363
#include <bitcoin/node/sessions/session_outbound.hpp>
64+
#include <bitcoin/node/sessions/session_tcp.hpp>
6465
#include <bitcoin/node/sessions/sessions.hpp>
6566

6667
#endif

include/bitcoin/node/full_node.hpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
#include <bitcoin/node/chasers/chasers.hpp>
2626
#include <bitcoin/node/configuration.hpp>
2727
#include <bitcoin/node/define.hpp>
28-
////#include <bitcoin/node/protocols/protocols.hpp>
29-
////#include <bitcoin/node/sessions/sessions.hpp>
28+
#include <bitcoin/node/sessions/sessions.hpp>
3029

3130
namespace libbitcoin {
3231
namespace node {
@@ -45,6 +44,7 @@ class BCN_API full_node
4544
using store = node::store;
4645
using query = node::query;
4746
using memory_controller = block_memory;
47+
using result_handler = network::result_handler;
4848
typedef std::shared_ptr<full_node> ptr;
4949

5050
/// Constructors.
@@ -59,10 +59,10 @@ class BCN_API full_node
5959
/// -----------------------------------------------------------------------
6060

6161
/// Start the node (seed and manual services).
62-
void start(network::result_handler&& handler) NOEXCEPT override;
62+
void start(result_handler&& handler) NOEXCEPT override;
6363

6464
/// Run the node (inbound/outbound services and blockchain chasers).
65-
void run(network::result_handler&& handler) NOEXCEPT override;
65+
void run(result_handler&& handler) NOEXCEPT override;
6666

6767
/// Close the node.
6868
void close() NOEXCEPT override;
@@ -81,7 +81,7 @@ class BCN_API full_node
8181
/// Manage download queue.
8282
virtual void get_hashes(map_handler&& handler) NOEXCEPT;
8383
virtual void put_hashes(const map_ptr& map,
84-
network::result_handler&& handler) NOEXCEPT;
84+
result_handler&& handler) NOEXCEPT;
8585

8686
/// Events.
8787
/// -----------------------------------------------------------------------
@@ -149,7 +149,7 @@ class BCN_API full_node
149149

150150
/// Handle performance, base returns false (implied terminate).
151151
virtual void performance(object_key channel, uint64_t speed,
152-
network::result_handler&& handler) NOEXCEPT;
152+
result_handler&& handler) NOEXCEPT;
153153

154154
/// Get the memory resource.
155155
virtual network::memory& get_memory() NOEXCEPT;
@@ -160,14 +160,17 @@ class BCN_API full_node
160160
network::session_manual::ptr attach_manual_session() NOEXCEPT override;
161161
network::session_inbound::ptr attach_inbound_session() NOEXCEPT override;
162162
network::session_outbound::ptr attach_outbound_session() NOEXCEPT override;
163+
virtual session_explore::ptr attach_explore_session() NOEXCEPT;
163164

164165
/// Virtual handlers.
165166
/// -----------------------------------------------------------------------
166-
void do_start(const network::result_handler& handler) NOEXCEPT override;
167-
void do_run(const network::result_handler& handler) NOEXCEPT override;
167+
void do_start(const result_handler& handler) NOEXCEPT override;
168+
void do_run(const result_handler& handler) NOEXCEPT override;
168169
void do_close() NOEXCEPT override;
169170

170171
private:
172+
void start_explore(const code& ec, const result_handler& handler) NOEXCEPT;
173+
171174
void do_subscribe_events(const event_notifier& handler,
172175
const event_completer& complete) NOEXCEPT;
173176
void do_notify(const code& ec, chase event_, event_value value) NOEXCEPT;

include/bitcoin/node/protocols/protocol.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
namespace libbitcoin {
3030
namespace node {
3131

32+
///////////////////////////////////////////////////////////////////////////////
33+
// TODO: ... -> node::protocol_tcp -> node::protocol -> network::protocol. //
34+
// TODO: ... -> node::protocol_peer -> node::protocol -> network::protocol. //
35+
///////////////////////////////////////////////////////////////////////////////
36+
3237
/// Abstract base for node protocols, thread safe.
3338
class BCN_API protocol
3439
: public network::protocol_peer

include/bitcoin/node/sessions/attach.hpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define LIBBITCOIN_NODE_SESSIONS_ATTACH_HPP
2121

2222
#include <memory>
23+
#include <utility>
2324
#include <bitcoin/network.hpp>
2425
#include <bitcoin/node/define.hpp>
2526
#include <bitcoin/node/protocols/protocols.hpp>
@@ -30,7 +31,7 @@ namespace node {
3031

3132
class full_node;
3233

33-
/// Session base class template for protocol attachment.
34+
/// Session base class template for network session attachment.
3435
/// node::session does not derive from network::session (siblings).
3536
/// This avoids the diamond inheritance problem between network/node.
3637
/// Protocol contructors are templatized on Session, obtaining session.
@@ -39,8 +40,10 @@ class attach
3940
: public Session, public node::session
4041
{
4142
public:
42-
attach(full_node& node, uint64_t identifier) NOEXCEPT
43-
: Session(node, identifier), session(node)
43+
template <typename... Args>
44+
attach(full_node& node, uint64_t identifier, Args&&... args) NOEXCEPT
45+
: Session(node, identifier, std::forward<Args>(args)...),
46+
node::session(node)
4447
{
4548
}
4649

@@ -56,21 +59,22 @@ class attach
5659
Session::attach_handshake(channel, std::move(handler));
5760
}
5861

59-
void attach_protocols(const network::channel::ptr& channel) NOEXCEPT override
62+
void attach_protocols(
63+
const network::channel::ptr& channel) NOEXCEPT override
6064
{
6165
using namespace system;
6266
using namespace network::messages::peer;
63-
const auto relay = config().network.enable_relay;
64-
const auto delay = config().node.delay_inbound;
65-
const auto headers = config().node.headers_first;
67+
const auto relay = this->config().network.enable_relay;
68+
const auto delay = this->config().node.delay_inbound;
69+
const auto headers = this->config().node.headers_first;
6670
const auto node_network = to_bool(bit_and<uint64_t>
6771
(
68-
config().network.services_maximum,
72+
this->config().network.services_maximum,
6973
network::messages::peer::service::node_network
7074
));
7175
const auto node_client_filters = to_bool(bit_and<uint64_t>
7276
(
73-
config().network.services_maximum,
77+
this->config().network.services_maximum,
7478
network::messages::peer::service::node_client_filters
7579
));
7680

@@ -158,12 +162,12 @@ class attach
158162
}
159163
}
160164

161-
network::channel::ptr create_channel(const network::socket::ptr& socket) NOEXCEPT override
165+
network::channel::ptr create_channel(
166+
const network::socket::ptr& socket) NOEXCEPT override
162167
{
163168
return std::static_pointer_cast<network::channel>(
164-
std::make_shared<node::channel_peer>(node::session::get_memory(),
165-
network::session::log, socket, node::session::config(),
166-
network::session::create_key()));
169+
std::make_shared<node::channel_peer>(this->get_memory(), this->log,
170+
socket, this->config(), this->create_key()));
167171
}
168172
};
169173

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
#ifndef LIBBITCOIN_NODE_SESSIONS_SESSION_TCP_HPP
20+
#define LIBBITCOIN_NODE_SESSIONS_SESSION_TCP_HPP
21+
22+
#include <bitcoin/network.hpp>
23+
#include <bitcoin/node/define.hpp>
24+
#include <bitcoin/node/protocols/protocols.hpp>
25+
26+
namespace libbitcoin {
27+
namespace node {
28+
29+
class full_node;
30+
31+
class session_tcp
32+
: public network::session_tcp, public node::session
33+
{
34+
public:
35+
typedef std::shared_ptr<session_tcp> ptr;
36+
using options_t = network::session_tcp::options_t;
37+
38+
session_tcp(full_node& node, uint64_t identifier,
39+
const options_t& options) NOEXCEPT;
40+
41+
protected:
42+
inline bool enabled() const NOEXCEPT override;
43+
};
44+
45+
// TODO: move all sessions up from network.
46+
////using session_web = network::session_server<protocol_web, session_tcp>;
47+
using session_explore = network::session_server<protocol_explore, session_tcp>;
48+
////using session_websocket = network::session_server<protocol_websocket, session_tcp>;
49+
////using session_bitcoind = network::session_server<protocol_bitcoind, session_tcp>;
50+
////using session_electrum = network::session_server<protocol_electrum, session_tcp>;
51+
////using session_stratum_v1 = network::session_server<protocol_stratum_v1, session_tcp>;
52+
////using session_stratum_v2 = network::session_server<protocol_stratum_v2, session_tcp>;
53+
54+
} // namespace node
55+
} // namespace libbitcoin
56+
57+
#endif

include/bitcoin/node/sessions/sessions.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
#include <bitcoin/node/sessions/session_inbound.hpp>
2525
#include <bitcoin/node/sessions/session_manual.hpp>
2626
#include <bitcoin/node/sessions/session_outbound.hpp>
27+
#include <bitcoin/node/sessions/session_tcp.hpp>
2728

2829
#endif

0 commit comments

Comments
 (0)