Skip to content

Commit 78fc6d9

Browse files
jgriffithsjkauffman1
authored andcommitted
Revert "wamp_transport, session, and boost::asio::io_context"
Reverting in an attempt to fix the mingw compilation/login crash issues. This reverts commit 52f2b2e.
1 parent 8f0bd88 commit 78fc6d9

File tree

9 files changed

+38
-97
lines changed

9 files changed

+38
-97
lines changed

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ add_library(greenaddress-objects OBJECT
2424
ga_tx.cpp ga_tx.hpp
2525
ga_wally.cpp ga_wally.hpp
2626
http_client.cpp http_client.hpp
27-
io_runner.cpp io_runner.hpp
2827
json_utils.cpp json_utils.hpp
2928
network_parameters.cpp network_parameters.hpp
3029
session.cpp session.hpp

src/ga_session.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,11 @@ namespace sdk {
270270
, m_multi_call_category(0)
271271
, m_cache(std::make_shared<cache>(m_net_params, m_net_params.network()))
272272
, m_user_agent(std::string(GDK_COMMIT) + " " + m_net_params.user_agent())
273+
, m_wamp(new wamp_transport(m_net_params,
274+
std::bind(&ga_session::emit_notification, this, std::placeholders::_1, std::placeholders::_2)))
273275
, m_spv_thread_done(false)
274276
, m_spv_thread_stop(false)
275277
{
276-
m_wamp = std::make_unique<wamp_transport>(m_net_params, *session_impl::m_io, *session_impl::m_strand,
277-
std::bind(&ga_session::emit_notification, this, std::placeholders::_1, std::placeholders::_2));
278278
m_fee_estimates.assign(NUM_FEE_ESTIMATES, m_min_fee_rate);
279279
}
280280

@@ -287,12 +287,6 @@ namespace sdk {
287287
constexpr bool do_start = false;
288288
download_headers_ctl(locker, do_start);
289289
});
290-
no_std_exception_escape(
291-
[this] {
292-
m_wamp->disconnect();
293-
m_wamp.reset();
294-
},
295-
"ga_session wamp_transport");
296290
}
297291

298292
void ga_session::connect()

