20 #include <ripple/json/json_reader.h>
21 #include <ripple/json/to_string.h>
22 #include <ripple/protocol/jss.h>
23 #include <ripple/server/Port.h>
24 #include <boost/beast/core/multi_buffer.hpp>
25 #include <boost/beast/websocket.hpp>
27 #include <test/jtx/WSClient.h>
35 #include <ripple/beast/unit_test.h>
53 static boost::asio::ip::tcp::endpoint
59 auto const ps = v2 ?
"ws2" :
"ws";
68 using namespace boost::asio::ip;
69 if (pp.
ip && pp.
ip->is_unspecified())
70 *pp.
ip = pp.
ip->is_v6() ? address{address_v6::loopback()}
71 : address{address_v4::loopback()};
74 Throw<std::runtime_error>(
"Missing WebSocket port");
78 template <
class ConstBuffers>
82 using boost::asio::buffer;
83 using boost::asio::buffer_size;
86 buffer_copy(buffer(&s[0], s.
size()), b);
90 boost::asio::io_service
ios_;
91 boost::optional<boost::asio::io_service::work>
work_;
95 boost::beast::websocket::stream<boost::asio::ip::tcp::socket&>
ws_;
96 boost::beast::multi_buffer
rb_;
118 ws_.async_close({}, strand_.wrap([&](error_code ec) {
131 unsigned rpc_version,
147 [&](boost::beast::websocket::request_type& req) {
148 for (auto const& h : headers)
149 req.set(h.first, h.second);
171 using boost::asio::buffer;
172 using namespace std::chrono_literals;
180 jp[jss::method] = cmd;
181 jp[jss::jsonrpc] =
"2.0";
182 jp[jss::ripplerpc] =
"2.0";
186 jp[jss::command] = cmd;
188 ws_.write_some(
true, buffer(s));
192 return jval[jss::type] == jss::response;
197 jv->removeMember(jss::type);
198 if ((*jv).isMember(jss::status) && (*jv)[jss::status] == jss::error)
201 ret[jss::result] = *jv;
202 if ((*jv).isMember(jss::error))
203 ret[jss::error] = (*jv)[jss::error];
204 ret[jss::status] = jss::error;
207 if ((*jv).isMember(jss::status) && (*jv).isMember(jss::result))
208 (*jv)[jss::result][jss::status] = (*jv)[jss::status];
214 boost::optional<Json::Value>
220 if (!
cv_.
wait_for(lock, timeout, [&] { return !msgs_.empty(); }))
222 m = std::move(
msgs_.back());
225 return std::move(m->
jv);
228 boost::optional<Json::Value>
237 for (auto it = msgs_.begin(); it != msgs_.end(); ++it)
252 return std::move(m->jv);
267 if (ec == boost::beast::websocket::error::closed)
276 auto m = std::make_shared<msg>(std::move(jv));
302 unsigned rpc_version,
305 return std::make_unique<WSClientImpl>(cfg, v2, rpc_version, headers);