diff options
-rw-r--r-- | src/server/authserver/Server/AuthSession.cpp | 10 | ||||
-rw-r--r-- | src/server/authserver/Server/AuthSession.h | 9 | ||||
-rw-r--r-- | src/server/game/Server/WorldSocket.h | 6 | ||||
-rw-r--r-- | src/server/shared/Networking/Socket.h | 27 | ||||
-rw-r--r-- | src/server/shared/Packets/ByteBuffer.h | 13 |
5 files changed, 43 insertions, 22 deletions
diff --git a/src/server/authserver/Server/AuthSession.cpp b/src/server/authserver/Server/AuthSession.cpp index cc5dc8cbb1b..697fe027f5e 100644 --- a/src/server/authserver/Server/AuthSession.cpp +++ b/src/server/authserver/Server/AuthSession.cpp @@ -18,7 +18,6 @@ #include "AuthSession.h" #include "Log.h" -#include "ByteBuffer.h" #include "AuthCodes.h" #include "Database/DatabaseEnv.h" #include "SHA1.h" @@ -173,19 +172,16 @@ void AuthSession::ReadDataHandler(boost::system::error_code error, size_t transf CloseSocket(); } -void AuthSession::AsyncWrite(ByteBuffer const& packet) +void AuthSession::AsyncWrite(ByteBuffer& packet) { - std::vector<uint8> data(packet.size()); - std::memcpy(data.data(), packet.contents(), packet.size()); - std::lock_guard<std::mutex> guard(_writeLock); bool needsWriteStart = _writeQueue.empty(); - _writeQueue.push(std::move(data)); + _writeQueue.push(std::move(packet)); if (needsWriteStart) - AsyncWrite(_writeQueue.front()); + Base::AsyncWrite(_writeQueue.front()); } bool AuthSession::HandleLogonChallenge() diff --git a/src/server/authserver/Server/AuthSession.h b/src/server/authserver/Server/AuthSession.h index eedffb86ff8..822f3c334fc 100644 --- a/src/server/authserver/Server/AuthSession.h +++ b/src/server/authserver/Server/AuthSession.h @@ -22,16 +22,17 @@ #include "Common.h" #include "Socket.h" #include "BigNumber.h" +#include "ByteBuffer.h" #include <memory> #include <boost/asio/ip/tcp.hpp> using boost::asio::ip::tcp; struct AuthHandler; -class ByteBuffer; -class AuthSession : public Socket<AuthSession> +class AuthSession : public Socket<AuthSession, ByteBuffer> { + typedef Socket<AuthSession, ByteBuffer> Base; public: static std::unordered_map<uint8, AuthHandler> InitHandlers(); @@ -47,8 +48,8 @@ public: AsyncReadHeader(); } - using Socket<AuthSession>::AsyncWrite; - void AsyncWrite(ByteBuffer const& packet); + using Base::AsyncWrite; + void AsyncWrite(ByteBuffer& packet); protected: void ReadHeaderHandler(boost::system::error_code error, size_t transferedBytes) override; diff --git a/src/server/game/Server/WorldSocket.h b/src/server/game/Server/WorldSocket.h index 57732547b17..f355308ff7a 100644 --- a/src/server/game/Server/WorldSocket.h +++ b/src/server/game/Server/WorldSocket.h @@ -40,8 +40,10 @@ struct ClientPktHeader #pragma pack(pop) -class WorldSocket : public Socket<WorldSocket> +class WorldSocket : public Socket<WorldSocket, std::vector<uint8> > { + typedef Socket<WorldSocket, std::vector<uint8> > Base; + public: WorldSocket(tcp::socket&& socket); @@ -50,8 +52,8 @@ public: void Start() override; + using Base::AsyncWrite; void AsyncWrite(WorldPacket const& packet); - using Socket<WorldSocket>::AsyncWrite; protected: void ReadHeaderHandler(boost::system::error_code error, size_t transferedBytes) override; diff --git a/src/server/shared/Networking/Socket.h b/src/server/shared/Networking/Socket.h index 676418c27a7..96f597257be 100644 --- a/src/server/shared/Networking/Socket.h +++ b/src/server/shared/Networking/Socket.h @@ -1,6 +1,5 @@ /* * Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/> - * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/> * * 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 @@ -26,12 +25,13 @@ #include <queue> #include <memory> #include <functional> +#include <type_traits> #include <boost/asio/ip/tcp.hpp> #include <boost/asio/write.hpp> using boost::asio::ip::tcp; -template<class T> +template<class T, class PacketType> class Socket : public std::enable_shared_from_this<T> { public: @@ -44,12 +44,14 @@ public: void AsyncReadHeader() { - _socket.async_read_some(boost::asio::buffer(_readBuffer, _headerSize), std::bind(&Socket<T>::ReadHeaderHandlerInternal, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2)); + _socket.async_read_some(boost::asio::buffer(_readBuffer, _headerSize), std::bind(&Socket<T, PacketType>::ReadHeaderHandlerInternal, this->shared_from_this(), + std::placeholders::_1, std::placeholders::_2)); } void AsyncReadData(std::size_t size, std::size_t bufferOffset) { - _socket.async_read_some(boost::asio::buffer(&_readBuffer[bufferOffset], size), std::bind(&Socket<T>::ReadDataHandlerInternal, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2)); + _socket.async_read_some(boost::asio::buffer(&_readBuffer[bufferOffset], size), std::bind(&Socket<T, PacketType>::ReadDataHandlerInternal, this->shared_from_this(), + std::placeholders::_1, std::placeholders::_2)); } void ReadData(std::size_t size, std::size_t bufferOffset) @@ -57,9 +59,10 @@ public: _socket.read_some(boost::asio::buffer(&_readBuffer[bufferOffset], size)); } - void AsyncWrite(std::vector<uint8> const& data) + void AsyncWrite(PacketType const& data) { - boost::asio::async_write(_socket, boost::asio::buffer(data), std::bind(&Socket<T>::WriteHandler, this->shared_from_this(), std::placeholders::_1, std::placeholders::_2)); + boost::asio::async_write(_socket, boost::asio::buffer(data), std::bind(&Socket<T, PacketType>::WriteHandler, this->shared_from_this(), std::placeholders::_1, + std::placeholders::_2)); } bool IsOpen() const { return _socket.is_open(); } @@ -68,7 +71,8 @@ public: boost::system::error_code socketError; _socket.close(socketError); if (socketError) - TC_LOG_DEBUG("network", "Socket::CloseSocket: %s errored when closing socket: %i (%s)", GetRemoteIpAddress().to_string().c_str(), socketError.value(), socketError.message().c_str()); + TC_LOG_DEBUG("network", "Socket::CloseSocket: %s errored when closing socket: %i (%s)", GetRemoteIpAddress().to_string().c_str(), + socketError.value(), socketError.message().c_str()); } uint8* GetReadBuffer() { return _readBuffer; } @@ -78,7 +82,7 @@ protected: virtual void ReadDataHandler(boost::system::error_code error, size_t transferedBytes) = 0; std::mutex _writeLock; - std::queue<std::vector<uint8> > _writeQueue; + std::queue<PacketType> _writeQueue; private: void ReadHeaderHandlerInternal(boost::system::error_code error, size_t transferedBytes) { ReadHeaderHandler(error, transferedBytes); } @@ -90,6 +94,7 @@ private: { std::lock_guard<std::mutex> deleteGuard(_writeLock); + DeletePacket(_writeQueue.front()); _writeQueue.pop(); if (!_writeQueue.empty()) @@ -99,6 +104,12 @@ private: CloseSocket(); } + template<typename Q = PacketType> + typename std::enable_if<std::is_pointer<Q>::value>::type DeletePacket(PacketType& packet) { delete packet; } + + template<typename Q = PacketType> + typename std::enable_if<!std::is_pointer<Q>::value>::type DeletePacket(PacketType const& /*packet*/) { } + tcp::socket _socket; uint8 _readBuffer[4096]; diff --git a/src/server/shared/Packets/ByteBuffer.h b/src/server/shared/Packets/ByteBuffer.h index 81c6bcd977c..311143b384c 100644 --- a/src/server/shared/Packets/ByteBuffer.h +++ b/src/server/shared/Packets/ByteBuffer.h @@ -32,6 +32,7 @@ #include <cstring> #include <time.h> #include <math.h> +#include <boost/asio/buffer.hpp> // Root of ByteBuffer exception hierarchy class ByteBufferException : public std::exception @@ -608,5 +609,15 @@ inline void ByteBuffer::read_skip<std::string>() read_skip<char*>(); } -#endif +namespace boost +{ + namespace asio + { + inline const_buffers_1 buffer(ByteBuffer const& packet) + { + return buffer(packet.contents(), packet.size()); + } + } +} +#endif |