20#ifndef RIPPLE_WEBSOCKET_AUTOSOCKET_AUTOSOCKET_H_INCLUDED
21#define RIPPLE_WEBSOCKET_AUTOSOCKET_AUTOSOCKET_H_INCLUDED
23#include <xrpl/basics/Log.h>
24#include <xrpl/beast/net/IPAddressConversion.h>
26#include <boost/asio.hpp>
27#include <boost/asio/ip/tcp.hpp>
28#include <boost/asio/ssl.hpp>
29#include <boost/beast/core/bind_handler.hpp>
39 using ssl_socket = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
50 boost::asio::io_context& s,
51 boost::asio::ssl::context& c,
55 ,
mBuffer((plainOnly || secureOnly) ? 0 : 4)
56 ,
j_{
beast::Journal::getNullSink()}
61 AutoSocket(boost::asio::io_context& s, boost::asio::ssl::context& c)
108 boost::system::error_code
117 if ((type == ssl_socket::client) || (
mSecure))
121 mSocket->async_handshake(type, cbFunc);
129 boost::beast::bind_handler(cbFunc,
error_code()));
134 mSocket->next_layer().async_receive(
136 boost::asio::socket_base::message_peek,
141 std::placeholders::_1,
142 std::placeholders::_2));
146 template <
typename ShutdownHandler>
151 mSocket->async_shutdown(handler);
159 catch (boost::system::system_error& e)
165 boost::beast::bind_handler(handler, ec));
169 template <
typename Seq,
typename Handler>
174 mSocket->async_read_some(buffers, handler);
179 template <
typename Seq,
typename Condition,
typename Handler>
184 boost::asio::async_read_until(
185 *
mSocket, buffers, condition, handler);
187 boost::asio::async_read_until(
191 template <
typename Allocator,
typename Handler>
194 boost::asio::basic_streambuf<Allocator>& buffers,
199 boost::asio::async_read_until(*
mSocket, buffers, delim, handler);
201 boost::asio::async_read_until(
205 template <
typename Allocator,
typename MatchCondition,
typename Handler>
208 boost::asio::basic_streambuf<Allocator>& buffers,
213 boost::asio::async_read_until(*
mSocket, buffers, cond, handler);
215 boost::asio::async_read_until(
219 template <
typename Buf,
typename Handler>
224 boost::asio::async_write(*
mSocket, buffers, handler);
226 boost::asio::async_write(
PlainSocket(), buffers, handler);
229 template <
typename Allocator,
typename Handler>
232 boost::asio::basic_streambuf<Allocator>& buffers,
236 boost::asio::async_write(*
mSocket, buffers, handler);
238 boost::asio::async_write(
PlainSocket(), buffers, handler);
241 template <
typename Buf,
typename Condition,
typename Handler>
243 async_read(Buf
const& buffers, Condition cond, Handler handler)
246 boost::asio::async_read(*
mSocket, buffers, cond, handler);
248 boost::asio::async_read(
PlainSocket(), buffers, cond, handler);
251 template <
typename Allocator,
typename Condition,
typename Handler>
254 boost::asio::basic_streambuf<Allocator>& buffers,
259 boost::asio::async_read(*
mSocket, buffers, cond, handler);
261 boost::asio::async_read(
PlainSocket(), buffers, cond, handler);
264 template <
typename Buf,
typename Handler>
269 boost::asio::async_read(*
mSocket, buffers, handler);
271 boost::asio::async_read(
PlainSocket(), buffers, handler);
274 template <
typename Seq,
typename Handler>
279 mSocket->async_write_some(buffers, handler);
289 size_t bytesTransferred)
295 JLOG(
j_.
warn()) <<
"Handle autodetect error: " << ec;
300 ((bytesTransferred < 2) ||
302 ((bytesTransferred < 3) ||
304 ((bytesTransferred < 4) ||
317 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.