20#ifndef RIPPLE_APP_MISC_DETAIL_WORKBASE_H_INCLUDED
21#define RIPPLE_APP_MISC_DETAIL_WORKBASE_H_INCLUDED
23#include <xrpld/app/misc/detail/Work.h>
24#include <xrpl/basics/random.h>
25#include <xrpl/protocol/BuildInfo.h>
27#include <boost/asio.hpp>
28#include <boost/beast/core/multi_buffer.hpp>
29#include <boost/beast/http/empty_body.hpp>
30#include <boost/beast/http/read.hpp>
31#include <boost/beast/http/write.hpp>
55 boost::beast::http::request<boost::beast::http::empty_body>;
61 boost::asio::io_service&
ios_;
76 boost::asio::io_service& ios,
85 return *
static_cast<Impl*
>(
this);
121 boost::asio::io_service& ios,
133 , lastEndpoint_{lastEndpoint}
134 , lastStatus_(lastStatus)
152 if (!strand_.running_in_this_thread())
156 resolver_.async_resolve(
161 impl().shared_from_this(),
162 std::placeholders::_1,
163 std::placeholders::_2)));
170 if (!strand_.running_in_this_thread())
172 return ios_.post(strand_.wrap(
187 cb_(ec, lastEndpoint_, std::move(res_));
207 results.begin(), results.end(), [&](
endpoint_type const& e) {
208 if (e == lastEndpoint_)
213 if (foundIt != results.end() && lastStatus_)
214 return lastEndpoint_;
215 else if (results.size() == 1)
216 return *results.begin();
217 else if (foundIt == results.end())
227 (results.size() > 2) ?
rand_int(results.size() - 2) : 0;
228 if (randIndex == foundIndex)
229 randIndex = results.size() - 1;
230 return *
std::next(results.begin(), randIndex);
233 socket_.async_connect(
237 impl().shared_from_this(),
238 std::placeholders::_1)));
245 req_.method(boost::beast::http::verb::get);
246 req_.target(path_.empty() ?
"/" : path_);
248 req_.set(
"Host", host_ +
":" + port_);
250 req_.prepare_payload();
251 boost::beast::http::async_write(
256 impl().shared_from_this(),
257 std::placeholders::_1)));
267 boost::beast::http::async_read(
273 impl().shared_from_this(),
274 std::placeholders::_1)));
285 XRPL_ASSERT(cb_,
"ripple::detail::WorkBase::onResponse : callback is set");
286 cb_(ec, lastEndpoint_, std::move(res_));
294 if (socket_.is_open())
297 socket_.shutdown(boost::asio::socket_base::shutdown_send, ec);
boost::asio::io_service & ios_
endpoint_type lastEndpoint_
boost::asio::ip::tcp::resolver::results_type results_type
boost::beast::http::request< boost::beast::http::empty_body > request_type
void onResolve(error_code const &ec, results_type results)
void onRequest(error_code const &ec)
boost::asio::ip::tcp::socket socket_type
boost::asio::io_service::strand strand_
void onResponse(error_code const &ec)
boost::asio::ip::tcp::endpoint endpoint_type
boost::asio::ip::tcp::resolver resolver_type
boost::system::error_code error_code
boost::beast::multi_buffer readBuf_
WorkBase(std::string const &host, std::string const &path, std::string const &port, boost::asio::io_service &ios, endpoint_type const &lastEndpoint, bool lastStatus, callback_type cb)
void fail(error_code const &ec)
std::string const & getFullVersionString()
Full server version string.
boost::beast::http::response< boost::beast::http::string_body > response_type
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::enable_if_t< std::is_integral< Integral >::value, Integral > rand_int()
std::error_code make_error_code(ripple::TokenCodecErrc e)