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/beast/core/bind_handler.hpp>
26 #include <boost/asio.hpp>
27 #include <boost/asio/ip/tcp.hpp>
28 #include <boost/asio/ssl.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);
61 boost::asio::io_service& s,
62 boost::asio::ssl::context& c)
104 return mSocket->lowest_layer ();
114 boost::system::error_code
cancel (boost::system::error_code& ec)
121 if ((type == ssl_socket::client) || (
mSecure))
125 mSocket->async_handshake (type, cbFunc);
133 boost::beast::bind_handler(cbFunc,
error_code()));
138 mSocket->next_layer ().async_receive (
140 boost::asio::socket_base::message_peek,
144 std::placeholders::_1,
145 std::placeholders::_2));
149 template <
typename ShutdownHandler>
153 mSocket->async_shutdown (handler);
161 catch (boost::system::system_error& e)
167 boost::beast::bind_handler(handler, ec));
171 template <
typename Seq,
typename Handler>
175 mSocket->async_read_some (buffers, handler);
180 template <
typename Seq,
typename Condition,
typename Handler>
182 const Seq& buffers, Condition condition, Handler handler)
185 boost::asio::async_read_until(
186 *
mSocket, buffers, condition, handler);
188 boost::asio::async_read_until(
192 template <
typename Allocator,
typename Handler>
197 boost::asio::async_read_until(*
mSocket, buffers, delim, handler);
199 boost::asio::async_read_until(
203 template <
typename Allocator,
typename MatchCondition,
typename Handler>
205 MatchCondition cond, Handler handler)
208 boost::asio::async_read_until(*
mSocket, buffers, cond, handler);
210 boost::asio::async_read_until(
214 template <
typename Buf,
typename Handler>
218 boost::asio::async_write(*
mSocket, buffers, handler);
220 boost::asio::async_write(
PlainSocket (), buffers, handler);
223 template <
typename Allocator,
typename Handler>
224 void async_write (boost::asio::basic_streambuf<Allocator>& buffers,
228 boost::asio::async_write(*
mSocket, buffers, handler);
230 boost::asio::async_write(
PlainSocket(), buffers, handler);
233 template <
typename Buf,
typename Condition,
typename Handler>
234 void async_read (
const Buf& buffers, Condition cond, Handler handler)
237 boost::asio::async_read(*
mSocket, buffers, cond, handler);
239 boost::asio::async_read(
PlainSocket(), buffers, cond, handler);
242 template <
typename Allocator,
typename Condition,
typename Handler>
243 void async_read (boost::asio::basic_streambuf<Allocator>& buffers,
244 Condition cond, Handler handler)
247 boost::asio::async_read (*
mSocket, buffers, cond, handler);
249 boost::asio::async_read (
PlainSocket (), buffers, cond, handler);
252 template <
typename Buf,
typename Handler>
256 boost::asio::async_read (*
mSocket, buffers, handler);
258 boost::asio::async_read (
PlainSocket (), buffers, handler);
261 template <
typename Seq,
typename Handler>
265 mSocket->async_write_some (buffers, handler);
267 PlainSocket ().async_write_some (buffers, handler);
279 "Handle autodetect error: " << ec;
283 ((bytesTransferred < 2)
285 ((bytesTransferred < 3)
287 ((bytesTransferred < 4)
291 JLOG (
j_.
trace()) <<
"non-SSL";
300 mSocket->async_handshake (ssl_socket::server, cbFunc);