diff --git a/include/bitcoin/network/define.hpp b/include/bitcoin/network/define.hpp index 6019813c8..524e08fba 100644 --- a/include/bitcoin/network/define.hpp +++ b/include/bitcoin/network/define.hpp @@ -122,11 +122,13 @@ namespace network { std::forward(args)...) #define BIND_THIS(method, args) \ std::bind(std::forward(method), static_cast(this), \ - std::forward(args)...) + std::forward(args)...) #define BIND(method, ...) \ bind(&CLASS::method, __VA_ARGS__) #define POST(method, ...) \ post(&CLASS::method, __VA_ARGS__) +#define PARALLEL(method, ...) \ + parallel(&CLASS::method, __VA_ARGS__) } // namespace network } // namespace libbitcoin diff --git a/include/bitcoin/network/net/proxy.hpp b/include/bitcoin/network/net/proxy.hpp index 943322e58..6f528729d 100644 --- a/include/bitcoin/network/net/proxy.hpp +++ b/include/bitcoin/network/net/proxy.hpp @@ -72,6 +72,9 @@ class BCT_API proxy /// The channel strand. asio::strand& strand() NOEXCEPT; + /// Get the network threadpool iocontext. + asio::io_context& service() const NOEXCEPT; + /// The strand is running in this thread. bool stranded() const NOEXCEPT; diff --git a/include/bitcoin/network/net/socket.hpp b/include/bitcoin/network/net/socket.hpp index b07c8694d..9a34d7796 100644 --- a/include/bitcoin/network/net/socket.hpp +++ b/include/bitcoin/network/net/socket.hpp @@ -130,6 +130,9 @@ class BCT_API socket /// Get the strand of the socket. virtual asio::strand& strand() NOEXCEPT; + /// Get the network threadpool iocontext. + virtual asio::io_context& service() const NOEXCEPT; + protected: /// The socket was upgraded to a websocket (requires strand). virtual bool websocket() const NOEXCEPT; @@ -204,6 +207,7 @@ class BCT_API socket protected: // These are thread safe. asio::strand strand_; + asio::io_context& service_; std::atomic_bool stopped_{}; // These are protected by strand (see also handle_accept). diff --git a/include/bitcoin/network/protocols/protocol.hpp b/include/bitcoin/network/protocols/protocol.hpp index 68ec43978..8432eee2a 100644 --- a/include/bitcoin/network/protocols/protocol.hpp +++ b/include/bitcoin/network/protocols/protocol.hpp @@ -73,14 +73,14 @@ class BCT_API protocol /// Messaging. /// ----------------------------------------------------------------------- - /// Bind a method in base or derived class (use BIND). + /// Bind base or derived class method (use BIND). template inline auto bind(Method&& method, Args&&... args) NOEXCEPT { return BIND_SHARED(method, args); } - /// Post a method in base or derived class to channel strand (use POST). + /// Post base or derived class method to channel strand (use POST). template inline auto post(Method&& method, Args&&... args) NOEXCEPT { @@ -88,6 +88,14 @@ class BCT_API protocol BIND_SHARED(method, args)); } + /// Post base or derived class method to network threadpool (use PARALLEL). + template + inline auto parallel(Method&& method, Args&&... args) NOEXCEPT + { + return boost::asio::post(channel_->service(), + BIND_SHARED(method, args)); + } + /// Subscribe to messages broadcasts by type (use SUBSCRIBE_BROADCAST). /// Method is invoked with error::subscriber_stopped if already stopped. template diff --git a/src/net/proxy.cpp b/src/net/proxy.cpp index 55e545330..d6a473a8a 100644 --- a/src/net/proxy.cpp +++ b/src/net/proxy.cpp @@ -322,6 +322,11 @@ asio::strand& proxy::strand() NOEXCEPT return socket_->strand(); } +asio::io_context& proxy::service() const NOEXCEPT +{ + return socket_->service(); +} + bool proxy::stranded() const NOEXCEPT { return socket_->stranded(); diff --git a/src/net/socket.cpp b/src/net/socket.cpp index 285c316c4..a6806ce19 100644 --- a/src/net/socket.cpp +++ b/src/net/socket.cpp @@ -52,6 +52,7 @@ socket::socket(const logger& log, asio::io_context& service, const config::address& address) NOEXCEPT : strand_(service.get_executor()), socket_(strand_), + service_(service), address_(address), reporter(log), tracker(log) @@ -664,6 +665,11 @@ asio::strand& socket::strand() NOEXCEPT return strand_; } +asio::io_context& socket::service() const NOEXCEPT +{ + return service_; +} + // Websocket properties. // ----------------------------------------------------------------------------