websocket connection in a separate thread

This commit is contained in:
Pavel Baykov 2022-06-13 07:23:40 -03:00
parent 1a684df3f2
commit 4a63c202be
2 changed files with 21 additions and 14 deletions

View file

@ -17,6 +17,8 @@
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <fc/thread/thread.hpp>
namespace graphene { namespace peerplays_sidechain {
typedef websocketpp::client<websocketpp::config::asio_client> client;
@ -67,7 +69,7 @@ public:
eth_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls);
void start(std::string uri);
void start();
void stop();
void on_socket_init(websocketpp::connection_hdl);
void on_fail(websocketpp::connection_hdl hdl);
@ -133,6 +135,7 @@ private:
ethereum_function_call_encoder m_ethereum_function_call_encoder;
std::shared_ptr<fc::thread> _thread;
std::string signature;
std::string geth_url;
uint64_t t_id;

View file

@ -67,7 +67,7 @@ std::string eth_rpc_client::ethereum_function_call_encoder::encode_bytes(const s
}
eth_rpc_client::eth_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls) {
eth_rpc_client::eth_rpc_client(const std::string &url, const std::string &user_name, const std::string &password, bool debug_rpc_calls) : _thread(std::make_shared<fc::thread>("eth_rpc_client")){
geth_url = url;
user = user_name;
this->password = password;
@ -91,20 +91,24 @@ eth_rpc_client::eth_rpc_client(const std::string &url, const std::string &user_n
m_endpoint.set_fail_handler(bind(&type::on_fail,this,::_1));
}
void eth_rpc_client::start(std::string uri) {
websocketpp::lib::error_code ec;
client::connection_ptr con = m_endpoint.get_connection(uri, ec);
m_hdl = con->get_handle();
void eth_rpc_client::start() {
ilog("### eth_rpc_client::start uri: ${uri}", ("uri", geth_url));
auto future = _thread->async([this]
{
websocketpp::lib::error_code ec;
client::connection_ptr con = m_endpoint.get_connection(this->geth_url, ec);
m_hdl = con->get_handle();
if (ec) {
m_endpoint.get_alog().write(websocketpp::log::alevel::app,ec.message());
return;
}
if (ec) {
m_endpoint.get_alog().write(websocketpp::log::alevel::app,ec.message());
return;
}
m_endpoint.connect(con);
m_endpoint.connect(con);
// Start the ASIO io_service run loop
m_endpoint.run();
// Start the ASIO io_service run loop
m_endpoint.run();
});
}
void eth_rpc_client::stop() {
@ -455,7 +459,7 @@ sidechain_net_handler_eth::sidechain_net_handler_eth(peerplays_sidechain_plugin
url = options.at("ethereum-node-rpc-url").as<std::string>();
eth_client = std::unique_ptr<eth_rpc_client>(new eth_rpc_client(url, rpc_user, rpc_password, debug_rpc_calls));
eth_client->start(url);
eth_client->start();
/*
if (!wallet.empty()) {
eth_client->loadwallet(wallet);