3#include <xrpl/basics/Log.h>
4#include <xrpl/beast/net/IPAddressConversion.h>
5#include <xrpl/beast/net/IPEndpoint.h>
7#include <boost/asio.hpp>
8#include <boost/asio/ip/tcp.hpp>
9#include <boost/asio/ssl.hpp>
10#include <boost/beast/core/bind_handler.hpp>
27 using ssl_socket = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
38 boost::asio::io_context& s,
39 boost::asio::ssl::context& c,
43 ,
buffer_((plainOnly || secureOnly) ? 0 : 4)
44 ,
j_{
beast::Journal::getNullSink()}
49 AutoSocket(boost::asio::io_context& s, boost::asio::ssl::context& c)
96 boost::system::error_code
97 cancel(boost::system::error_code& ec)
105 if ((type == ssl_socket::client) || (
secure_))
109 socket_->async_handshake(type, cbFunc);
120 socket_->next_layer().async_receive(
122 boost::asio::socket_base::message_peek,
123 [
this, cbFunc](
error_code const& ec,
size_t bytesTransferred) {
129 template <
typename ShutdownHandler>
135 socket_->async_shutdown(handler);
142 lowestLayer().shutdown(plain_socket::shutdown_both);
144 catch (boost::system::system_error
const& e)
148 post(
socket_->get_executor(), boost::beast::bind_handler(handler, ec));
152 template <
typename Seq,
typename Handler>
158 socket_->async_read_some(buffers, handler);
166 template <
typename Seq,
typename Condition,
typename Handler>
172 boost::asio::async_read_until(*
socket_, buffers, condition, handler);
176 boost::asio::async_read_until(
plainSocket(), buffers, condition, handler);
180 template <
typename Allocator,
typename Handler>
183 boost::asio::basic_streambuf<Allocator>& buffers,
189 boost::asio::async_read_until(*
socket_, buffers, delim, handler);
193 boost::asio::async_read_until(
plainSocket(), buffers, delim, handler);
197 template <
typename Allocator,
typename MatchCondition,
typename Handler>
200 boost::asio::basic_streambuf<Allocator>& buffers,
206 boost::asio::async_read_until(*
socket_, buffers, cond, handler);
210 boost::asio::async_read_until(
plainSocket(), buffers, cond, handler);
214 template <
typename Buf,
typename Handler>
220 boost::asio::async_write(*
socket_, buffers, handler);
224 boost::asio::async_write(
plainSocket(), buffers, handler);
228 template <
typename Allocator,
typename Handler>
230 asyncWrite(boost::asio::basic_streambuf<Allocator>& buffers, Handler handler)
234 boost::asio::async_write(*
socket_, buffers, handler);
238 boost::asio::async_write(
plainSocket(), buffers, handler);
242 template <
typename Buf,
typename Condition,
typename Handler>
244 asyncRead(Buf
const& buffers, Condition cond, Handler handler)
248 boost::asio::async_read(*
socket_, buffers, cond, handler);
252 boost::asio::async_read(
plainSocket(), buffers, cond, handler);
256 template <
typename Allocator,
typename Condition,
typename Handler>
258 asyncRead(boost::asio::basic_streambuf<Allocator>& buffers, Condition cond, Handler handler)
262 boost::asio::async_read(*
socket_, buffers, cond, handler);
266 boost::asio::async_read(
plainSocket(), buffers, cond, handler);
270 template <
typename Buf,
typename Handler>
276 boost::asio::async_read(*
socket_, buffers, handler);
280 boost::asio::async_read(
plainSocket(), buffers, handler);
284 template <
typename Seq,
typename Handler>
290 socket_->async_write_some(buffers, handler);
302 using namespace xrpl;
306 JLOG(
j_.warn()) <<
"Handle autodetect error: " << ec;
311 ((bytesTransferred < 2) || ((
buffer_[1] < 127) && (
buffer_[1] > 31))) &&
312 ((bytesTransferred < 3) || ((
buffer_[2] < 127) && (
buffer_[2] > 31))) &&
313 ((bytesTransferred < 4) || ((
buffer_[3] < 127) && (
buffer_[3] > 31))))
316 JLOG(
j_.trace()) <<
"non-SSL";
323 JLOG(
j_.trace()) <<
"SSL";
325 socket_->async_handshake(ssl_socket::server, cbFunc);
std::function< void(error_code)> callback
AutoSocket(boost::asio::io_context &s, boost::asio::ssl::context &c, bool secureOnly, bool plainOnly)
ssl_socket::handshake_type handshake_type
ssl_socket::lowest_layer_type lowest_layer_type
ssl_socket::next_layer_type plain_socket
boost::asio::ip::tcp::socket::endpoint_type endpoint_type
lowest_layer_type & lowestLayer()
plain_socket & plainSocket()
beast::ip::Endpoint localEndpoint()
boost::system::error_code error_code
std::vector< char > buffer_
void asyncRead(Buf const &buffers, Condition cond, Handler handler)
AutoSocket(boost::asio::io_context &s, boost::asio::ssl::context &c)
std::unique_ptr< ssl_socket > socket_ptr
void asyncReadUntil(Seq const &buffers, Condition condition, Handler handler)
void asyncReadUntil(boost::asio::basic_streambuf< Allocator > &buffers, std::string const &delim, Handler handler)
void asyncShutdown(ShutdownHandler handler)
void swap(AutoSocket &s) noexcept
void handleAutodetect(callback cbFunc, error_code const &ec, size_t bytesTransferred)
beast::ip::Endpoint remoteEndpoint()
void asyncRead(Buf const &buffers, Handler handler)
void asyncWriteSome(Seq const &buffers, Handler handler)
void asyncRead(boost::asio::basic_streambuf< Allocator > &buffers, Condition cond, Handler handler)
void asyncReadSome(Seq const &buffers, Handler handler)
boost::system::error_code cancel(boost::system::error_code &ec)
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > ssl_socket
void asyncWrite(Buf const &buffers, Handler handler)
void asyncHandshake(handshake_type type, callback cbFunc)
void asyncReadUntil(boost::asio::basic_streambuf< Allocator > &buffers, MatchCondition cond, Handler handler)
void asyncWrite(boost::asio::basic_streambuf< Allocator > &buffers, Handler handler)
A generic endpoint for log messages.
A version-independent IP address and port combination.
Endpoint fromAsio(boost::asio::ip::address const &address)
Convert to Endpoint.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.