mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Cleanups. Support socket swapping.
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
|
||||
#include <boost/function.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/smart_ptr.hpp>
|
||||
#include <boost/make_shared.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
|
||||
@@ -23,6 +25,7 @@ class AutoSocket
|
||||
{
|
||||
public:
|
||||
typedef bassl::stream<basio::ip::tcp::socket> ssl_socket;
|
||||
typedef boost::shared_ptr<ssl_socket> socket_ptr;
|
||||
typedef ssl_socket::next_layer_type plain_socket;
|
||||
typedef ssl_socket::lowest_layer_type lowest_layer_type;
|
||||
typedef ssl_socket::handshake_type handshake_type;
|
||||
@@ -30,56 +33,58 @@ public:
|
||||
typedef boost::function<void(error_code)> callback;
|
||||
|
||||
protected:
|
||||
ssl_socket mSocket;
|
||||
socket_ptr mSocket;
|
||||
bool mSecure;
|
||||
|
||||
std::vector<char> mBuffer;
|
||||
|
||||
public:
|
||||
AutoSocket(basio::io_service& s, bassl::context& c) : mSocket(s, c), mSecure(false), mBuffer(4) { ; }
|
||||
AutoSocket(basio::io_service& s, bassl::context& c) : mSecure(false), mBuffer(4)
|
||||
{
|
||||
mSocket = boost::make_shared<ssl_socket>(boost::ref(s), boost::ref(c));
|
||||
}
|
||||
|
||||
bool isSecure() { return mSecure; }
|
||||
ssl_socket& SSLSocket() { return mSocket; }
|
||||
plain_socket& PlainSocket() { return mSocket.next_layer(); }
|
||||
ssl_socket& SSLSocket() { return *mSocket; }
|
||||
plain_socket& PlainSocket() { return mSocket->next_layer(); }
|
||||
void setSSLOnly() { mBuffer.clear(); }
|
||||
|
||||
lowest_layer_type& lowest_layer() { return mSocket.lowest_layer(); }
|
||||
lowest_layer_type& lowest_layer() { return mSocket->lowest_layer(); }
|
||||
|
||||
void swap(AutoSocket& s)
|
||||
{
|
||||
mBuffer.swap(s.mBuffer);
|
||||
mSocket.swap(s.mSocket);
|
||||
std::swap(mSecure, s.mSecure);
|
||||
}
|
||||
|
||||
void async_handshake(handshake_type type, callback cbFunc)
|
||||
{
|
||||
if ((type == ssl_socket::client) || (mBuffer.empty()))
|
||||
{
|
||||
mSecure = true;
|
||||
mSocket.async_handshake(type, cbFunc);
|
||||
mSocket->async_handshake(type, cbFunc);
|
||||
}
|
||||
else
|
||||
mSocket.next_layer().async_receive(basio::buffer(mBuffer), basio::socket_base::message_peek,
|
||||
mSocket->next_layer().async_receive(basio::buffer(mBuffer), basio::socket_base::message_peek,
|
||||
boost::bind(&AutoSocket::handle_autodetect, this, cbFunc, basio::placeholders::error));
|
||||
}
|
||||
|
||||
template <typename StreamType> StreamType& getSocket()
|
||||
{
|
||||
if (isSecure())
|
||||
return SSLSocket();
|
||||
if (!isSecure())
|
||||
return PlainSocket();
|
||||
}
|
||||
|
||||
template <typename ShutdownHandler> void async_shutdown(ShutdownHandler handler)
|
||||
{
|
||||
if (isSecure())
|
||||
mSocket.async_shutdown(handler);
|
||||
mSocket->async_shutdown(handler);
|
||||
else
|
||||
{
|
||||
lowest_layer().shutdown(plain_socket::shutdown_both);
|
||||
mSocket.get_io_service().post(boost::bind(handler, error_code()));
|
||||
mSocket->get_io_service().post(boost::bind(handler, error_code()));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Seq, typename Handler> void async_read_some(const Seq& buffers, Handler handler)
|
||||
{
|
||||
if (isSecure())
|
||||
mSocket.async_read_some(buffers, handler);
|
||||
mSocket->async_read_some(buffers, handler);
|
||||
else
|
||||
PlainSocket().async_read_some(buffers, handler);
|
||||
}
|
||||
@@ -87,7 +92,7 @@ public:
|
||||
template <typename Seq, typename Handler> void async_write_some(const Seq& buffers, Handler handler)
|
||||
{
|
||||
if (isSecure())
|
||||
mSocket.async_write_some(buffers, handler);
|
||||
mSocket->async_write_some(buffers, handler);
|
||||
else
|
||||
PlainSocket().async_write_some(buffers, handler);
|
||||
}
|
||||
@@ -110,7 +115,7 @@ protected:
|
||||
else
|
||||
{ // ssl
|
||||
mSecure = true;
|
||||
mSocket.async_handshake(ssl_socket::server, cbFunc);
|
||||
mSocket->async_handshake(ssl_socket::server, cbFunc);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user