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)
106 return mSocket->lowest_layer();
117 boost::system::error_code
126 if ((type == ssl_socket::client) || (
mSecure))
130 mSocket->async_handshake(type, cbFunc);
138 boost::beast::bind_handler(cbFunc,
error_code()));
143 mSocket->next_layer().async_receive(
145 boost::asio::socket_base::message_peek,
150 std::placeholders::_1,
151 std::placeholders::_2));
155 template <
typename ShutdownHandler>
160 mSocket->async_shutdown(handler);
168 catch (boost::system::system_error& e)
174 boost::beast::bind_handler(handler, ec));
178 template <
typename Seq,
typename Handler>
183 mSocket->async_read_some(buffers, handler);
188 template <
typename Seq,
typename Condition,
typename Handler>
193 boost::asio::async_read_until(
194 *
mSocket, buffers, condition, handler);
196 boost::asio::async_read_until(
200 template <
typename Allocator,
typename Handler>
203 boost::asio::basic_streambuf<Allocator>& buffers,
208 boost::asio::async_read_until(*
mSocket, buffers, delim, handler);
210 boost::asio::async_read_until(
214 template <
typename Allocator,
typename MatchCondition,
typename Handler>
217 boost::asio::basic_streambuf<Allocator>& buffers,
222 boost::asio::async_read_until(*
mSocket, buffers, cond, handler);
224 boost::asio::async_read_until(
228 template <
typename Buf,
typename Handler>
233 boost::asio::async_write(*
mSocket, buffers, handler);
235 boost::asio::async_write(
PlainSocket(), buffers, handler);
238 template <
typename Allocator,
typename Handler>
241 boost::asio::basic_streambuf<Allocator>& buffers,
245 boost::asio::async_write(*
mSocket, buffers, handler);
247 boost::asio::async_write(
PlainSocket(), buffers, handler);
250 template <
typename Buf,
typename Condition,
typename Handler>
252 async_read(
const Buf& buffers, Condition cond, Handler handler)
255 boost::asio::async_read(*
mSocket, buffers, cond, handler);
257 boost::asio::async_read(
PlainSocket(), buffers, cond, handler);
260 template <
typename Allocator,
typename Condition,
typename Handler>
263 boost::asio::basic_streambuf<Allocator>& buffers,
268 boost::asio::async_read(*
mSocket, buffers, cond, handler);
270 boost::asio::async_read(
PlainSocket(), buffers, cond, handler);
273 template <
typename Buf,
typename Handler>
278 boost::asio::async_read(*
mSocket, buffers, handler);
280 boost::asio::async_read(
PlainSocket(), buffers, handler);
283 template <
typename Seq,
typename Handler>
288 mSocket->async_write_some(buffers, handler);
298 size_t bytesTransferred)
304 JLOG(
j_.
warn()) <<
"Handle autodetect error: " << ec;
309 ((bytesTransferred < 2) ||
311 ((bytesTransferred < 3) ||
313 ((bytesTransferred < 4) ||
326 mSocket->async_handshake(ssl_socket::server, cbFunc);