Skip to content

Commit cb59784

Browse files
committed
Use std::bind, capture shared.
1 parent 79db31e commit cb59784

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

include/bitcoin/network/channels/channel.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class BCT_API channel
109109
void start_inactivity() NOEXCEPT;
110110
void handle_inactivity(const code& ec) NOEXCEPT;
111111

112+
void handle_monitor(const code& ec) NOEXCEPT;
113+
112114
// These are thread safe (const).
113115
const settings_t& settings_;
114116
const uint64_t identifier_;

src/channels/channel.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
namespace libbitcoin {
2828
namespace network {
2929

30+
#define CLASS channel
31+
3032
using namespace system;
3133
using namespace std::placeholders;
3234

@@ -94,24 +96,16 @@ void channel::monitor(bool value) NOEXCEPT
9496
{
9597
BC_ASSERT(stranded());
9698

97-
if (value)
98-
{
99-
// Invoke stop only if was *not* canceled (by cancel/stop).
100-
wait([&](const code& ec) NOEXCEPT
101-
{
102-
LOGA("wait [" << authority() << "] " << ec.message());
103-
if (ec) stop(ec);
104-
});
105-
}
106-
else
107-
{
108-
// Invokes wait handler with operation_canceled (stop on fail).
109-
cancel([&](const code& ec) NOEXCEPT
110-
{
111-
LOGA("cancel [" << authority() << "] " << ec.message());
112-
if (ec) stop(ec);
113-
});
114-
}
99+
auto handler = std::bind(&channel::handle_monitor,
100+
shared_from_base<channel>(), _1);
101+
102+
value ? wait(std::move(handler)) : cancel(std::move(handler));
103+
}
104+
105+
void channel::handle_monitor(const code& ec) NOEXCEPT
106+
{
107+
BC_ASSERT(stranded());
108+
if (ec) stop(ec);
115109
}
116110

117111
// Timers.

0 commit comments

Comments
 (0)