Skip to content

Commit 3203560

Browse files
authored
Merge pull request #655 from evoskuil/master
Define protocol::parallel<> and PARALLEL macro.
2 parents b013ec7 + 975f2b9 commit 3203560

File tree

6 files changed

+31
-3
lines changed

6 files changed

+31
-3
lines changed

include/bitcoin/network/define.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,13 @@ namespace network {
122122
std::forward<Args>(args)...)
123123
#define BIND_THIS(method, args) \
124124
std::bind(std::forward<Method>(method), static_cast<Derived*>(this), \
125-
std::forward<Args>(args)...)
125+
std::forward<Args>(args)...)
126126
#define BIND(method, ...) \
127127
bind<CLASS>(&CLASS::method, __VA_ARGS__)
128128
#define POST(method, ...) \
129129
post<CLASS>(&CLASS::method, __VA_ARGS__)
130+
#define PARALLEL(method, ...) \
131+
parallel<CLASS>(&CLASS::method, __VA_ARGS__)
130132

131133
} // namespace network
132134
} // namespace libbitcoin

include/bitcoin/network/net/proxy.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class BCT_API proxy
7272
/// The channel strand.
7373
asio::strand& strand() NOEXCEPT;
7474

75+
/// Get the network threadpool iocontext.
76+
asio::io_context& service() const NOEXCEPT;
77+
7578
/// The strand is running in this thread.
7679
bool stranded() const NOEXCEPT;
7780

include/bitcoin/network/net/socket.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ class BCT_API socket
130130
/// Get the strand of the socket.
131131
virtual asio::strand& strand() NOEXCEPT;
132132

133+
/// Get the network threadpool iocontext.
134+
virtual asio::io_context& service() const NOEXCEPT;
135+
133136
protected:
134137
/// The socket was upgraded to a websocket (requires strand).
135138
virtual bool websocket() const NOEXCEPT;
@@ -204,6 +207,7 @@ class BCT_API socket
204207
protected:
205208
// These are thread safe.
206209
asio::strand strand_;
210+
asio::io_context& service_;
207211
std::atomic_bool stopped_{};
208212

209213
// These are protected by strand (see also handle_accept).

include/bitcoin/network/protocols/protocol.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,29 @@ class BCT_API protocol
7373
/// Messaging.
7474
/// -----------------------------------------------------------------------
7575

76-
/// Bind a method in base or derived class (use BIND).
76+
/// Bind base or derived class method (use BIND).
7777
template <class Derived, typename Method, typename... Args>
7878
inline auto bind(Method&& method, Args&&... args) NOEXCEPT
7979
{
8080
return BIND_SHARED(method, args);
8181
}
8282

83-
/// Post a method in base or derived class to channel strand (use POST).
83+
/// Post base or derived class method to channel strand (use POST).
8484
template <class Derived, typename Method, typename... Args>
8585
inline auto post(Method&& method, Args&&... args) NOEXCEPT
8686
{
8787
return boost::asio::post(channel_->strand(),
8888
BIND_SHARED(method, args));
8989
}
9090

91+
/// Post base or derived class method to network threadpool (use PARALLEL).
92+
template <class Derived, typename Method, typename... Args>
93+
inline auto parallel(Method&& method, Args&&... args) NOEXCEPT
94+
{
95+
return boost::asio::post(channel_->service(),
96+
BIND_SHARED(method, args));
97+
}
98+
9199
/// Subscribe to messages broadcasts by type (use SUBSCRIBE_BROADCAST).
92100
/// Method is invoked with error::subscriber_stopped if already stopped.
93101
template <class Derived, class Message, typename Method, typename... Args>

src/net/proxy.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ asio::strand& proxy::strand() NOEXCEPT
322322
return socket_->strand();
323323
}
324324

325+
asio::io_context& proxy::service() const NOEXCEPT
326+
{
327+
return socket_->service();
328+
}
329+
325330
bool proxy::stranded() const NOEXCEPT
326331
{
327332
return socket_->stranded();

src/net/socket.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ socket::socket(const logger& log, asio::io_context& service,
5252
const config::address& address) NOEXCEPT
5353
: strand_(service.get_executor()),
5454
socket_(strand_),
55+
service_(service),
5556
address_(address),
5657
reporter(log),
5758
tracker<socket>(log)
@@ -664,6 +665,11 @@ asio::strand& socket::strand() NOEXCEPT
664665
return strand_;
665666
}
666667

668+
asio::io_context& socket::service() const NOEXCEPT
669+
{
670+
return service_;
671+
}
672+
667673
// Websocket properties.
668674
// ----------------------------------------------------------------------------
669675

0 commit comments

Comments
 (0)