• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

libbitcoin / libbitcoin-network / 46567279699

01 Dec 2026 03:16AM UTC coverage: 93.891% (-0.1%) from 94.013%
46567279699

push

github

web-flow
Merge pull request #1075 from evoskuil/master

Add canceled for timer handlers.

7 of 9 new or added lines in 4 files covered. (77.78%)

34 existing lines in 5 files now uncovered.

17890 of 19054 relevant lines covered (93.89%)

2048.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.37
/src/protocols/protocol.cpp
1
/**
2
 * Copyright (c) 2011-2026 libbitcoin developers
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
#include <bitcoin/network/protocols/protocol.hpp>
20

21
#include <bitcoin/network/channels/channels.hpp>
22
#include <bitcoin/network/config/config.hpp>
23
#include <bitcoin/network/define.hpp>
24
#include <bitcoin/network/log/log.hpp>
25
#include <bitcoin/network/messages/messages.hpp>
26
#include <bitcoin/network/net/net.hpp>
27
#include <bitcoin/network/sessions/sessions.hpp>
28

29
namespace libbitcoin {
30
namespace network {
31

32
#define CLASS protocol
33

34
using namespace system;
35
using namespace messages::peer;
36
using namespace std::placeholders;
37

38
// Protocols invoke channel stop for application layer protocol violations.
39
// Channels invoke channel stop for channel timeouts and communication failures.
40
protocol::protocol(const session::ptr& session,
267 ✔
41
    const channel::ptr& channel) NOEXCEPT
267 ✔
42
  : reporter(session->log),
267 ✔
43
    channel_(channel),
267 ✔
44
    session_(session)
267 ✔
45
{
46
}
267 ✔
47

48
protocol::~protocol() NOEXCEPT
801 ✔
49
{
50
    BC_ASSERT_MSG(stopped(), "protocol destruct before channel stop");
267 ✔
51
    if (!stopped()) { LOGF("~protocol is not stopped."); }
267 ✔
52
}
267 ✔
53

54
// Start/Stop.
55
// ----------------------------------------------------------------------------
56

57
void protocol::start() NOEXCEPT
264 ✔
58
{
59
    BC_ASSERT(stranded());
264 ✔
60
    started_ = true;
264 ✔
61
}
264 ✔
62

63
bool protocol::started() const NOEXCEPT
574 ✔
64
{
65
    BC_ASSERT(stranded());
574 ✔
66
    return started_;
574 ✔
67
}
68

69
// Utility to test for failure code or stopped.
70
// Any failure code from send or receive handler implies channel::proxy stop. So
71
// use this to test entry into those handlers. Timer handlers use canceled().
72
bool protocol::stopped(const code& ec) const NOEXCEPT
1,974 ✔
73
{
74
    return channel_->stopped() || ec;
1,974 ✔
75
}
76

77
// Utility for timer handlers, as a timer may be canceled while not stopped.
78
bool protocol::canceled(const code& ec) NOEXCEPT
36 ✔
79
{
80
    if (stopped() || ec == error::operation_canceled)
36 ✔
81
        return true;
29 ✔
82

83
    if (ec)
7 ✔
84
    {
NEW
85
        stop(ec);
×
NEW
86
        return true;
×
87
    }
88

89
    return false;
90
}
91

92
// Called from stop subscription instead of stop (which would be a cycle).
93
void protocol::stopping(const code&) NOEXCEPT
61 ✔
94
{
95
    BC_ASSERT(stranded());
61 ✔
96
}
61 ✔
97

98
// Stop channel::proxy, which results in protocol stop handler invocation.
99
// Stop handler invokes stopping(ec), for protocol cleanup operations.
100
void protocol::stop(const code& ec) NOEXCEPT
22 ✔
101
{
102
    channel_->stop(ec);
22 ✔
103
}
22 ✔
104

105
// Hold the dispatch gate until resume.
106
void protocol::pause() NOEXCEPT
77 ✔
107
{
108
    BC_ASSERT(stranded());
77 ✔
109
    channel_->pause();
77 ✔
110
}
77 ✔
111

112
const channel::gate_t::ptr& protocol::gate() NOEXCEPT
2 ✔
113
{
114
    BC_ASSERT(stranded());
2 ✔
115
    return channel_->gate();
2 ✔
116
}
117

118
// Zero if timer expired.
119
size_t protocol::remaining() const NOEXCEPT
2 ✔
120
{
121
    BC_ASSERT(stranded());
2 ✔
122
    return channel_->remaining();
2 ✔
123
}
124

125
// Properties.
126
// ----------------------------------------------------------------------------
127
// Public properties may be accessed outside strand, except during handshake
128
// protocol operation. Thread safety requires setters are never invoked outside
129
// of handshake protocol (start handler).
130

131
bool protocol::stranded() const NOEXCEPT
4,190 ✔
132
{
133
    return channel_->stranded();
4,190 ✔
134
}
135

136
config::endpoint protocol::opposite() const NOEXCEPT
110 ✔
137
{
138
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
139
    return { channel_->endpoint() };
110 ✔
140
    BC_POP_WARNING()
141
}
142

143
const config::address& protocol::outbound() const NOEXCEPT
115 ✔
144
{
145
    return channel_->address();
115 ✔
146
}
147

148
config::endpoint protocol::binding() const NOEXCEPT
2 ✔
149
{
150
    BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
151
    return { channel_->binding() };
2 ✔
152
    BC_POP_WARNING()
153
}
154

155
bool protocol::is_terminal(size_t slots) const NOEXCEPT
2 ✔
156
{
157
    const auto slot = channel_->slot();
2 ✔
158
    return slot != max_size_t && slot >= slots;
2 ✔
159
}
160

161
uint64_t protocol::nonce() const NOEXCEPT
110 ✔
162
{
163
    return channel_->nonce();
110 ✔
164
}
165

166
uint64_t protocol::sent() const NOEXCEPT
2 ✔
167
{
168
    return channel_->sent();
2 ✔
169
}
170

171
uint64_t protocol::received() const NOEXCEPT
2 ✔
172
{
173
    return channel_->received();
2 ✔
174
}
175

176
// The stamps are monotonic, so the wall time is derived from the elapsed.
177
static uint32_t to_unix(const steady_clock::time_point& stamp) NOEXCEPT
6 ✔
178
{
179
    const auto span = std::chrono::duration_cast<std::chrono::seconds>(
6 ✔
180
        steady_clock::now() - stamp);
6 ✔
181

182
    return floored_subtract(unix_time(),
6 ✔
183
        possible_narrow_sign_cast<uint32_t>(span.count()));
6 ✔
184
}
185

186
uint32_t protocol::created() const NOEXCEPT
2 ✔
187
{
188
    return to_unix(channel_->created());
2 ✔
189
}
190

191
uint32_t protocol::last_read() const NOEXCEPT
2 ✔
192
{
193
    return to_unix(channel_->last_read());
2 ✔
194
}
195

196
uint32_t protocol::last_write() const NOEXCEPT
2 ✔
197
{
198
    return to_unix(channel_->last_write());
2 ✔
199
}
200

201
const network::settings& protocol::network_settings() const NOEXCEPT
695 ✔
202
{
203
    return session_->network_settings();
695 ✔
204
}
205

206
uint64_t protocol::identifier() const NOEXCEPT
5 ✔
207
{
208
    return channel_->identifier();
5 ✔
209
}
210

211
// Send.
212
// ----------------------------------------------------------------------------
213
// Send (and receive) completion handlers invoked on channel strand.
214

215
// Send and receive failures logged by proxy, so no need to log here. Can be
216
// used as no-op handler for sends. Some protocols may create custom handlers to
217
// perform operations upon send completion.
218
void protocol::handle_send(const code&) NOEXCEPT
115 ✔
219
{
220
    BC_ASSERT(stranded());
115 ✔
221
}
115 ✔
222

223
} // namespace network
224
} // namespace libbitcoin
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc