TrinityCore
WorldSocket.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef __WORLDSOCKET_H__
19#define __WORLDSOCKET_H__
20
22#include "AuthDefines.h"
23#include "DatabaseEnvFwd.h"
24#include "MessageBuffer.h"
25#include "Socket.h"
26#include "WorldPacket.h"
27#include "WorldPacketCrypt.h"
28#include "MPSCQueue.h"
29#include <array>
30#include <boost/asio/ip/tcp.hpp>
31#include <mutex>
32
33typedef struct z_stream_s z_stream;
35class WorldPacket;
36class WorldSession;
37enum ConnectionType : int8;
38enum OpcodeClient : uint16;
39
41{
42public:
43 EncryptablePacket(WorldPacket const& packet, bool encrypt) : WorldPacket(packet), _encrypt(encrypt)
44 {
45 SocketQueueLink.store(nullptr, std::memory_order_relaxed);
46 }
47
48 bool NeedsEncryption() const { return _encrypt; }
49
50 std::atomic<EncryptablePacket*> SocketQueueLink;
51
52private:
54};
55
56namespace WorldPackets
57{
58 class ServerPacket;
59 namespace Auth
60 {
61 class AuthSession;
62 class AuthContinuedSession;
63 class ConnectToFailed;
64 class Ping;
65 }
66}
67
68#pragma pack(push, 1)
69
70struct PacketHeader
71{
74
75 bool IsValidSize() { return Size < 0x10000; }
76};
77
79{
81};
82
83#pragma pack(pop)
84
85class TC_GAME_API WorldSocket : public Socket<WorldSocket>
86{
87 static std::string const ServerConnectionInitialize;
88 static std::string const ClientConnectionInitialize;
90
91 static uint8 const AuthCheckSeed[16];
92 static uint8 const SessionKeySeed[16];
93 static uint8 const ContinuedSessionSeed[16];
94 static uint8 const EncryptionKeySeed[16];
95
97
98public:
99 WorldSocket(boost::asio::ip::tcp::socket&& socket);
100 ~WorldSocket();
101
102 WorldSocket(WorldSocket const& right) = delete;
103 WorldSocket& operator=(WorldSocket const& right) = delete;
104
105 void Start() override;
106 bool Update() override;
107
108 void SendPacket(WorldPacket const& packet);
109
110 ConnectionType GetConnectionType() const { return _type; }
111
112 void SendAuthResponseError(uint32 code);
113 void SetWorldSession(WorldSession* session);
114 void SetSendBufferSize(std::size_t sendBufferSize) { _sendBufferSize = sendBufferSize; }
115
116protected:
117 void OnClose() override;
118 void ReadHandler() override;
119 bool ReadHeaderHandler();
120
122 {
123 Ok = 0,
124 Error = 1,
125 WaitingForQuery = 2
126 };
127
128 ReadDataHandlerResult ReadDataHandler();
129private:
130 void CheckIpCallback(PreparedQueryResult result);
131 void InitializeHandler(boost::system::error_code const& error, std::size_t transferedBytes);
132
135 void LogOpcodeText(OpcodeClient opcode, std::unique_lock<std::mutex> const& guard) const;
137 void SendPacketAndLogOpcode(WorldPacket const& packet);
138 void WritePacketToBuffer(EncryptablePacket const& packet, MessageBuffer& buffer);
139 uint32 CompressPacket(uint8* buffer, WorldPacket const& packet);
140
141 void HandleSendAuthSession();
142 void HandleAuthSession(std::shared_ptr<WorldPackets::Auth::AuthSession> authSession);
143 void HandleAuthSessionCallback(std::shared_ptr<WorldPackets::Auth::AuthSession> authSession, PreparedQueryResult result);
144 void HandleAuthContinuedSession(std::shared_ptr<WorldPackets::Auth::AuthContinuedSession> authSession);
145 void HandleAuthContinuedSessionCallback(std::shared_ptr<WorldPackets::Auth::AuthContinuedSession> authSession, PreparedQueryResult result);
146 void LoadSessionPermissionsCallback(PreparedQueryResult result);
147 void HandleConnectToFailed(WorldPackets::Auth::ConnectToFailed& connectToFailed);
148 bool HandlePing(WorldPackets::Auth::Ping& ping);
149 void HandleEnterEncryptedModeAck();
150
153
154 std::array<uint8, 16> _serverChallenge;
157 std::array<uint8, 16> _encryptKey;
158
161
166
170 std::size_t _sendBufferSize;
171
173
175 std::string _ipCountry;
176};
177
178#endif
std::array< uint8, SESSION_KEY_LENGTH > SessionKey
Definition: AuthDefines.h:25
std::shared_ptr< PreparedResultSet > PreparedQueryResult
#define TC_GAME_API
Definition: Define.h:123
uint8_t uint8
Definition: Define.h:144
int8_t int8
Definition: Define.h:140
uint64_t uint64
Definition: Define.h:141
uint16_t uint16
Definition: Define.h:143
uint32_t uint32
Definition: Define.h:142
std::chrono::steady_clock::time_point TimePoint
time_point shorthand typedefs
Definition: Duration.h:41
std::conditional_t< IntrusiveLink !=nullptr, Trinity::Impl::MPSCQueueIntrusive< T, IntrusiveLink >, Trinity::Impl::MPSCQueueNonIntrusive< T > > MPSCQueue
Definition: MPSCQueue.h:173
struct z_stream_s z_stream
Definition: WorldSocket.h:33
bool NeedsEncryption() const
Definition: WorldSocket.h:48
EncryptablePacket(WorldPacket const &packet, bool encrypt)
Definition: WorldSocket.h:43
std::atomic< EncryptablePacket * > SocketQueueLink
Definition: WorldSocket.h:50
Definition: Socket.h:63
virtual bool Update()
Definition: Socket.h:87
virtual void Start()=0
virtual void ReadHandler()=0
virtual void OnClose()
Definition: Socket.h:172
Player session in the World.
Definition: WorldSession.h:963
SessionKey _sessionKey
Definition: WorldSocket.h:156
ConnectionType _type
Definition: WorldSocket.h:151
MessageBuffer _headerBuffer
Definition: WorldSocket.h:167
ConnectionType GetConnectionType() const
Definition: WorldSocket.h:110
static std::string const ClientConnectionInitialize
Definition: WorldSocket.h:88
bool _canRequestHotfixes
Definition: WorldSocket.h:165
MessageBuffer _packetBuffer
Definition: WorldSocket.h:168
QueryCallbackProcessor _queryProcessor
Definition: WorldSocket.h:174
WorldPacketCrypt _authCrypt
Definition: WorldSocket.h:155
WorldSocket & operator=(WorldSocket const &right)=delete
std::size_t _sendBufferSize
Definition: WorldSocket.h:170
uint32 _OverSpeedPings
Definition: WorldSocket.h:160
TimePoint _LastPingTime
Definition: WorldSocket.h:159
WorldSession * _worldSession
Definition: WorldSocket.h:163
uint64 _key
Definition: WorldSocket.h:152
std::string _ipCountry
Definition: WorldSocket.h:175
z_stream * _compressionStream
Definition: WorldSocket.h:172
void SetSendBufferSize(std::size_t sendBufferSize)
Definition: WorldSocket.h:114
MPSCQueue< EncryptablePacket, &EncryptablePacket::SocketQueueLink > _bufferQueue
Definition: WorldSocket.h:169
static std::string const ServerConnectionInitialize
Definition: WorldSocket.h:87
WorldSocket(WorldSocket const &right)=delete
std::array< uint8, 16 > _encryptKey
Definition: WorldSocket.h:157
std::array< uint8, 16 > _serverChallenge
Definition: WorldSocket.h:154
std::mutex _worldSessionLock
Definition: WorldSocket.h:162
Socket< WorldSocket > BaseSocket
Definition: WorldSocket.h:96
static uint32 const MinSizeForCompression
Definition: WorldSocket.h:89
ConnectionType
Definition: Opcodes.h:29
OpcodeClient
Definition: Opcodes.h:46
void Error(char const *file, int line, char const *function, char const *message)
Definition: Errors.cpp:110
bool IsValidSize()
Definition: WorldSocket.h:75
uint32 Size
Definition: WorldSocket.h:72
uint8 Tag[12]
Definition: WorldSocket.h:73