3#include <xrpl/basics/Log.h>
4#include <xrpl/beast/net/IPAddressConversion.h>
6#include <boost/asio.hpp>
7#include <boost/asio/ip/tcp.hpp>
8#include <boost/asio/ssl.hpp>
9#include <boost/beast/core/bind_handler.hpp>
19 using ssl_socket = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
30 boost::asio::io_context& s,
31 boost::asio::ssl::context& c,
35 ,
buffer_((plainOnly || secureOnly) ? 0 : 4)
36 ,
j_{
beast::Journal::getNullSink()}
41 AutoSocket(boost::asio::io_context& s, boost::asio::ssl::context& c)
88 boost::system::error_code
89 cancel(boost::system::error_code& ec)
97 if ((type == ssl_socket::client) || (
secure_))
101 socket_->async_handshake(type, cbFunc);
112 socket_->next_layer().async_receive(
114 boost::asio::socket_base::message_peek,
119 std::placeholders::_1,
120 std::placeholders::_2));
124 template <
typename ShutdownHandler>
130 socket_->async_shutdown(handler);
137 lowestLayer().shutdown(plain_socket::shutdown_both);
139 catch (boost::system::system_error
const& e)
143 post(
socket_->get_executor(), boost::beast::bind_handler(handler, ec));
147 template <
typename Seq,
typename Handler>
153 socket_->async_read_some(buffers, handler);
161 template <
typename Seq,
typename Condition,
typename Handler>
167 boost::asio::async_read_until(*
socket_, buffers, condition, handler);
171 boost::asio::async_read_until(
plainSocket(), buffers, condition, handler);
175 template <
typename Allocator,
typename Handler>
178 boost::asio::basic_streambuf<Allocator>& buffers,
184 boost::asio::async_read_until(*
socket_, buffers, delim, handler);
188 boost::asio::async_read_until(
plainSocket(), buffers, delim, handler);
192 template <
typename Allocator,
typename MatchCondition,
typename Handler>
195 boost::asio::basic_streambuf<Allocator>& buffers,
201 boost::asio::async_read_until(*
socket_, buffers, cond, handler);
205 boost::asio::async_read_until(
plainSocket(), buffers, cond, handler);
209 template <
typename Buf,
typename Handler>
215 boost::asio::async_write(*
socket_, buffers, handler);
219 boost::asio::async_write(
plainSocket(), buffers, handler);
223 template <
typename Allocator,
typename Handler>
225 asyncWrite(boost::asio::basic_streambuf<Allocator>& buffers, Handler handler)
229 boost::asio::async_write(*
socket_, buffers, handler);
233 boost::asio::async_write(
plainSocket(), buffers, handler);
237 template <
typename Buf,
typename Condition,
typename Handler>
239 asyncRead(Buf
const& buffers, Condition cond, Handler handler)
243 boost::asio::async_read(*
socket_, buffers, cond, handler);
247 boost::asio::async_read(
plainSocket(), buffers, cond, handler);
251 template <
typename Allocator,
typename Condition,
typename Handler>
253 asyncRead(boost::asio::basic_streambuf<Allocator>& buffers, Condition cond, Handler handler)
257 boost::asio::async_read(*
socket_, buffers, cond, handler);
261 boost::asio::async_read(
plainSocket(), buffers, cond, handler);
265 template <
typename Buf,
typename Handler>
271 boost::asio::async_read(*
socket_, buffers, handler);
275 boost::asio::async_read(
plainSocket(), buffers, handler);
279 template <
typename Seq,
typename Handler>
285 socket_->async_write_some(buffers, handler);
297 using namespace xrpl;
301 JLOG(
j_.warn()) <<
"Handle autodetect error: " << ec;
306 ((bytesTransferred < 2) || ((
buffer_[1] < 127) && (
buffer_[1] > 31))) &&
307 ((bytesTransferred < 3) || ((
buffer_[2] < 127) && (
buffer_[2] > 31))) &&
308 ((bytesTransferred < 4) || ((
buffer_[3] < 127) && (
buffer_[3] > 31))))
311 JLOG(
j_.trace()) <<
"non-SSL";
318 JLOG(
j_.trace()) <<
"SSL";
320 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()
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)
beast::IP::Endpoint remoteEndpoint()
void asyncShutdown(ShutdownHandler handler)
void swap(AutoSocket &s) noexcept
void handleAutodetect(callback cbFunc, error_code const &ec, size_t bytesTransferred)
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)
beast::IP::Endpoint localEndpoint()
void asyncWrite(boost::asio::basic_streambuf< Allocator > &buffers, Handler handler)
A version-independent IP address and port combination.
A generic endpoint for log messages.
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.