mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Merge branch 'master' of github.com:jedmccaleb/NewCoin
This commit is contained in:
32
src/cpp/ripple/AutoSocket.cpp
Normal file
32
src/cpp/ripple/AutoSocket.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
#include "AutoSocket.h"
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
void AutoSocket::handle_autodetect(const error_code& ec)
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
if (mCallback)
|
||||
mCallback(ec);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((mBuffer[0] < 127) && (mBuffer[0] > 31) &&
|
||||
(mBuffer[1] < 127) && (mBuffer[1] > 31) &&
|
||||
(mBuffer[2] < 127) && (mBuffer[2] > 31) &&
|
||||
(mBuffer[3] < 127) && (mBuffer[3] > 31))
|
||||
{ // non-SSL
|
||||
mSecure = false;
|
||||
if (mCallback)
|
||||
mCallback(ec);
|
||||
}
|
||||
else
|
||||
{ // ssl
|
||||
mSecure = true;
|
||||
SSLSocket().async_handshake(ssl_socket::server, mCallback);
|
||||
mCallback = callback();
|
||||
}
|
||||
}
|
||||
|
||||
// vim:ts=4
|
||||
99
src/cpp/ripple/AutoSocket.h
Normal file
99
src/cpp/ripple/AutoSocket.h
Normal file
@@ -0,0 +1,99 @@
|
||||
#ifndef __AUTOSOCKET_H_
|
||||
#define __AUTOSOCKET_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <boost/function.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/asio/ssl.hpp>
|
||||
|
||||
// Socket wrapper that supports both SSL and non-SSL connections.
|
||||
// Generally, handle it as you would an SSL connection.
|
||||
// To force a non-SSL connection, just don't call async_handshake.
|
||||
// To force SSL only inbound, call setSSLOnly.
|
||||
|
||||
namespace basio = boost::asio;
|
||||
namespace bassl = basio::ssl;
|
||||
|
||||
class AutoSocket
|
||||
{
|
||||
public:
|
||||
typedef bassl::stream<basio::ip::tcp::socket> ssl_socket;
|
||||
typedef ssl_socket::next_layer_type plain_socket;
|
||||
typedef boost::system::error_code error_code;
|
||||
typedef boost::function<void(error_code)> callback;
|
||||
|
||||
protected:
|
||||
ssl_socket mSocket;
|
||||
bool mSecure;
|
||||
callback mCallback;
|
||||
|
||||
std::vector<char> mBuffer;
|
||||
|
||||
public:
|
||||
AutoSocket(basio::io_service& s, bassl::context& c) : mSocket(s, c), mSecure(false), mBuffer(4) { ; }
|
||||
|
||||
bool isSecure() { return mSecure; }
|
||||
ssl_socket& SSLSocket() { return mSocket; }
|
||||
plain_socket& PlainSocket() { return mSocket.next_layer(); }
|
||||
|
||||
void setSSLOnly() { mBuffer.clear(); }
|
||||
|
||||
void async_handshake(ssl_socket::handshake_type type, callback cbFunc)
|
||||
{
|
||||
mSecure = true;
|
||||
if ((type == ssl_socket::client) || (mBuffer.empty()))
|
||||
SSLSocket().async_handshake(type, cbFunc);
|
||||
else
|
||||
{
|
||||
mCallback = cbFunc;
|
||||
PlainSocket().async_receive(basio::buffer(mBuffer), basio::socket_base::message_peek,
|
||||
boost::bind(&AutoSocket::handle_autodetect, this, 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())
|
||||
SSLSocket().async_shutdown(handler);
|
||||
else
|
||||
{
|
||||
PlainSocket().shutdown(plain_socket::shutdown_both);
|
||||
if (handler)
|
||||
mSocket.get_io_service().post(handler);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Seq, typename Handler> void async_read_some(const Seq& buffers, Handler handler)
|
||||
{
|
||||
if (isSecure())
|
||||
SSLSocket().async_read_some(buffers, handler);
|
||||
else
|
||||
PlainSocket().async_read_some(buffers, handler);
|
||||
}
|
||||
|
||||
template <typename Seq, typename Handler> void async_write_some(const Seq& buffers, Handler handler)
|
||||
{
|
||||
if (isSecure())
|
||||
SSLSocket().async_write_some(buffers, handler);
|
||||
else
|
||||
PlainSocket().async_write_some(buffers, handler);
|
||||
}
|
||||
|
||||
protected:
|
||||
void handle_autodetect(const error_code&);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
// vim:ts=4
|
||||
Reference in New Issue
Block a user