TrinityCore
Loading...
Searching...
No Matches
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 TRINITYCORE_WORLD_SOCKET_H
19#define TRINITYCORE_WORLD_SOCKET_H
20
22#include "AuthDefines.h"
23#include "DatabaseEnvFwd.h"
24#include "MPSCQueue.h"
25#include "MessageBuffer.h"
26#include "Socket.h"
27#include "WorldPacket.h"
28#include "WorldPacketCrypt.h"
29#include <array>
30#include <mutex>
31
33{
34class RealmJoinTicket;
35}
36
37typedef struct z_stream_s z_stream;
39class WorldPacket;
40class WorldSession;
41enum ConnectionType : int8;
42enum OpcodeClient : uint32;
43
45{
46public:
47 EncryptablePacket(WorldPacket const& packet, bool encrypt) : WorldPacket(packet), _encrypt(encrypt)
48 {
49 SocketQueueLink.store(nullptr, std::memory_order_relaxed);
50 }
51
52 bool NeedsEncryption() const { return _encrypt; }
53
54 std::atomic<EncryptablePacket*> SocketQueueLink;
55
56private:
58};
59
61{
62 class AuthSession;
63 class AuthContinuedSession;
64}
65
66#pragma pack(push, 1)
67
68struct PacketHeader
69{
72
73 bool IsValidSize() const { return Size < 0x10000; }
74};
75
80
81#pragma pack(pop)
82
84{
86
87 static std::array<uint8, 32> const AuthCheckSeed;
88 static std::array<uint8, 32> const SessionKeySeed;
89 static std::array<uint8, 32> const ContinuedSessionSeed;
90 static std::array<uint8, 32> const EncryptionKeySeed;
91
92 using BaseSocket = Socket;
93
94public:
97
98 WorldSocket(WorldSocket const& right) = delete;
99 WorldSocket(WorldSocket&& right) = delete;
100 WorldSocket& operator=(WorldSocket const& right) = delete;
102
103 void Start() override;
104 bool Update() override;
105
106 void SendPacket(WorldPacket const& packet);
107
108 ConnectionType GetConnectionType() const { return _type; }
109
110 void SendAuthResponseError(uint32 code);
111 void SetWorldSession(WorldSession* session);
112 void SetSendBufferSize(std::size_t sendBufferSize) { _sendBufferSize = sendBufferSize; }
113
114 void OnClose() override;
116
117 void QueueQuery(QueryCallback&& queryCallback);
118
119 void SendAuthSession();
120 bool InitializeCompression();
121
122protected:
123 bool ReadHeaderHandler();
124
126 {
127 Ok = 0,
128 Error = 1,
129 WaitingForQuery = 2
130 };
131
132 ReadDataHandlerResult ReadDataHandler();
133private:
135 void LogOpcodeText(OpcodeClient opcode) const;
136 void LogOpcodeText(OpcodeClient opcode, std::scoped_lock<std::mutex> const& guard) const;
138 void SendPacketAndLogOpcode(WorldPacket const& packet);
139 void WritePacketToBuffer(EncryptablePacket const& packet, MessageBuffer& buffer);
140 uint32 CompressPacket(uint8* buffer, WorldPacket const& packet);
141
142 ReadDataHandlerResult HandleAuthSession(WorldPacket&& packet);
143 void HandleAuthSessionCallback(WorldPackets::Auth::AuthSession const* authSession, JSON::RealmList::RealmJoinTicket* joinTicket, PreparedResultSet const* result);
144 ReadDataHandlerResult HandleAuthContinuedSession(WorldPacket&& packet);
145 void HandleAuthContinuedSessionCallback(WorldPackets::Auth::AuthContinuedSession const* authSession, PreparedResultSet const* result);
146 void LoadSessionPermissionsCallback(PreparedQueryResult result);
147 ReadDataHandlerResult HandleKeepAlive();
148 ReadDataHandlerResult HandleLogDisconnect(WorldPacket&& packet) const;
149 ReadDataHandlerResult HandleConnectToFailed(WorldPacket&& packet);
150 ReadDataHandlerResult HandlePing(WorldPacket&& packet);
151 ReadDataHandlerResult HandleEnterEncryptedModeAck();
152
155
156 std::array<uint8, 32> _serverChallenge;
159 std::array<uint8, 32> _encryptKey;
160
163
168
172 std::size_t _sendBufferSize;
173
175
177 std::string _ipCountry;
178};
179
180#endif
std::array< uint8, SESSION_KEY_LENGTH > SessionKey
Definition AuthDefines.h:25
std::shared_ptr< PreparedResultSet > PreparedQueryResult
#define TC_GAME_API
Definition Define.h:129
uint8_t uint8
Definition Define.h:156
int8_t int8
Definition Define.h:152
uint64_t uint64
Definition Define.h:153
uint32_t uint32
Definition Define.h:154
std::chrono::steady_clock::time_point TimePoint
time_point shorthand typedefs
Definition Duration.h:40
std::conditional_t< IntrusiveLink !=nullptr, Trinity::Impl::MPSCQueueIntrusive< T, IntrusiveLink >, Trinity::Impl::MPSCQueueNonIntrusive< T > > MPSCQueue
Definition MPSCQueue.h:173
ConnectionType
Definition Opcodes.h:27
OpcodeClient
Definition Opcodes.h:38
struct z_stream_s z_stream
Definition WorldSocket.h:37
bool NeedsEncryption() const
Definition WorldSocket.h:52
EncryptablePacket(WorldPacket const &packet, bool encrypt)
Definition WorldSocket.h:47
std::atomic< EncryptablePacket * > SocketQueueLink
Definition WorldSocket.h:54
virtual SocketReadCallbackResult ReadHandler()
Definition Socket.h:262
virtual void OnClose()
Definition Socket.h:260
virtual bool Update()
Definition Socket.h:170
virtual void Start()
Definition Socket.h:152
Player session in the World.
SessionKey _sessionKey
ConnectionType _type
MessageBuffer _headerBuffer
TimePoint _lastPingTime
ConnectionType GetConnectionType() const
WorldSocket(WorldSocket &&right)=delete
bool _canRequestHotfixes
MessageBuffer _packetBuffer
QueryCallbackProcessor _queryProcessor
WorldPacketCrypt _authCrypt
Socket BaseSocket
Definition WorldSocket.h:92
WorldSocket & operator=(WorldSocket const &right)=delete
std::size_t _sendBufferSize
std::array< uint8, 32 > _serverChallenge
WorldSession * _worldSession
std::string _ipCountry
z_stream * _compressionStream
void SetSendBufferSize(std::size_t sendBufferSize)
WorldSocket & operator=(WorldSocket &&right)=delete
MPSCQueue< EncryptablePacket, &EncryptablePacket::SocketQueueLink > _bufferQueue
std::array< uint8, 32 > _encryptKey
WorldSocket(WorldSocket const &right)=delete
std::mutex _worldSessionLock
uint32 _overSpeedPings
static uint32 const MinSizeForCompression
Definition WorldSocket.h:85
SocketReadCallbackResult
Definition Socket.h:49
boost::asio::basic_stream_socket< boost::asio::ip::tcp, boost::asio::io_context::executor_type > IoContextTcpSocket
Definition Socket.h:40
uint8 Tag[12]
Definition WorldSocket.h:71
bool IsValidSize() const
Definition WorldSocket.h:73