Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
310 lines (279 loc) · 8.79 KB

File metadata and controls

310 lines (279 loc) · 8.79 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2016 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <test/jtx.h>
#include <test/jtx/WSClient.h>
#include <xrpl/json/json_reader.h>
#include <xrpl/json/to_string.h>
#include <xrpl/protocol/jss.h>
#include <xrpl/server/Port.h>
#include <boost/beast/core/multi_buffer.hpp>
#include <boost/beast/websocket.hpp>
#include <condition_variable>
#include <string>
#include <unordered_map>
#include <iostream>
#include <xrpl/beast/unit_test.h>
namespace ripple {
namespace test {
class WSClientImpl : public WSClient
{
using error_code = boost::system::error_code;
struct msg
{
Json::Value jv;
explicit msg(Json::Value&& jv_) : jv(jv_)
{
}
};
static boost::asio::ip::tcp::endpoint
getEndpoint(BasicConfig const& cfg, bool v2)
{
auto& log = std::cerr;
ParsedPort common;
parse_Port(common, cfg["server"], log);
auto const ps = v2 ? "ws2" : "ws";
for (auto const& name : cfg.section("server").values())
{
if (!cfg.exists(name))
continue;
ParsedPort pp;
parse_Port(pp, cfg[name], log);
if (pp.protocol.count(ps) == 0)
continue;
using namespace boost::asio::ip;
if (pp.ip && pp.ip->is_unspecified())
*pp.ip = pp.ip->is_v6() ? address{address_v6::loopback()}
: address{address_v4::loopback()};
return {*pp.ip, *pp.port};
}
Throw<std::runtime_error>("Missing WebSocket port");
return {}; // Silence compiler control paths return value warning
}
template <class ConstBuffers>
static std::string
buffer_string(ConstBuffers const& b)
{
using boost::asio::buffer;
using boost::asio::buffer_size;
std::string s;
s.resize(buffer_size(b));
buffer_copy(buffer(&s[0], s.size()), b);
return s;
}
boost::asio::io_service ios_;
std::optional<boost::asio::io_service::work> work_;
boost::asio::io_service::strand strand_;
std::thread thread_;
boost::asio::ip::tcp::socket stream_;
boost::beast::websocket::stream<boost::asio::ip::tcp::socket&> ws_;
boost::beast::multi_buffer rb_;
bool peerClosed_ = false;
// synchronize destructor
bool b0_ = false;
std::mutex m0_;
std::condition_variable cv0_;
// synchronize message queue
std::mutex m_;
std::condition_variable cv_;
std::list<std::shared_ptr<msg>> msgs_;
unsigned rpc_version_;
void
cleanup()
{
ios_.post(strand_.wrap([this] {
if (!peerClosed_)
{
ws_.async_close({}, strand_.wrap([&](error_code ec) {
stream_.cancel(ec);
}));
}
}));
work_ = std::nullopt;
thread_.join();
}
public:
WSClientImpl(
Config const& cfg,
bool v2,
unsigned rpc_version,
std::unordered_map<std::string, std::string> const& headers = {})
: work_(ios_)
, strand_(ios_)
, thread_([&] { ios_.run(); })
, stream_(ios_)
, ws_(stream_)
, rpc_version_(rpc_version)
{
try
{
auto const ep = getEndpoint(cfg, v2);
stream_.connect(ep);
ws_.set_option(boost::beast::websocket::stream_base::decorator(
[&](boost::beast::websocket::request_type& req) {
for (auto const& h : headers)
req.set(h.first, h.second);
}));
ws_.handshake(
ep.address().to_string() + ":" + std::to_string(ep.port()),
"/");
ws_.async_read(
rb_,
strand_.wrap(std::bind(
&WSClientImpl::on_read_msg, this, std::placeholders::_1)));
}
catch (std::exception&)
{
cleanup();
Rethrow();
}
}
~WSClientImpl() override
{
cleanup();
}
Json::Value
invoke(std::string const& cmd, Json::Value const& params) override
{
using boost::asio::buffer;
using namespace std::chrono_literals;
{
Json::Value jp;
if (params)
jp = params;
if (rpc_version_ == 2)
{
jp[jss::method] = cmd;
jp[jss::jsonrpc] = "2.0";
jp[jss::ripplerpc] = "2.0";
jp[jss::id] = 5;
}
else
jp[jss::command] = cmd;
auto const s = to_string(jp);
ws_.write_some(true, buffer(s));
}
auto jv = findMsg(5s, [&](Json::Value const& jval) {
return jval[jss::type] == jss::response;
});
if (jv)
{
// Normalize JSON output
jv->removeMember(jss::type);
if ((*jv).isMember(jss::status) && (*jv)[jss::status] == jss::error)
{
Json::Value ret;
ret[jss::result] = *jv;
if ((*jv).isMember(jss::error))
ret[jss::error] = (*jv)[jss::error];
ret[jss::status] = jss::error;
return ret;
}
if ((*jv).isMember(jss::status) && (*jv).isMember(jss::result))
(*jv)[jss::result][jss::status] = (*jv)[jss::status];
return *jv;
}
return {};
}
std::optional<Json::Value>
getMsg(std::chrono::milliseconds const& timeout) override
{
std::shared_ptr<msg> m;
{
std::unique_lock<std::mutex> lock(m_);
if (!cv_.wait_for(lock, timeout, [&] { return !msgs_.empty(); }))
return std::nullopt;
m = std::move(msgs_.back());
msgs_.pop_back();
}
return std::move(m->jv);
}
std::optional<Json::Value>
findMsg(
std::chrono::milliseconds const& timeout,
std::function<bool(Json::Value const&)> pred) override
{
std::shared_ptr<msg> m;
{
std::unique_lock<std::mutex> lock(m_);
if (!cv_.wait_for(lock, timeout, [&] {
for (auto it = msgs_.begin(); it != msgs_.end(); ++it)
{
if (pred((*it)->jv))
{
m = std::move(*it);
msgs_.erase(it);
return true;
}
}
return false;
}))
{
return std::nullopt;
}
}
return std::move(m->jv);
}
unsigned
version() const override
{
return rpc_version_;
}
private:
void
on_read_msg(error_code const& ec)
{
if (ec)
{
if (ec == boost::beast::websocket::error::closed)
peerClosed_ = true;
return;
}
Json::Value jv;
Json::Reader jr;
jr.parse(buffer_string(rb_.data()), jv);
rb_.consume(rb_.size());
auto m = std::make_shared<msg>(std::move(jv));
{
std::lock_guard lock(m_);
msgs_.push_front(m);
cv_.notify_all();
}
ws_.async_read(
rb_,
strand_.wrap(std::bind(
&WSClientImpl::on_read_msg, this, std::placeholders::_1)));
}
// Called when the read op terminates
void
on_read_done()
{
std::lock_guard lock(m0_);
b0_ = true;
cv0_.notify_all();
}
};
std::unique_ptr<WSClient>
makeWSClient(
Config const& cfg,
bool v2,
unsigned rpc_version,
std::unordered_map<std::string, std::string> const& headers)
{
return std::make_unique<WSClientImpl>(cfg, v2, rpc_version, headers);
}
} // namespace test
} // namespace ripple
Morty Proxy This is a proxified and sanitized view of the page, visit original site.