Refactor WebsocketReader into a separate file
This commit is contained in:
parent
10fc605078
commit
2016f19c3d
4 changed files with 118 additions and 56 deletions
59
src/main.cpp
59
src/main.cpp
|
@ -15,8 +15,7 @@
|
||||||
* along with duckticker. If not, see <http://www.gnu.org/licenses/>.
|
* along with duckticker. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <boost/beast.hpp>
|
#include "websocket_reader.hpp"
|
||||||
#include <boost/asio.hpp>
|
|
||||||
#include <simdjson.h>
|
#include <simdjson.h>
|
||||||
#include <wrenpp/vm_fun.hpp>
|
#include <wrenpp/vm_fun.hpp>
|
||||||
#include <wrenpp/def_configuration.hpp>
|
#include <wrenpp/def_configuration.hpp>
|
||||||
|
@ -54,58 +53,6 @@ class App {
|
||||||
var the_app = App.new("USD", "kraken")
|
var the_app = App.new("USD", "kraken")
|
||||||
)script";
|
)script";
|
||||||
|
|
||||||
class WebsocketReader {
|
|
||||||
using tcp = boost::asio::ip::tcp;
|
|
||||||
public:
|
|
||||||
WebsocketReader (std::string host,
|
|
||||||
const std::string& port,
|
|
||||||
const std::string& path,
|
|
||||||
const std::string& user_agent,
|
|
||||||
const std::string& message
|
|
||||||
);
|
|
||||||
|
|
||||||
~WebsocketReader();
|
|
||||||
|
|
||||||
std::string read();
|
|
||||||
|
|
||||||
private:
|
|
||||||
boost::asio::io_context m_ioc;
|
|
||||||
tcp::resolver m_resolver;
|
|
||||||
boost::beast::websocket::stream<tcp::socket> m_ws;
|
|
||||||
};
|
|
||||||
|
|
||||||
WebsocketReader::WebsocketReader (std::string host,
|
|
||||||
const std::string& port,
|
|
||||||
const std::string& path,
|
|
||||||
const std::string& user_agent,
|
|
||||||
const std::string& message
|
|
||||||
) :
|
|
||||||
m_resolver{m_ioc},
|
|
||||||
m_ws{m_ioc}
|
|
||||||
{
|
|
||||||
//see https://www.boost.org/doc/libs/develop/libs/beast/example/websocket/client/sync/websocket_client_sync.cpp
|
|
||||||
const auto results = m_resolver.resolve(host, port);
|
|
||||||
auto ep = boost::asio::connect(m_ws.next_layer(), results);
|
|
||||||
host += ':' + std::to_string(ep.port());
|
|
||||||
m_ws.set_option(boost::beast::websocket::stream_base::decorator(
|
|
||||||
[user_agent](boost::beast::websocket::request_type& req)
|
|
||||||
{
|
|
||||||
req.set(boost::beast::http::field::user_agent, user_agent);
|
|
||||||
}
|
|
||||||
));
|
|
||||||
m_ws.handshake(host, path);
|
|
||||||
m_ws.write(boost::asio::buffer(message));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string WebsocketReader::read() {
|
|
||||||
boost::beast::flat_buffer buffer;
|
|
||||||
m_ws.read(buffer);
|
|
||||||
return boost::beast::buffers_to_string(buffer.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
WebsocketReader::~WebsocketReader() {
|
|
||||||
m_ws.close(boost::beast::websocket::close_code::normal);
|
|
||||||
}
|
|
||||||
|
|
||||||
class TickerPrice {
|
class TickerPrice {
|
||||||
public:
|
public:
|
||||||
|
@ -133,10 +80,10 @@ private:
|
||||||
TickerPrice ticker_price_bitoinity (std::string_view currency, std::string_view exchange) {
|
TickerPrice ticker_price_bitoinity (std::string_view currency, std::string_view exchange) {
|
||||||
static const TickerPrice error_price {0.0, "", ""};
|
static const TickerPrice error_price {0.0, "", ""};
|
||||||
|
|
||||||
WebsocketReader websocket{"bitcoinity.org",
|
duck::WebsocketReader websocket{"bitcoinity.org",
|
||||||
"80",
|
"80",
|
||||||
"/webs_bridge/websocket",
|
"/webs_bridge/websocket",
|
||||||
std::string(BOOST_BEAST_VERSION_STRING) + " websocket-client-coro",
|
std::string{duck::WebsocketReader::BeastVersionString} + " websocket-client-coro",
|
||||||
R"({"topic":"webs:markets_)" + std::string{exchange} + "_" +
|
R"({"topic":"webs:markets_)" + std::string{exchange} + "_" +
|
||||||
std::string{currency} + R"(","event":"phx_join","payload":{},"ref":"3"})"
|
std::string{currency} + R"(","event":"phx_join","payload":{},"ref":"3"})"
|
||||||
};
|
};
|
||||||
|
|
|
@ -23,6 +23,7 @@ executable(meson.project_name(),
|
||||||
'nap/http_header_parse.cpp',
|
'nap/http_header_parse.cpp',
|
||||||
'nap/page_fetch.cpp',
|
'nap/page_fetch.cpp',
|
||||||
'nap/quick_rest.cpp',
|
'nap/quick_rest.cpp',
|
||||||
|
'websocket_reader.cpp',
|
||||||
install: true,
|
install: true,
|
||||||
dependencies: [
|
dependencies: [
|
||||||
curlcpp_dep,
|
curlcpp_dep,
|
||||||
|
|
69
src/websocket_reader.cpp
Normal file
69
src/websocket_reader.cpp
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
/* Copyright 2022, Michele Santullo
|
||||||
|
* This file is part of duckticker.
|
||||||
|
*
|
||||||
|
* Wrenpp is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Wrenpp is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with duckticker. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "websocket_reader.hpp"
|
||||||
|
#include <boost/beast.hpp>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
|
||||||
|
namespace duck {
|
||||||
|
const std::string_view WebsocketReader::BeastVersionString = BOOST_BEAST_VERSION_STRING;
|
||||||
|
|
||||||
|
struct WebsocketReader::LocalData {
|
||||||
|
using tcp = boost::asio::ip::tcp;
|
||||||
|
|
||||||
|
LocalData() :
|
||||||
|
resolver{ioc},
|
||||||
|
ws{ioc}
|
||||||
|
{ }
|
||||||
|
|
||||||
|
boost::asio::io_context ioc;
|
||||||
|
tcp::resolver resolver;
|
||||||
|
boost::beast::websocket::stream<tcp::socket> ws;
|
||||||
|
};
|
||||||
|
|
||||||
|
WebsocketReader::WebsocketReader (std::string host,
|
||||||
|
const std::string& port,
|
||||||
|
const std::string& path,
|
||||||
|
const std::string& user_agent,
|
||||||
|
const std::string& message
|
||||||
|
) :
|
||||||
|
m_local(std::make_unique<LocalData>())
|
||||||
|
{
|
||||||
|
//see https://www.boost.org/doc/libs/develop/libs/beast/example/websocket/client/sync/websocket_client_sync.cpp
|
||||||
|
const auto results = m_local->resolver.resolve(host, port);
|
||||||
|
auto ep = boost::asio::connect(m_local->ws.next_layer(), results);
|
||||||
|
host += ':' + std::to_string(ep.port());
|
||||||
|
m_local->ws.set_option(boost::beast::websocket::stream_base::decorator(
|
||||||
|
[user_agent](boost::beast::websocket::request_type& req)
|
||||||
|
{
|
||||||
|
req.set(boost::beast::http::field::user_agent, user_agent);
|
||||||
|
}
|
||||||
|
));
|
||||||
|
m_local->ws.handshake(host, path);
|
||||||
|
m_local->ws.write(boost::asio::buffer(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string WebsocketReader::read() {
|
||||||
|
boost::beast::flat_buffer buffer;
|
||||||
|
m_local->ws.read(buffer);
|
||||||
|
return boost::beast::buffers_to_string(buffer.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
WebsocketReader::~WebsocketReader() {
|
||||||
|
m_local->ws.close(boost::beast::websocket::close_code::normal);
|
||||||
|
}
|
||||||
|
} //namespace duck
|
45
src/websocket_reader.hpp
Normal file
45
src/websocket_reader.hpp
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
/* Copyright 2022, Michele Santullo
|
||||||
|
* This file is part of duckticker.
|
||||||
|
*
|
||||||
|
* Wrenpp is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* Wrenpp is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with duckticker. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace duck {
|
||||||
|
class WebsocketReader {
|
||||||
|
public:
|
||||||
|
static const std::string_view BeastVersionString;
|
||||||
|
|
||||||
|
WebsocketReader (std::string host,
|
||||||
|
const std::string& port,
|
||||||
|
const std::string& path,
|
||||||
|
const std::string& user_agent,
|
||||||
|
const std::string& message
|
||||||
|
);
|
||||||
|
|
||||||
|
~WebsocketReader();
|
||||||
|
|
||||||
|
std::string read();
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct LocalData;
|
||||||
|
std::unique_ptr<LocalData> m_local;
|
||||||
|
};
|
||||||
|
|
||||||
|
} //namespace duck
|
Loading…
Reference in a new issue