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>
25#include <xrpl/basics/random.h>
26#include <xrpl/protocol/BuildInfo.h>
28#include <boost/asio.hpp>
29#include <boost/beast/core/multi_buffer.hpp>
30#include <boost/beast/http/empty_body.hpp>
31#include <boost/beast/http/read.hpp>
32#include <boost/beast/http/write.hpp>
54 boost::beast::http::request<boost::beast::http::empty_body>;
60 boost::asio::io_service&
ios_;
75 boost::asio::io_service& ios,
84 return *
static_cast<Impl*
>(
this);
120 boost::asio::io_service& ios,
132 , lastEndpoint_{lastEndpoint}
133 , lastStatus_(lastStatus)
151 if (!strand_.running_in_this_thread())
155 resolver_.async_resolve(
160 impl().shared_from_this(),
161 std::placeholders::_1,
162 std::placeholders::_2)));
169 if (!strand_.running_in_this_thread())
171 return ios_.post(strand_.wrap(
186 cb_(ec, lastEndpoint_, std::move(res_));
206 results.begin(), results.end(), [&](
endpoint_type const& e) {
207 if (e == lastEndpoint_)
212 if (foundIt != results.end() && lastStatus_)
213 return lastEndpoint_;
214 else if (results.size() == 1)
215 return *results.begin();
216 else if (foundIt == results.end())
226 (results.size() > 2) ?
rand_int(results.size() - 2) : 0;
227 if (randIndex == foundIndex)
228 randIndex = results.size() - 1;
229 return *
std::next(results.begin(), randIndex);
232 socket_.async_connect(
236 impl().shared_from_this(),
237 std::placeholders::_1)));
244 req_.method(boost::beast::http::verb::get);
245 req_.target(path_.empty() ?
"/" : path_);
247 req_.set(
"Host", host_ +
":" + port_);
249 req_.prepare_payload();
250 boost::beast::http::async_write(
255 impl().shared_from_this(),
256 std::placeholders::_1)));
266 boost::beast::http::async_read(
272 impl().shared_from_this(),
273 std::placeholders::_1)));
284 XRPL_ASSERT(cb_,
"ripple::detail::WorkBase::onResponse : callback is set");
285 cb_(ec, lastEndpoint_, std::move(res_));
293 if (socket_.is_open())
296 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)