generated from Warchant/cmake-hunter-seed
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathwsc.hpp
92 lines (78 loc) · 2.59 KB
/
wsc.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <boost/asio/executor_work_guard.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <future>
#include <libp2p/multi/multiaddress.hpp>
#include <mutex>
#include <queue>
#include <thread>
#include "api/rpc/json.hpp"
#include "api/rpc/rpc.hpp"
#include "api/visit.hpp"
#include "common/io_thread.hpp"
#include "common/logger.hpp"
namespace fc::api::rpc {
using boost::asio::io_context;
using libp2p::multi::Multiaddress;
using Logger = common::Logger;
struct Client {
using ResultCb = std::function<void(outcome::result<Document>)>;
using ChanCb = std::function<bool(boost::optional<Document>)>;
explicit Client(io_context &io2);
Client(const Client &) = delete;
Client(Client &&) = delete;
~Client();
Client &operator=(const Client &) = delete;
Client &operator=(Client &&) = delete;
outcome::result<void> connect(const Multiaddress &address,
const std::string &target,
const std::string &token);
outcome::result<void> connect(const std::string &host,
const std::string &port,
const std::string &target,
const std::string &token);
void call(Request &&req, ResultCb &&cb);
void _chan(uint64_t id, ChanCb &&cb);
void _error(const std::error_code &error);
void _flush();
void _read();
void _onread(const Document &j);
template <typename A>
void setup(A &api) {
visit(api, [&](auto &m) { _setup(*this, m); });
}
struct ClientData {
std::string host;
std::string port;
std::string target;
std::string token;
} client_data;
void reconnect(int counter, std::chrono::milliseconds wait);
private:
std::thread thread;
IoThread thread_chan;
io_context io;
io_context &io2;
boost::asio::executor_work_guard<io_context::executor_type> work_guard;
boost::optional<
boost::beast::websocket::stream<boost::asio::ip::tcp::socket>>
socket;
boost::beast::flat_buffer buffer;
std::mutex mutex;
uint64_t next_req{};
std::map<uint64_t, ResultCb> result_queue;
std::map<uint64_t, ChanCb> chans;
std::queue<std::pair<uint64_t, Bytes>> write_queue;
bool writing{false};
bool reconnecting{false};
template <typename M>
void _setup(Client &c, M &m);
Logger logger_;
};
} // namespace fc::api::rpc