src/ga_session.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace ga {
1818
namespace sdk {
1919
struct cache;
2020
class ga_user_pubkeys;
21-
class io_runner;
2221
class wamp_transport;
2322

2423
class ga_session final : public session_impl {

src/io_runner.cpp

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/io_runner.hpp

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/session_impl.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "ga_tor.hpp"
99
#include "ga_tx.hpp"
1010
#include "http_client.hpp"
11-
#include "io_runner.hpp"
1211
#include "logging.hpp"
1312
#include "signer.hpp"
1413
#include "transaction_utils.hpp"
@@ -31,6 +30,17 @@ namespace sdk {
3130

3231
} // namespace
3332

33+
struct io_context_and_guard {
34+
io_context_and_guard()
35+
: m_io()
36+
, m_work_guard(boost::asio::make_work_guard(m_io))
37+
{
38+
}
39+
40+
boost::asio::io_context m_io;
41+
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> m_work_guard;
42+
};
43+
3444
std::shared_ptr<session_impl> session_impl::create(const nlohmann::json& net_params)
3545
{
3646
auto defaults = network_parameters::get(net_params.value("name", std::string()));
@@ -44,8 +54,7 @@ namespace sdk {
4454

4555
session_impl::session_impl(network_parameters&& net_params)
4656
: m_net_params(net_params)
47-
, m_io(std::make_unique<io_runner>())
48-
, m_strand(std::make_unique<boost::asio::io_context::strand>(m_io->get_io_context()))
57+
, m_io(std::make_unique<io_context_and_guard>())
4958
, m_user_proxy(socksify(m_net_params.get_json().value("proxy", std::string())))
5059
, m_notification_handler(nullptr)
5160
, m_notification_context(nullptr)
@@ -55,12 +64,13 @@ namespace sdk {
5564
// Enable internal tor controller
5665
m_tor_ctrl = tor_controller::get_shared_ref();
5766
}
67+
m_run_thread = std::thread([this] { m_io->m_io.run(); });
5868
}
5969

6070
session_impl::~session_impl()
6171
{
62-
no_std_exception_escape([this] { m_io.reset(); }, "session_impl m_io");
63-
no_std_exception_escape([this] { m_strand.reset(); }, "session_impl m_strand");
72+
no_std_exception_escape([this] { m_io->m_work_guard.reset(); }, "session_impl dtor(1)");
73+
no_std_exception_escape([this] { m_run_thread.join(); }, "session_impl dtor(2)");
6474
}
6575

6676
void session_impl::set_notification_handler(GA_notification_handler handler, void* context)
@@ -123,7 +133,7 @@ namespace sdk {
123133

124134
std::shared_ptr<http_client> client;
125135
auto&& get = [&] {
126-
client = make_http_client(m_io->get_io_context(), ssl_ctx.get());
136+
client = make_http_client(m_io->m_io, ssl_ctx.get());
127137
GDK_RUNTIME_ASSERT(client != nullptr);
128138

129139
const auto verb = boost::beast::http::string_to_verb(params["method"]);

src/session_impl.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#ifndef GDK_SESSION_IMPL_HPP
22
#define GDK_SESSION_IMPL_HPP
3-
#pragma once
43

4+
#pragma once
55
#include <atomic>
6-
#include <boost/asio/io_context.hpp>
76
#include <mutex>
87
#include <set>
98
#include <thread>
@@ -18,11 +17,11 @@ namespace sdk {
1817
using unique_pubkeys_and_scripts_t = std::set<pubkey_and_script_t>;
1918

2019
class ga_pubkeys;
21-
class io_runner;
2220
class user_pubkeys;
2321
class signer;
2422
class Tx;
2523
struct tor_controller;
24+
struct io_context_and_guard;
2625

2726
class session_impl {
2827
public:
@@ -259,9 +258,9 @@ namespace sdk {
259258

260259
// Immutable upon construction
261260
const network_parameters m_net_params;
262-
std::unique_ptr<io_runner> m_io;
263-
std::unique_ptr<boost::asio::io_context::strand> m_strand;
261+
std::unique_ptr<io_context_and_guard> m_io;
264262

263+
std::thread m_run_thread; // Runs the asio context
265264
const std::string m_user_proxy;
266265
std::shared_ptr<tor_controller> m_tor_ctrl;
267266

src/wamp_transport.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <array>
2-
#include <boost/asio/io_context.hpp>
32
#include <cstdio>
43
#include <fstream>
54
#include <map>
@@ -18,7 +17,6 @@
1817
#include "boost_wrapper.hpp"
1918
#include "exception.hpp"
2019
#include "http_client.hpp"
21-
#include "io_runner.hpp"
2220
#include "logging.hpp"
2321
#include "network_parameters.hpp"
2422
#include "utils.hpp"
@@ -313,11 +311,11 @@ namespace sdk {
313311

314312
nlohmann::json wamp_cast_json(const autobahn::wamp_call_result& result) { return wamp_cast_json_impl(result); }
315313

316-
wamp_transport::wamp_transport(const network_parameters& net_params, io_runner& runner,
317-
boost::asio::io_context::strand& strand, wamp_transport::notify_fn_t fn, std::string server_prefix)
314+
wamp_transport::wamp_transport(
315+
const network_parameters& net_params, wamp_transport::notify_fn_t fn, std::string server_prefix)
318316
: m_net_params(net_params)
319-
, m_io(runner)
320-
, m_strand(strand)
317+
, m_io()
318+
, m_work_guard(asio::make_work_guard(m_io))
321319
, m_server_prefix(std::move(server_prefix))
322320
, m_server(m_net_params.get_connection_string(m_server_prefix))
323321
, m_wamp_host_name(websocketpp::uri(m_net_params.gait_wamp_url(m_server_prefix)).get_host())
@@ -333,12 +331,13 @@ namespace sdk {
333331

334332
m_wamp_call_options.set_timeout(std::chrono::seconds(WAMP_CALL_TIMEOUT_SECS));
335333

334+
m_run_thread = std::thread([this] { m_io.run(); });
336335
m_reconnect_thread = std::thread([this] { reconnect_handler(); });
337336

338337
if (!m_net_params.is_tls_connection(m_server_prefix)) {
339338
m_client = std::make_unique<client>();
340339
m_client->set_pong_timeout_handler(std::bind(&wamp_transport::heartbeat_timeout_cb, this, _1, _2));
341-
m_client->init_asio(&m_io.get_io_context());
340+
m_client->init_asio(&m_io);
342341
return;
343342
}
344343

@@ -348,13 +347,15 @@ namespace sdk {
348347
return tls_init(m_wamp_host_name, m_net_params.gait_wamp_cert_roots(), m_net_params.gait_wamp_cert_pins(),
349348
m_net_params.cert_expiry_threshold());
350349
});
351-
m_client_tls->init_asio(&m_io.get_io_context());
350+
m_client_tls->init_asio(&m_io);
352351
}
353352

354353
wamp_transport::~wamp_transport()
355354
{
356355
no_std_exception_escape([this] { change_state_to(state_t::exited, std::string(), false); }, "wamp dtor(1)");
357356
no_std_exception_escape([this] { m_reconnect_thread.join(); }, "wamp dtor(2)");
357+
no_std_exception_escape([this] { m_work_guard.reset(); }, "wamp dtor(2)");
358+
no_std_exception_escape([this] { m_run_thread.join(); }, "wamp dtor(3)");
358359
}
359360

360361
void wamp_transport::connect(const std::string& proxy) { change_state_to(state_t::connected, proxy, true); }
@@ -492,7 +493,7 @@ namespace sdk {
492493
{
493494
const bool is_tls = m_net_params.is_tls_connection(m_server_prefix);
494495
const bool is_debug = gdk_config()["log_level"] == "debug";
495-
const auto& executor = m_io.get_io_context().get_executor();
496+
const auto& executor = m_io.get_executor();
496497

497498
// The last failure number that we handled
498499
auto last_handled_failure_count = m_failure_count.load();
@@ -580,7 +581,7 @@ namespace sdk {
580581
} else {
581582
t = std::make_shared<transport>(*m_client, m_server, proxy, is_debug);
582583
}
583-
s = std::make_shared<autobahn::wamp_session>(m_io.get_io_context(), is_debug);
584+
s = std::make_shared<autobahn::wamp_session>(m_io, is_debug);
584585
t->attach(std::static_pointer_cast<autobahn::wamp_transport_handler>(s));
585586
bool failed = false;
586587
if (no_std_exception_escape(

src/wamp_transport.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define GDK_WAMP_TRANSPORT_HPP
33
#pragma once
44

5-
#include <boost/asio/io_context.hpp>
65
#include <nlohmann/json_fwd.hpp>
76
#include <optional>
87
#include <string>
@@ -17,7 +16,6 @@ namespace sdk {
1716
class exponential_backoff;
1817
struct websocketpp_gdk_config;
1918
struct websocketpp_gdk_tls_config;
20-
class io_runner;
2119

2220
nlohmann::json wamp_cast_json(const autobahn::wamp_event& event);
2321
nlohmann::json wamp_cast_json(const autobahn::wamp_call_result& result);
@@ -46,8 +44,7 @@ namespace sdk {
4644
using notify_fn_t = std::function<void(nlohmann::json, bool)>;
4745
using subscribe_fn_t = std::function<void(nlohmann::json)>;
4846

49-
wamp_transport(const network_parameters& net_params, io_runner& runner, boost::asio::io_context::strand& strand,
50-
notify_fn_t fn, std::string server_prefix = {});
47+
wamp_transport(const network_parameters& net_params, notify_fn_t fn, std::string server_prefix = {});
5148
~wamp_transport();
5249

5350
// Connect the transport. The proxy to use is passed to us as it can
@@ -93,7 +90,7 @@ namespace sdk {
9390
}
9491

9592
// Post a function to run on the asio executor thread
96-
template <typename FN> void post(FN&& fn) { boost::asio::post(m_strand, fn); }
93+
template <typename FN> void post(FN&& fn) { boost::asio::post(m_io.get_executor(), fn); }
9794

9895
private:
9996
using session_ptr = std::shared_ptr<autobahn::wamp_session>;
@@ -125,8 +122,9 @@ namespace sdk {
125122

126123
// These members are immutable after construction
127124
const network_parameters& m_net_params;
128-
io_runner& m_io;
129-
boost::asio::io_context::strand& m_strand;
125+
boost::asio::io_context m_io;
126+
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> m_work_guard;
127+
std::thread m_run_thread; // Runs the asio context
130128
std::thread m_reconnect_thread; // Runs the reconnection logic
131129
const std::string m_server_prefix;
132130
const std::string m_server;

0 commit comments

Comments
 (0)