diff --git a/Makefile.am b/Makefile.am index d14558119..5440051e2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -159,6 +159,7 @@ include_bitcoin_node_HEADERS = \ include_bitcoin_node_channelsdir = ${includedir}/bitcoin/node/channels include_bitcoin_node_channels_HEADERS = \ include/bitcoin/node/channels/channel.hpp \ + include/bitcoin/node/channels/channel_http.hpp \ include/bitcoin/node/channels/channel_peer.hpp \ include/bitcoin/node/channels/channel_tcp.hpp \ include/bitcoin/node/channels/channels.hpp @@ -185,22 +186,30 @@ include_bitcoin_node_impl_chasers_HEADERS = \ include_bitcoin_node_protocolsdir = ${includedir}/bitcoin/node/protocols include_bitcoin_node_protocols_HEADERS = \ include/bitcoin/node/protocols/protocol.hpp \ + include/bitcoin/node/protocols/protocol_bitcoind.hpp \ include/bitcoin/node/protocols/protocol_block_in_106.hpp \ include/bitcoin/node/protocols/protocol_block_in_31800.hpp \ include/bitcoin/node/protocols/protocol_block_out_106.hpp \ include/bitcoin/node/protocols/protocol_block_out_70012.hpp \ + include/bitcoin/node/protocols/protocol_electrum.hpp \ include/bitcoin/node/protocols/protocol_explore.hpp \ include/bitcoin/node/protocols/protocol_filter_out_70015.hpp \ include/bitcoin/node/protocols/protocol_header_in_31800.hpp \ include/bitcoin/node/protocols/protocol_header_in_70012.hpp \ include/bitcoin/node/protocols/protocol_header_out_31800.hpp \ include/bitcoin/node/protocols/protocol_header_out_70012.hpp \ + include/bitcoin/node/protocols/protocol_html.hpp \ + include/bitcoin/node/protocols/protocol_http.hpp \ include/bitcoin/node/protocols/protocol_observer.hpp \ include/bitcoin/node/protocols/protocol_peer.hpp \ include/bitcoin/node/protocols/protocol_performer.hpp \ + include/bitcoin/node/protocols/protocol_stratum_v1.hpp \ + include/bitcoin/node/protocols/protocol_stratum_v2.hpp \ include/bitcoin/node/protocols/protocol_tcp.hpp \ include/bitcoin/node/protocols/protocol_transaction_in_106.hpp \ include/bitcoin/node/protocols/protocol_transaction_out_106.hpp \ + include/bitcoin/node/protocols/protocol_web.hpp \ + include/bitcoin/node/protocols/protocol_websocket.hpp \ include/bitcoin/node/protocols/protocols.hpp include_bitcoin_node_sessionsdir = ${includedir}/bitcoin/node/sessions @@ -210,6 +219,7 @@ include_bitcoin_node_sessions_HEADERS = \ include/bitcoin/node/sessions/session_manual.hpp \ include/bitcoin/node/sessions/session_outbound.hpp \ include/bitcoin/node/sessions/session_peer.hpp \ + include/bitcoin/node/sessions/session_server.hpp \ include/bitcoin/node/sessions/session_tcp.hpp \ include/bitcoin/node/sessions/sessions.hpp diff --git a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj index 1f600456b..6ebad56b5 100644 --- a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj +++ b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj @@ -166,6 +166,7 @@ + @@ -189,28 +190,37 @@ + + + + + + + + + diff --git a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters index cc0278eea..500acd61a 100644 --- a/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters +++ b/builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters @@ -185,6 +185,9 @@ include\bitcoin\node\channels + + include\bitcoin\node\channels + include\bitcoin\node\channels @@ -254,6 +257,9 @@ include\bitcoin\node\protocols + + include\bitcoin\node\protocols + include\bitcoin\node\protocols @@ -266,6 +272,9 @@ include\bitcoin\node\protocols + + include\bitcoin\node\protocols + include\bitcoin\node\protocols @@ -284,6 +293,12 @@ include\bitcoin\node\protocols + + include\bitcoin\node\protocols + + + include\bitcoin\node\protocols + include\bitcoin\node\protocols @@ -293,6 +308,12 @@ include\bitcoin\node\protocols + + include\bitcoin\node\protocols + + + include\bitcoin\node\protocols + include\bitcoin\node\protocols @@ -302,6 +323,12 @@ include\bitcoin\node\protocols + + include\bitcoin\node\protocols + + + include\bitcoin\node\protocols + include\bitcoin\node\protocols @@ -320,6 +347,9 @@ include\bitcoin\node\sessions + + include\bitcoin\node\sessions + include\bitcoin\node\sessions diff --git a/include/bitcoin/node.hpp b/include/bitcoin/node.hpp index 9bd6b508c..55822b28e 100644 --- a/include/bitcoin/node.hpp +++ b/include/bitcoin/node.hpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -44,28 +45,37 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include #include #include #include +#include +#include #include #include #include +#include +#include #include #include #include +#include +#include #include #include #include #include #include #include +#include #include #include diff --git a/include/bitcoin/node/channels/channel.hpp b/include/bitcoin/node/channels/channel.hpp index 24338e996..96a59150b 100644 --- a/include/bitcoin/node/channels/channel.hpp +++ b/include/bitcoin/node/channels/channel.hpp @@ -34,14 +34,13 @@ class BCN_API channel public: typedef std::shared_ptr ptr; + DELETE_COPY_MOVE_DESTRUCT(channel); + protected: channel(const network::logger&, const network::socket::ptr&, const node::configuration&, uint64_t=zero) NOEXCEPT { } - - virtual ~channel() NOEXCEPT {} - }; } // namespace node diff --git a/include/bitcoin/node/channels/channel_http.hpp b/include/bitcoin/node/channels/channel_http.hpp new file mode 100644 index 000000000..18e3523aa --- /dev/null +++ b/include/bitcoin/node/channels/channel_http.hpp @@ -0,0 +1,52 @@ +/** + * 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 . + */ +#ifndef LIBBITCOIN_NODE_CHANNELS_CHANNEL_HTTP_HPP +#define LIBBITCOIN_NODE_CHANNELS_CHANNEL_HTTP_HPP + +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace node { + +/// Abstract base HTTP channel state for the node. +class BCN_API channel_http + : public network::channel_http, + public node::channel +{ +public: + typedef std::shared_ptr ptr; + using options_t = network::settings::http_server; + + channel_http(const network::logger& log, const network::socket::ptr& socket, + const node::configuration& config, uint64_t identifier=zero, + const options_t& options={}) NOEXCEPT + : network::channel_http(log, socket, config.network, identifier, options), + node::channel(log, socket, config, identifier) + { + } +}; + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/channels/channel_peer.hpp b/include/bitcoin/node/channels/channel_peer.hpp index 220d72c6c..754d55338 100644 --- a/include/bitcoin/node/channels/channel_peer.hpp +++ b/include/bitcoin/node/channels/channel_peer.hpp @@ -28,16 +28,22 @@ namespace libbitcoin { namespace node { -/// Peer channel state for the node. +/// Abstract base peer channel state for the node. class BCN_API channel_peer - : public network::channel_peer, node::channel + : public network::channel_peer, + public node::channel { public: typedef std::shared_ptr ptr; channel_peer(network::memory& memory, const network::logger& log, const network::socket::ptr& socket, const node::configuration& config, - uint64_t identifier=zero) NOEXCEPT; + uint64_t identifier=zero) NOEXCEPT + : network::channel_peer(memory, log, socket, config.network, identifier), + node::channel(log, socket, config, identifier), + announced_(config.node.announcement_cache) + { + } void set_announced(const system::hash_digest& hash) NOEXCEPT; bool was_announced(const system::hash_digest& hash) const NOEXCEPT; diff --git a/include/bitcoin/node/channels/channel_tcp.hpp b/include/bitcoin/node/channels/channel_tcp.hpp index d284ce311..8338c7df4 100644 --- a/include/bitcoin/node/channels/channel_tcp.hpp +++ b/include/bitcoin/node/channels/channel_tcp.hpp @@ -28,9 +28,10 @@ namespace libbitcoin { namespace node { -/// TCP channel state for the node. +/// Abstract base TCP channel state for the node. class BCN_API channel_tcp - : public network::channel_tcp, node::channel + : public network::channel_tcp, + public node::channel { public: typedef std::shared_ptr ptr; @@ -43,8 +44,6 @@ class BCN_API channel_tcp node::channel(log, socket, config, identifier) { } - - virtual ~channel_tcp() NOEXCEPT {} }; } // namespace node diff --git a/include/bitcoin/node/channels/channels.hpp b/include/bitcoin/node/channels/channels.hpp index 201d2d062..cd53d5c6c 100644 --- a/include/bitcoin/node/channels/channels.hpp +++ b/include/bitcoin/node/channels/channels.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #endif diff --git a/include/bitcoin/node/protocols/protocol.hpp b/include/bitcoin/node/protocols/protocol.hpp index 88ebb819f..183d1106c 100644 --- a/include/bitcoin/node/protocols/protocol.hpp +++ b/include/bitcoin/node/protocols/protocol.hpp @@ -47,6 +47,8 @@ class BCN_API protocol protected: typedef std::shared_ptr ptr; + DELETE_COPY_MOVE_DESTRUCT(protocol); + /// Constructors. /// ----------------------------------------------------------------------- @@ -58,8 +60,6 @@ class BCN_API protocol { } - virtual ~protocol() NOEXCEPT {} - /// Properties. /// ----------------------------------------------------------------------- diff --git a/include/bitcoin/node/protocols/protocol_bitcoind.hpp b/include/bitcoin/node/protocols/protocol_bitcoind.hpp new file mode 100644 index 000000000..94a3b2a69 --- /dev/null +++ b/include/bitcoin/node/protocols/protocol_bitcoind.hpp @@ -0,0 +1,66 @@ +/** + * 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 . + */ +#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_BITCOIND_HPP +#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_BITCOIND_HPP + +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace node { + +class BCN_API protocol_bitcoind + : public node::protocol_http, + protected network::tracker +{ +public: + typedef std::shared_ptr ptr; + using options_t = network::settings::http_server; + using channel_t = node::channel_http; + + protocol_bitcoind(const auto& session, + const network::channel::ptr& channel, + const options_t& options) NOEXCEPT + : node::protocol_http(session, channel, options), + network::tracker(session->log) + { + } + + /// Public start is required. + void start() NOEXCEPT override + { + node::protocol_http::start(); + } + +////protected: +//// void handle_receive_get(const code& ec, +//// const network::http::method::get& request) NOEXCEPT override; + +private: + // This is thread safe. + ////const options_t& options_; +}; + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/protocols/protocol_electrum.hpp b/include/bitcoin/node/protocols/protocol_electrum.hpp new file mode 100644 index 000000000..05621849b --- /dev/null +++ b/include/bitcoin/node/protocols/protocol_electrum.hpp @@ -0,0 +1,62 @@ +/** + * 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 . + */ +#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_ELECTRUM_HPP +#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_ELECTRUM_HPP + +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace node { + +class BCN_API protocol_electrum + : public node::protocol_tcp, + protected network::tracker +{ +public: + typedef std::shared_ptr ptr; + using options_t = network::settings::tcp_server; + using channel_t = node::channel_tcp; + + protocol_electrum(const auto& session, + const network::channel::ptr& channel, + const options_t& options) NOEXCEPT + : node::protocol_tcp(session, channel, options), + network::tracker(session->log) + { + } + + /// Public start is required. + void start() NOEXCEPT override + { + node::protocol_tcp::start(); + } + +private: + // This is thread safe. + ////const options_t& options_; +}; + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/protocols/protocol_explore.hpp b/include/bitcoin/node/protocols/protocol_explore.hpp index f84525511..6659acd61 100644 --- a/include/bitcoin/node/protocols/protocol_explore.hpp +++ b/include/bitcoin/node/protocols/protocol_explore.hpp @@ -21,30 +21,36 @@ #include #include +#include #include -#include +#include namespace libbitcoin { namespace node { class BCN_API protocol_explore - : public network::protocol_html, + : public node::protocol_html, protected network::tracker { public: typedef std::shared_ptr ptr; using options_t = network::settings::html_server; - using channel_t = network::channel_http; + using channel_t = node::channel_http; protocol_explore(const auto& session, const network::channel::ptr& channel, const options_t& options) NOEXCEPT - : network::protocol_html(session, channel, options), - ////options_(options), + : node::protocol_html(session, channel, options), network::tracker(session->log) { } + /// Public start is required. + void start() NOEXCEPT override + { + node::protocol_html::start(); + } + protected: void handle_receive_get(const code& ec, const network::http::method::get& request) NOEXCEPT override; diff --git a/include/bitcoin/node/protocols/protocol_html.hpp b/include/bitcoin/node/protocols/protocol_html.hpp new file mode 100644 index 000000000..8ffbb4071 --- /dev/null +++ b/include/bitcoin/node/protocols/protocol_html.hpp @@ -0,0 +1,52 @@ +/** + * 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 . + */ +#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_HTML_HPP +#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_HTML_HPP + +#include +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace node { + +/// Abstract base for HTML protocols, thread safe. +class BCN_API protocol_html + : public network::protocol_html, + public node::protocol +{ +protected: + typedef std::shared_ptr ptr; + + protocol_html(const auto& session, + const network::channel::ptr& channel, + const options_t& options) NOEXCEPT + : network::protocol_html(session, channel, options), + node::protocol(session, channel) + { + } +}; + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/protocols/protocol_http.hpp b/include/bitcoin/node/protocols/protocol_http.hpp new file mode 100644 index 000000000..0ea0492f4 --- /dev/null +++ b/include/bitcoin/node/protocols/protocol_http.hpp @@ -0,0 +1,61 @@ +/** + * 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 . + */ +#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_HTTP_HPP +#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_HTTP_HPP + +#include +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace node { + +/// Abstract base for HTTP protocols, thread safe. +class BCN_API protocol_http + : public network::protocol_http, + public node::protocol +{ +protected: + typedef std::shared_ptr ptr; + + protocol_http(const auto& session, + const network::channel::ptr& channel, + const options_t& options) NOEXCEPT + : network::protocol_http(session, channel, options), + node::protocol(session, channel), + channel_(std::static_pointer_cast(channel)), + session_(session) + { + } + +private: + // This derived channel requires stranded calls, base is thread safe. + const node::channel_tcp::ptr channel_; + + // This is thread safe. + const session::ptr session_; +}; + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/protocols/protocol_peer.hpp b/include/bitcoin/node/protocols/protocol_peer.hpp index a7e2e526c..263c20443 100644 --- a/include/bitcoin/node/protocols/protocol_peer.hpp +++ b/include/bitcoin/node/protocols/protocol_peer.hpp @@ -31,7 +31,8 @@ namespace node { /// Abstract base for node peer protocols, thread safe. class BCN_API protocol_peer - : public network::protocol_peer, public node::protocol + : public network::protocol_peer, + public node::protocol { protected: /// Constructors. @@ -47,8 +48,6 @@ class BCN_API protocol_peer { } - virtual ~protocol_peer() NOEXCEPT {} - /// Organizers. /// ----------------------------------------------------------------------- diff --git a/include/bitcoin/node/protocols/protocol_stratum_v1.hpp b/include/bitcoin/node/protocols/protocol_stratum_v1.hpp new file mode 100644 index 000000000..b378b7b56 --- /dev/null +++ b/include/bitcoin/node/protocols/protocol_stratum_v1.hpp @@ -0,0 +1,63 @@ +/** + * 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 . + */ +#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_STRATUM_V1_HPP +#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_STRATUM_V1_HPP + +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace node { + +class BCN_API protocol_stratum_v1 + : public node::protocol_tcp, + protected network::tracker +{ +public: + typedef std::shared_ptr ptr; + using options_t = network::settings::tcp_server; + using channel_t = node::channel_tcp; + + protocol_stratum_v1(const auto& session, + const network::channel::ptr& channel, + const options_t& options) NOEXCEPT + : node::protocol_tcp(session, channel, options), + ////options_(options), + network::tracker(session->log) + { + } + + /// Public start is required. + void start() NOEXCEPT override + { + node::protocol_tcp::start(); + } + +private: + // This is thread safe. + ////const options_t& options_; +}; + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/protocols/protocol_stratum_v2.hpp b/include/bitcoin/node/protocols/protocol_stratum_v2.hpp new file mode 100644 index 000000000..eb214bbc9 --- /dev/null +++ b/include/bitcoin/node/protocols/protocol_stratum_v2.hpp @@ -0,0 +1,63 @@ +/** + * 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 . + */ +#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_STRATUM_V2_HPP +#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_STRATUM_V2_HPP + +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace node { + +class BCN_API protocol_stratum_v2 + : public node::protocol_tcp, + protected network::tracker +{ +public: + typedef std::shared_ptr ptr; + using options_t = network::settings::tcp_server; + using channel_t = node::channel_tcp; + + protocol_stratum_v2(const auto& session, + const network::channel::ptr& channel, + const options_t& options) NOEXCEPT + : node::protocol_tcp(session, channel, options), + ////options_(options), + network::tracker(session->log) + { + } + + /// Public start is required. + void start() NOEXCEPT override + { + node::protocol_tcp::start(); + } + +private: + // This is thread safe. + ////const options_t& options_; +}; + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/protocols/protocol_tcp.hpp b/include/bitcoin/node/protocols/protocol_tcp.hpp index 46dc189d9..5bdcc8b38 100644 --- a/include/bitcoin/node/protocols/protocol_tcp.hpp +++ b/include/bitcoin/node/protocols/protocol_tcp.hpp @@ -31,22 +31,22 @@ namespace node { /// Abstract base for TCP protocols, thread safe. class BCN_API protocol_tcp - : public network::protocol_tcp, public node::protocol + : public network::protocol_tcp, + public node::protocol { protected: typedef std::shared_ptr ptr; protocol_tcp(const auto& session, - const network::channel::ptr& channel) NOEXCEPT - : network::protocol_tcp(session, channel), + const network::channel::ptr& channel, + const options_t& options) NOEXCEPT + : network::protocol_tcp(session, channel, options), node::protocol(session, channel), channel_(std::static_pointer_cast(channel)), session_(session) { } - virtual ~protocol_tcp() NOEXCEPT {} - private: // This derived channel requires stranded calls, base is thread safe. const node::channel_tcp::ptr channel_; diff --git a/include/bitcoin/node/protocols/protocol_web.hpp b/include/bitcoin/node/protocols/protocol_web.hpp new file mode 100644 index 000000000..5e3616a90 --- /dev/null +++ b/include/bitcoin/node/protocols/protocol_web.hpp @@ -0,0 +1,66 @@ +/** + * 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 . + */ +#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_WEB_HPP +#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_WEB_HPP + +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace node { + +class BCN_API protocol_web + : public node::protocol_html, + protected network::tracker +{ +public: + typedef std::shared_ptr ptr; + using options_t = network::settings::html_server; + using channel_t = node::channel_http; + + protocol_web(const auto& session, + const network::channel::ptr& channel, + const options_t& options) NOEXCEPT + : node::protocol_html(session, channel, options), + network::tracker(session->log) + { + } + + /// Public start is required. + void start() NOEXCEPT override + { + node::protocol_html::start(); + } + +////protected: +//// void handle_receive_get(const code& ec, +//// const network::http::method::get& request) NOEXCEPT override; + +private: + // This is thread safe. + ////const options_t& options_; +}; + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/protocols/protocol_websocket.hpp b/include/bitcoin/node/protocols/protocol_websocket.hpp new file mode 100644 index 000000000..5b6085fe9 --- /dev/null +++ b/include/bitcoin/node/protocols/protocol_websocket.hpp @@ -0,0 +1,62 @@ +/** + * 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 . + */ +#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_WEBSOCKET_HPP +#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_WEBSOCKET_HPP + +#include +#include +#include +#include +#include + +namespace libbitcoin { +namespace node { + +class BCN_API protocol_websocket + : public node::protocol_http, + protected network::tracker +{ +public: + typedef std::shared_ptr ptr; + using options_t = network::settings::http_server; + using channel_t = node::channel_http; + + protocol_websocket(const auto& session, + const network::channel::ptr& channel, + const options_t& options) NOEXCEPT + : node::protocol_http(session, channel, options), + network::tracker(session->log) + { + } + + /// Public start is required. + void start() NOEXCEPT override + { + node::protocol_http::start(); + } + +private: + // This is thread safe. + ////const options_t& options_; +}; + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/protocols/protocols.hpp b/include/bitcoin/node/protocols/protocols.hpp index 3cf6c2989..9ffd30fff 100644 --- a/include/bitcoin/node/protocols/protocols.hpp +++ b/include/bitcoin/node/protocols/protocols.hpp @@ -19,22 +19,39 @@ #ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOLS_HPP #define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOLS_HPP +/// node base #include + +/// peer base +#include + +/// peer #include #include #include #include -#include #include #include #include #include #include #include -#include #include -#include #include #include +/// server base +#include +#include +#include + +/// server +#include +#include +#include +#include +#include +#include +#include + #endif diff --git a/include/bitcoin/node/sessions/session.hpp b/include/bitcoin/node/sessions/session.hpp index f449617c1..22f0afa74 100644 --- a/include/bitcoin/node/sessions/session.hpp +++ b/include/bitcoin/node/sessions/session.hpp @@ -36,7 +36,7 @@ class BCN_API session public: typedef std::shared_ptr ptr; - DELETE_COPY_MOVE(session); + DELETE_COPY_MOVE_DESTRUCT(session); /// Organizers. /// ----------------------------------------------------------------------- @@ -112,7 +112,6 @@ class BCN_API session /// ----------------------------------------------------------------------- session(full_node& node) NOEXCEPT; - ~session() NOEXCEPT; private: void do_subscribe_events(const event_notifier& handler, diff --git a/include/bitcoin/node/sessions/session_peer.hpp b/include/bitcoin/node/sessions/session_peer.hpp index 61d0163be..34e2eace3 100644 --- a/include/bitcoin/node/sessions/session_peer.hpp +++ b/include/bitcoin/node/sessions/session_peer.hpp @@ -34,7 +34,8 @@ namespace node { /// This avoids the diamond inheritance problem between network/node. template class session_peer - : public Session, public node::session + : public Session, + public node::session { public: template diff --git a/include/bitcoin/node/sessions/session_server.hpp b/include/bitcoin/node/sessions/session_server.hpp new file mode 100644 index 000000000..c41d81210 --- /dev/null +++ b/include/bitcoin/node/sessions/session_server.hpp @@ -0,0 +1,115 @@ +/** + * 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 . + */ +#ifndef LIBBITCOIN_NODE_SESSION_SERVER_HPP +#define LIBBITCOIN_NODE_SESSION_SERVER_HPP + +#include +#include +#include +#include + +namespace libbitcoin { +namespace node { + +// make_shared<> +BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) + +/// Declare a concrete instance of this type for client-server protocols built +/// on tcp/ip. session_tcp processing performs all connection management and +/// session tracking. This includes start/stop/disable/enable/black/whitelist. +/// Protocol must declare options_t and channel_t. This protocol is constructed +/// and attached to a constructed instance of channel_t. The protocol construct +/// and attachment can be overridden and/or augmented with other protocols. +template +class session_server + : public session_tcp, + protected network::tracker> +{ +public: + typedef std::shared_ptr> ptr; + + /// The protocol must define these public types. + using options_t = typename Protocol::options_t; + using channel_t = typename Protocol::channel_t; + + /// Construct an instance (network should be started). + template + session_server(Node& node, uint64_t identifier, const options_t& options, + Args&&... args) NOEXCEPT + : session_tcp(node, identifier, options, std::forward(args)...), + options_(options), + network::tracker>(node) + { + } + +protected: + using socket_ptr = network::socket::ptr; + using channel_ptr = network::channel::ptr; + + /// Override to construct channel. This allows the implementation to pass + /// other values to protocol construction and/or select the desired channel + /// based on available factors (e.g. a distinct protocol version). + channel_ptr create_channel(const socket_ptr& socket) NOEXCEPT override + { + BC_ASSERT_MSG(stranded(), "strand"); + + const auto channel = std::make_shared(log, socket, config(), + create_key(), options_); + + return std::static_pointer_cast(channel); + } + + /// Override to implement a connection handshake as required. By default + /// this is bypassed, which applies to basic http services. A handshake + /// is used to implement TLS and WebSocket upgrade from http (for example). + /// Handshake protocol(s) must invoke handler one time at completion. + /// Use std::dynamic_pointer_cast(channel) to obtain channel_t. + void attach_handshake(const channel_ptr& channel, + network::result_handler&& handler) NOEXCEPT override + { + BC_ASSERT_MSG(channel->stranded(), "channel strand"); + BC_ASSERT_MSG(channel->paused(), "channel not paused for handshake"); + + session_tcp::attach_handshake(channel, std::move(handler)); + } + + /// Overridden to set channel protocols. This allows the implementation to + /// pass other values to protocol construction and/or select the desired + /// protocol based on available factors (e.g. a distinct protocol version). + /// Use std::dynamic_pointer_cast(channel) to obtain channel_t. + void attach_protocols(const channel_ptr& channel) NOEXCEPT override + { + BC_ASSERT_MSG(channel->stranded(), "channel strand"); + BC_ASSERT_MSG(channel->paused(), "channel not paused for protocols"); + + const auto self = shared_from_base(); + channel->attach(self, options_)->start(); + } + +private: + // This is thread safe. + const options_t& options_; +}; + +BC_POP_WARNING() + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/sessions/session_tcp.hpp b/include/bitcoin/node/sessions/session_tcp.hpp index 18aa7fb92..386fa483a 100644 --- a/include/bitcoin/node/sessions/session_tcp.hpp +++ b/include/bitcoin/node/sessions/session_tcp.hpp @@ -21,7 +21,7 @@ #include #include -#include +#include namespace libbitcoin { namespace node { @@ -29,28 +29,27 @@ namespace node { class full_node; class session_tcp - : public network::session_tcp, public node::session + : public network::session_tcp, + public node::session { public: typedef std::shared_ptr ptr; using options_t = network::session_tcp::options_t; session_tcp(full_node& node, uint64_t identifier, - const options_t& options) NOEXCEPT; + const options_t& options) NOEXCEPT + : network::session_tcp( + (network::net&)node, + identifier, + (const network::settings::tcp_server&)options), + node::session(node) + { + } protected: bool enabled() const NOEXCEPT override; }; -// TODO: implement specialized protocol instances, like node::protocol_explore. -using session_web = network::session_server; -using session_explore = network::session_server; -using session_websocket = network::session_server; -using session_bitcoind = network::session_server; -using session_electrum = network::session_server; -using session_stratum_v1 = network::session_server; -using session_stratum_v2 = network::session_server; - } // namespace node } // namespace libbitcoin diff --git a/include/bitcoin/node/sessions/sessions.hpp b/include/bitcoin/node/sessions/sessions.hpp index 22aa8e5d3..84669534e 100644 --- a/include/bitcoin/node/sessions/sessions.hpp +++ b/include/bitcoin/node/sessions/sessions.hpp @@ -24,6 +24,22 @@ #include #include #include +#include #include +namespace libbitcoin { +namespace node { + +// Alias server sessions derived from session_tcp. +using session_web = session_server; +using session_explore = session_server; +using session_websocket = session_server; +using session_bitcoind = session_server; +using session_electrum = session_server; +using session_stratum_v1 = session_server; +using session_stratum_v2 = session_server; + +} // namespace network +} // namespace libbitcoin + #endif diff --git a/src/channels/channel_peer.cpp b/src/channels/channel_peer.cpp index f0832a280..a9ce4913c 100644 --- a/src/channels/channel_peer.cpp +++ b/src/channels/channel_peer.cpp @@ -18,6 +18,7 @@ */ #include +#include #include #include #include @@ -30,16 +31,6 @@ using namespace network; BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) -// Capture configured buffer size. -channel_peer::channel_peer(memory& memory, const logger& log, - const socket::ptr& socket, const node::configuration& config, - uint64_t identifier) NOEXCEPT - : network::channel_peer(memory, log, socket, config.network, identifier), - node::channel(log, socket, config, identifier), - announced_(config.node.announcement_cache) -{ -} - void channel_peer::set_announced(const hash_digest& hash) NOEXCEPT { BC_ASSERT(stranded()); diff --git a/src/sessions/session.cpp b/src/sessions/session.cpp index d6a9e424a..d15848208 100644 --- a/src/sessions/session.cpp +++ b/src/sessions/session.cpp @@ -41,10 +41,6 @@ session::session(full_node& node) NOEXCEPT { } -session::~session() NOEXCEPT -{ -} - // Organizers. // ---------------------------------------------------------------------------- diff --git a/src/sessions/session_tcp.cpp b/src/sessions/session_tcp.cpp index 842a92120..ff00f71c6 100644 --- a/src/sessions/session_tcp.cpp +++ b/src/sessions/session_tcp.cpp @@ -23,13 +23,6 @@ namespace libbitcoin { namespace node { -session_tcp::session_tcp(full_node& node, uint64_t identifier, - const options_t& options) NOEXCEPT - : network::session_tcp(node, identifier, options), - node::session(node) -{ -} - // Inbound connection attempts are dropped unless confirmed chain is current. // Used instead of suspension because suspension has independent start/stop. bool session_tcp::enabled() const NOEXCEPT