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>
25#include <boost/asio.hpp>
26#include <boost/asio/ip/tcp.hpp>
27#include <boost/asio/ssl.hpp>
28#include <boost/beast/core/bind_handler.hpp>
38 using ssl_socket = boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
49 boost::asio::io_service& s,
50 boost::asio::ssl::context& c,
54 ,
mBuffer((plainOnly || secureOnly) ? 0 : 4)
55 ,
j_{
beast::Journal::getNullSink()}
57 mSocket = std::make_unique<ssl_socket>(s, c);
60 AutoSocket(boost::asio::io_service& s, boost::asio::ssl::context& c)
107 boost::system::error_code
116 if ((type == ssl_socket::client) || (
mSecure))
120 mSocket->async_handshake(type, cbFunc);
128 boost::beast::bind_handler(cbFunc,
error_code()));
133 mSocket->next_layer().async_receive(
135 boost::asio::socket_base::message_peek,
140 std::placeholders::_1,
141 std::placeholders::_2));
145 template <
typename ShutdownHandler>
150 mSocket->async_shutdown(handler);
158 catch (boost::system::system_error& e)
164 boost::beast::bind_handler(handler, ec));
168 template <
typename Seq,
typename Handler>
173 mSocket->async_read_some(buffers, handler);
178 template <
typename Seq,
typename Condition,
typename Handler>
183 boost::asio::async_read_until(
184 *
mSocket, buffers, condition, handler);
186 boost::asio::async_read_until(
190 template <
typename Allocator,
typename Handler>
193 boost::asio::basic_streambuf<Allocator>& buffers,
198 boost::asio::async_read_until(*
mSocket, buffers, delim, handler);
200 boost::asio::async_read_until(
204 template <
typename Allocator,
typename MatchCondition,
typename Handler>
207 boost::asio::basic_streambuf<Allocator>& buffers,
212 boost::asio::async_read_until(*
mSocket, buffers, cond, handler);
214 boost::asio::async_read_until(
218 template <
typename Buf,
typename Handler>
223 boost::asio::async_write(*
mSocket, buffers, handler);
225 boost::asio::async_write(
PlainSocket(), buffers, handler);
228 template <
typename Allocator,
typename Handler>
231 boost::asio::basic_streambuf<Allocator>& buffers,
235 boost::asio::async_write(*
mSocket, buffers, handler);
237 boost::asio::async_write(
PlainSocket(), buffers, handler);
240 template <
typename Buf,
typename Condition,
typename Handler>
242 async_read(
const Buf& buffers, Condition cond, Handler handler)
245 boost::asio::async_read(*
mSocket, buffers, cond, handler);
247 boost::asio::async_read(
PlainSocket(), buffers, cond, handler);
250 template <
typename Allocator,
typename Condition,
typename Handler>
253 boost::asio::basic_streambuf<Allocator>& buffers,
258 boost::asio::async_read(*
mSocket, buffers, cond, handler);
260 boost::asio::async_read(
PlainSocket(), buffers, cond, handler);
263 template <
typename Buf,
typename Handler>
268 boost::asio::async_read(*
mSocket, buffers, handler);
270 boost::asio::async_read(
PlainSocket(), buffers, handler);
273 template <
typename Seq,
typename Handler>
278 mSocket->async_write_some(buffers, handler);
288 size_t bytesTransferred)
294 JLOG(
j_.
warn()) <<
"Handle autodetect error: " << ec;
299 ((bytesTransferred < 2) ||
301 ((bytesTransferred < 3) ||
303 ((bytesTransferred < 4) ||
316 mSocket->async_handshake(ssl_socket::server, cbFunc);
void async_write_some(const Seq &buffers, Handler handler)
void async_write(boost::asio::basic_streambuf< Allocator > &buffers, Handler handler)
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, const error_code &ec, size_t bytesTransferred)
boost::system::error_code error_code
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(const Seq &buffers, Condition condition, Handler handler)
void async_read_until(boost::asio::basic_streambuf< Allocator > &buffers, std::string const &delim, Handler handler)
lowest_layer_type & lowest_layer()
beast::IP::Endpoint local_endpoint()
void async_read_some(const Seq &buffers, Handler handler)
plain_socket & PlainSocket()
std::vector< char > mBuffer
void async_write(const Buf &buffers, Handler handler)
void swap(AutoSocket &s) noexcept
void async_read(const Buf &buffers, Condition cond, Handler handler)
AutoSocket(boost::asio::io_service &s, boost::asio::ssl::context &c)
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
void async_read(const Buf &buffers, Handler handler)
AutoSocket(boost::asio::io_service &s, boost::asio::ssl::context &c, bool secureOnly, bool plainOnly)
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.