20 #ifndef RIPPLE_WEBSOCKET_AUTOSOCKET_AUTOSOCKET_H_INCLUDED
21 #define RIPPLE_WEBSOCKET_AUTOSOCKET_AUTOSOCKET_H_INCLUDED
23 #include <ripple/basics/Log.h>
24 #include <ripple/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)
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);