1#ifndef XRPL_WEBSOCKET_AUTOSOCKET_AUTOSOCKET_H_INCLUDED
2#define XRPL_WEBSOCKET_AUTOSOCKET_AUTOSOCKET_H_INCLUDED
4#include <xrpl/basics/Log.h>
5#include <xrpl/beast/net/IPAddressConversion.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>
20 using ssl_socket = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
31 boost::asio::io_context& s,
32 boost::asio::ssl::context& c,
36 ,
mBuffer((plainOnly || secureOnly) ? 0 : 4)
37 ,
j_{
beast::Journal::getNullSink()}
42 AutoSocket(boost::asio::io_context& s, boost::asio::ssl::context& c)
89 boost::system::error_code
90 cancel(boost::system::error_code& ec)
98 if ((type == ssl_socket::client) || (
mSecure))
102 mSocket->async_handshake(type, cbFunc);
110 boost::beast::bind_handler(cbFunc,
error_code()));
115 mSocket->next_layer().async_receive(
117 boost::asio::socket_base::message_peek,
122 std::placeholders::_1,
123 std::placeholders::_2));
127 template <
typename ShutdownHandler>
132 mSocket->async_shutdown(handler);
140 catch (boost::system::system_error& e)
146 boost::beast::bind_handler(handler, ec));
150 template <
typename Seq,
typename Handler>
155 mSocket->async_read_some(buffers, handler);
160 template <
typename Seq,
typename Condition,
typename Handler>
165 boost::asio::async_read_until(
166 *
mSocket, buffers, condition, handler);
168 boost::asio::async_read_until(
172 template <
typename Allocator,
typename Handler>
175 boost::asio::basic_streambuf<Allocator>& buffers,
180 boost::asio::async_read_until(*
mSocket, buffers, delim, handler);
182 boost::asio::async_read_until(
186 template <
typename Allocator,
typename MatchCondition,
typename Handler>
189 boost::asio::basic_streambuf<Allocator>& buffers,
194 boost::asio::async_read_until(*
mSocket, buffers, cond, handler);
196 boost::asio::async_read_until(
200 template <
typename Buf,
typename Handler>
205 boost::asio::async_write(*
mSocket, buffers, handler);
207 boost::asio::async_write(
PlainSocket(), buffers, handler);
210 template <
typename Allocator,
typename Handler>
213 boost::asio::basic_streambuf<Allocator>& buffers,
217 boost::asio::async_write(*
mSocket, buffers, handler);
219 boost::asio::async_write(
PlainSocket(), buffers, handler);
222 template <
typename Buf,
typename Condition,
typename Handler>
224 async_read(Buf
const& buffers, Condition cond, Handler handler)
227 boost::asio::async_read(*
mSocket, buffers, cond, handler);
229 boost::asio::async_read(
PlainSocket(), buffers, cond, handler);
232 template <
typename Allocator,
typename Condition,
typename Handler>
235 boost::asio::basic_streambuf<Allocator>& buffers,
240 boost::asio::async_read(*
mSocket, buffers, cond, handler);
242 boost::asio::async_read(
PlainSocket(), buffers, cond, handler);
245 template <
typename Buf,
typename Handler>
250 boost::asio::async_read(*
mSocket, buffers, handler);
252 boost::asio::async_read(
PlainSocket(), buffers, handler);
255 template <
typename Seq,
typename Handler>
260 mSocket->async_write_some(buffers, handler);
270 size_t bytesTransferred)
276 JLOG(
j_.
warn()) <<
"Handle autodetect error: " << ec;
281 ((bytesTransferred < 2) ||
283 ((bytesTransferred < 3) ||
285 ((bytesTransferred < 4) ||
298 mSocket->async_handshake(ssl_socket::server, cbFunc);
void async_write(boost::asio::basic_streambuf< Allocator > &buffers, Handler handler)
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
beast::IP::Endpoint remote_endpoint()
void async_read(boost::asio::basic_streambuf< Allocator > &buffers, Condition cond, Handler handler)
void handle_autodetect(callback cbFunc, error_code const &ec, size_t bytesTransferred)
boost::system::error_code error_code
AutoSocket(boost::asio::io_context &s, boost::asio::ssl::context &c)
void async_handshake(handshake_type type, callback cbFunc)
void async_read_until(boost::asio::basic_streambuf< Allocator > &buffers, MatchCondition cond, Handler handler)
void async_read_until(boost::asio::basic_streambuf< Allocator > &buffers, std::string const &delim, Handler handler)
void async_read(Buf const &buffers, Handler handler)
void async_write(Buf const &buffers, Handler handler)
lowest_layer_type & lowest_layer()
beast::IP::Endpoint local_endpoint()
void async_read_until(Seq const &buffers, Condition condition, Handler handler)
plain_socket & PlainSocket()
void async_read(Buf const &buffers, Condition cond, Handler handler)
void async_read_some(Seq const &buffers, Handler handler)
std::vector< char > mBuffer
void swap(AutoSocket &s) noexcept
void async_write_some(Seq const &buffers, Handler handler)
void async_shutdown(ShutdownHandler handler)
boost::system::error_code cancel(boost::system::error_code &ec)
boost::asio::ssl::stream< boost::asio::ip::tcp::socket > ssl_socket
A version-independent IP address and port combination.
A generic endpoint for log messages.
Stream trace() const
Severity stream access functions.
Endpoint from_asio(boost::asio::ip::address const &address)
Convert to Endpoint.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.