aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/server/shared/Networking/Socket.h42
-rw-r--r--src/server/shared/Networking/SslSocket.h90
2 files changed, 127 insertions, 5 deletions
diff --git a/src/server/shared/Networking/Socket.h b/src/server/shared/Networking/Socket.h
index 0674ede57d8..7d7ac8ac7ee 100644
--- a/src/server/shared/Networking/Socket.h
+++ b/src/server/shared/Networking/Socket.h
@@ -34,7 +34,34 @@ using boost::asio::ip::tcp;
#define TC_SOCKET_USE_IOCP
#endif
-template<class T>
+/**
+ @class Socket
+
+ Base async socket implementation
+
+ @tparam T derived class type (CRTP)
+ @tparam Stream stream type used for operations on socket
+ Stream must implement the following methods:
+
+ void close(boost::system::error_code& error);
+
+ void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code& shutdownError);
+
+ template<typename MutableBufferSequence, typename ReadHandlerType>
+ void async_read_some(MutableBufferSequence const& buffers, ReadHandlerType&& handler);
+
+ template<typename ConstBufferSequence, typename WriteHandlerType>
+ void async_write_some(ConstBufferSequence const& buffers, WriteHandlerType&& handler);
+
+ template<typename ConstBufferSequence>
+ std::size_t write_some(ConstBufferSequence const& buffers, boost::system::error_code& error);
+
+ template<typename SettableSocketOption>
+ void set_option(SettableSocketOption const& option, boost::system::error_code& error);
+
+ tcp::socket::endpoint_type remote_endpoint() const;
+*/
+template<class T, class Stream = tcp::socket>
class Socket : public std::enable_shared_from_this<T>
{
public:
@@ -87,7 +114,7 @@ public:
_readBuffer.Normalize();
_readBuffer.EnsureFreeSpace();
_socket.async_read_some(boost::asio::buffer(_readBuffer.GetWritePointer(), _readBuffer.GetRemainingSpace()),
- std::bind(&Socket<T>::ReadHandlerInternal, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
+ std::bind(&Socket<T, Stream>::ReadHandlerInternal, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
void AsyncReadWithCallback(void (T::*callback)(boost::system::error_code, std::size_t))
@@ -145,10 +172,10 @@ protected:
#ifdef TC_SOCKET_USE_IOCP
MessageBuffer& buffer = _writeQueue.front();
- _socket.async_write_some(boost::asio::buffer(buffer.GetReadPointer(), buffer.GetActiveSize()), std::bind(&Socket<T>::WriteHandler,
+ _socket.async_write_some(boost::asio::buffer(buffer.GetReadPointer(), buffer.GetActiveSize()), std::bind(&Socket<T, Stream>::WriteHandler,
this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
#else
- _socket.async_write_some(boost::asio::null_buffers(), std::bind(&Socket<T>::WriteHandlerWrapper,
+ _socket.async_write_some(boost::asio::null_buffers(), std::bind(&Socket<T, Stream>::WriteHandlerWrapper,
this->shared_from_this(), std::placeholders::_1, std::placeholders::_2));
#endif
@@ -164,6 +191,11 @@ protected:
GetRemoteIpAddress().to_string().c_str(), err.value(), err.message().c_str());
}
+ Stream& underlying_stream()
+ {
+ return _socket;
+ }
+
private:
void ReadHandlerInternal(boost::system::error_code error, size_t transferredBytes)
{
@@ -248,7 +280,7 @@ private:
#endif
- tcp::socket _socket;
+ Stream _socket;
boost::asio::ip::address _remoteAddress;
uint16 _remotePort;
diff --git a/src/server/shared/Networking/SslSocket.h b/src/server/shared/Networking/SslSocket.h
new file mode 100644
index 00000000000..45a3fba88f0
--- /dev/null
+++ b/src/server/shared/Networking/SslSocket.h
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SslSocket_h__
+#define SslSocket_h__
+
+#include <boost/asio/ip/tcp.hpp>
+#include <boost/asio/ssl/stream.hpp>
+#include <boost/system/error_code.hpp>
+
+using boost::asio::ip::tcp;
+namespace boostssl = boost::asio::ssl;
+
+template<class SslContext>
+class SslSocket
+{
+public:
+ explicit SslSocket(tcp::socket&& socket) : _socket(std::move(socket)), _sslSocket(_socket, SslContext::instance())
+ {
+ _sslSocket.set_verify_mode(boostssl::verify_none);
+ }
+
+ // adapting tcp::socket api
+ void close(boost::system::error_code& error)
+ {
+ _socket.close(error);
+ }
+
+ void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code& shutdownError)
+ {
+ _sslSocket.shutdown(shutdownError);
+ _socket.shutdown(what, shutdownError);
+ }
+
+ template<typename MutableBufferSequence, typename ReadHandlerType>
+ void async_read_some(MutableBufferSequence const& buffers, ReadHandlerType&& handler)
+ {
+ _sslSocket.async_read_some(buffers, std::move(handler));
+ }
+
+ template<typename ConstBufferSequence, typename WriteHandlerType>
+ void async_write_some(ConstBufferSequence const& buffers, WriteHandlerType&& handler)
+ {
+ _sslSocket.async_write_some(buffers, std::move(handler));
+ }
+
+ template<typename ConstBufferSequence>
+ std::size_t write_some(ConstBufferSequence const& buffers, boost::system::error_code& error)
+ {
+ return _sslSocket.write_some(buffers, error);
+ }
+
+ template<typename SettableSocketOption>
+ void set_option(SettableSocketOption const& option, boost::system::error_code& error)
+ {
+ _socket.set_option(option, error);
+ }
+
+ tcp::socket::endpoint_type remote_endpoint() const
+ {
+ return _socket.remote_endpoint();
+ }
+
+ // ssl api
+ template<typename HandshakeHandlerType>
+ void async_handshake(boostssl::stream_base::handshake_type type, HandshakeHandlerType&& handler)
+ {
+ _sslSocket.async_handshake(type, std::move(handler));
+ }
+
+private:
+ tcp::socket _socket;
+ boostssl::stream<tcp::socket&> _sslSocket;
+};
+
+#endif // SslSocket_h__