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

libbitcoin / libbitcoin-network / 34507005611

10 Sep 2026 05:14PM UTC coverage: 48.448% (+32.3%) from 16.131%
34507005611

push

github

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

Update embedded wolfssl from 5.8.4 to 5.9.2.

2121 of 8395 new or added lines in 50 files covered. (25.27%)

528 existing lines in 31 files now uncovered.

27506 of 56774 relevant lines covered (48.45%)

166396.47 hits per line

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

70.64
/src/net/proxy.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/net/proxy.hpp>
20

21
#include <bitcoin/network/define.hpp>
22
#include <bitcoin/network/log/log.hpp>
23
#include <bitcoin/network/net/socket.hpp>
24

25
namespace libbitcoin {
26
namespace network {
27

28
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
29

30
// Factory for variable deadline timer pointer construction (or null).
31
inline deadline::ptr make_throttle(const logger& log, asio::strand& strand,
32
    uint32_t rate_limit) NOEXCEPT
33
{
34
    return to_bool(rate_limit) ?
58 ✔
35
        system::emplace_shared<deadline>(log, strand) : nullptr;
36
}
37

38
// This is created in a started state and must be stopped, as the subscribers
39
// assert if not stopped. Subscribers may hold protocols even if the service
40
// is not started.
41
proxy::proxy(const socket::ptr& socket, uint32_t rate_limit) NOEXCEPT
58 ✔
42
  : rate_limit_(rate_limit),
58 ✔
43
    socket_(socket),
44
    throttle_(make_throttle(socket->log, socket->strand(), rate_limit)),
58 ✔
45
    reporter(socket->log)
116 ✔
46
{
47
}
58 ✔
48

49
proxy::~proxy() NOEXCEPT
58 ✔
50
{
51
    BC_ASSERT_MSG(stopped(), "proxy is not stopped");
58 ✔
52
    if (!stopped()) { LOGF("~proxy is not stopped."); }
58 ✔
53
}
116 ✔
54

55
// Stop (socket/proxy started upon create).
56
// ----------------------------------------------------------------------------
57
// The proxy does not (must not) stop itself.
58

59
void proxy::stop(const code& ec) NOEXCEPT
74 ✔
60
{
61
    if (stopped())
74 ✔
62
        return;
63

64
    // Overruled by stop, set only for consistency.
65
    paused_.store(true);
66

67
    // An open batch response is closed (written) before the socket stops.
68
    // Batch state is protected by the strand, so only a stranded stop can
69
    // close gracefully (an unstranded stop, e.g. shutdown, is immediate).
70
    if (stranded() && batched_ && parted_)
59 ✔
71
    {
72
        do_stop(ec);
×
73
        return;
×
74
    }
75

76
    finish_stop(ec);
59 ✔
77
}
78

79
// private
80
// Close the open batch response, then stop (batch open rides on the first
81
// part, so there is nothing to close unless parted).
82
void proxy::do_stop(const code& ec) NOEXCEPT
×
83
{
84
    BC_ASSERT(stranded());
×
85
    using namespace std::placeholders;
86

87
    // The socket is not yet stopped, so a deferred send still holds the queue.
88
    // Release it here so that the close part is not delayed by the throttle.
89
    if (throttle_) throttle_->stop();
×
90

91
    batched_ = false;
×
92
    parted_ = false;
×
93

94
    rpc::response close{};
×
95
    close.batch = true;
×
96
    close.changed = true;
×
97

98
    const count_handler complete = std::bind(&proxy::handle_stop_write,
×
99
        shared_from_this(), _1, _2, ec);
×
100

101
    // The close part is written per framing (http chunk or stream).
102
    if (parser_)
×
103
    {
104
        socket_->rpc_write_chunk(std::move(close), move_copy(complete));
×
105
        return;
106
    }
107

108
    const auto out = system::move_shared(std::move(close));
109
    do_write(std::bind(&proxy::do_response_write,
×
110
        shared_from_this(), out, complete));
×
111
}
112

113
// private
114
void proxy::handle_stop_write(const code&, size_t,
×
115
    const code& reason) NOEXCEPT
116
{
117
    BC_ASSERT(stranded());
×
118
    finish_stop(reason);
×
119
}
×
120

121
// private
122
// Thread safe (called directly from stop or from the strand).
123
void proxy::finish_stop(const code& ec) NOEXCEPT
59 ✔
124
{
125
    // Client websocket or timer initated (when ws) stop is async (graceful).
126
    // error::service_stopped will invoke socket stop() below, which will
127
    // immediately terminate outstanding lazy_stop().
128
    if (ec == error::websocket_closed ||
59 ✔
129
        ec == error::channel_expired ||
59 ✔
130
        ec == error::channel_inactive)
59 ✔
131
    {
132
        // Stop the read loop, stop accepting new work, cancel pending work.
133
        // Allows for graceful ws/ssl::close, which would hang threadpool if
134
        // attempted within socket::stop(), as it issues a follow-on iocontext
135
        // job. A subsequent call to socket::stop() will terminate directly.
136
        socket_->lazy_stop();
×
137
    }
138
    else
139
    {
140
        // Stop the read loop, stop accepting new work, cancel pending work.
141
        socket_->stop();
59 ✔
142
    }
143

144
    boost::asio::post(strand(),
59 ✔
145
        std::bind(&proxy::stopping, shared_from_this(), ec));
118 ✔
146
}
59 ✔
147

148
// protected
149
// This should not be called internally (invoked by stop()).
150
void proxy::stopping(const code& ec) NOEXCEPT
59 ✔
151
{
152
    BC_ASSERT(stranded());
59 ✔
153

154
    // Release any deferred send (fires pending charge with canceled).
155
    if (throttle_) throttle_->stop();
59 ✔
156

157
    // Release any http message parse in progress.
158
    parser_.reset();
59 ✔
159

160
    // Post stop handlers to strand and clear/stop accepting subscriptions.
161
    // The code provides information on the reason that the channel stopped.
162
    stop_subscriber_.stop(ec);
59 ✔
163
}
59 ✔
164

165
// protected
166
void proxy::subscribe_stop(result_handler&& handler) NOEXCEPT
18 ✔
167
{
168
    BC_ASSERT(stranded());
18 ✔
169
    stop_subscriber_.subscribe(std::move(handler));
18 ✔
170
}
18 ✔
171

172
void proxy::subscribe_stop(result_handler&& handler,
13 ✔
173
    result_handler&& complete) NOEXCEPT
174
{
175
    if (stopped())
13 ✔
176
    {
UNCOV
177
        complete(error::channel_stopped);
×
UNCOV
178
        handler(error::channel_stopped);
×
UNCOV
179
        return;
×
180
    }
181

182
    boost::asio::dispatch(strand(),
13 ✔
183
        std::bind(&proxy::do_subscribe_stop,
26 ✔
184
            shared_from_this(), std::move(handler), std::move(complete)));
13 ✔
185
}
186

187
// private
188
void proxy::do_subscribe_stop(const result_handler& handler,
13 ✔
189
    const result_handler& complete) NOEXCEPT
190
{
191
    BC_ASSERT(stranded());
13 ✔
192
    stop_subscriber_.subscribe(move_copy(handler));
26 ✔
193
    complete(error::success);
13 ✔
194
}
13 ✔
195

196
// Pause (proxy is created paused).
197
// ----------------------------------------------------------------------------
198
// public
199

200
void proxy::pause() NOEXCEPT
15 ✔
201
{
202
    BC_ASSERT(stranded());
15 ✔
203
    paused_ = true;
204
}
15 ✔
205

206
void proxy::resume() NOEXCEPT
24 ✔
207
{
208
    BC_ASSERT(stranded());
24 ✔
209
    paused_ = false;
210
}
24 ✔
211

212
bool proxy::paused() const NOEXCEPT
54 ✔
213
{
214
    BC_ASSERT(stranded());
54 ✔
215
    return paused_;
54 ✔
216
}
217

218
// Signal activity.
219
// ----------------------------------------------------------------------------
220
// override reading() to update timers.
221

222
// protected
223
void proxy::reading() NOEXCEPT
7 ✔
224
{
225
}
7 ✔
226

227
// private
228
void proxy::do_reading() NOEXCEPT
22 ✔
229
{
230
    boost::asio::dispatch(strand(),
22 ✔
231
        std::bind(&proxy::reading, shared_from_this()));
44 ✔
232
}
22 ✔
233

234
// Properties.
235
// ----------------------------------------------------------------------------
236

237
asio::context& proxy::service() const NOEXCEPT
×
238
{
239
    return socket_->service();
×
240
}
241

242
asio::strand& proxy::strand() NOEXCEPT
195 ✔
243
{
244
    return socket_->strand();
195 ✔
245
}
246

247
bool proxy::stranded() const NOEXCEPT
1,056 ✔
248
{
249
    return socket_->stranded();
1,056 ✔
250
}
251

252
bool proxy::stopped() const NOEXCEPT
549 ✔
253
{
254
    return socket_->stopped();
549 ✔
255
}
256

257
bool proxy::inbound() const NOEXCEPT
54 ✔
258
{
259
    return socket_->inbound();
54 ✔
260
}
261

262
bool proxy::secure() const NOEXCEPT
2 ✔
263
{
264
    return socket_->secure();
2 ✔
265
}
266

267
bool proxy::encrypted() const NOEXCEPT
2 ✔
268
{
269
    return socket_->encrypted();
2 ✔
270
}
271

272
bool proxy::websocket() const NOEXCEPT
7 ✔
273
{
274
    return socket_->websocket();
7 ✔
275
}
276

277
bool proxy::downgraded() const NOEXCEPT
2 ✔
278
{
279
    return socket_->downgraded();
2 ✔
280
}
281

282
bool proxy::detected() const NOEXCEPT
×
283
{
284
    return socket_->detected();
×
285
}
286

287
code proxy::accept_websocket(const http::request& request) NOEXCEPT
×
288
{
289
    return socket_->accept_websocket(request);
×
290
}
291

292
uint64_t proxy::total() const NOEXCEPT
×
293
{
294
    return total_.load(std::memory_order_relaxed);
×
295
}
296

297
const config::address& proxy::address() const NOEXCEPT
5 ✔
298
{
299
    return socket_->address();
5 ✔
300
}
301

302
const config::endpoint& proxy::endpoint() const NOEXCEPT
67 ✔
303
{
304
    return socket_->endpoint();
67 ✔
305
}
306

307
BC_POP_WARNING()
308

309
} // namespace network
310
} // 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