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
21#include "Common.h"
22#include "ServerPktHeader.h"
23#include "Socket.h"
24#include "Util.h"
25#include "WorldPacket.h"
26#include "WorldSession.h"
27#include "WorldPacketCrypt.h"
28#include "MPSCQueue.h"
29#include <boost/asio/ip/tcp.hpp>
30
31using boost::asio::ip::tcp;
33{
34public:
35 EncryptablePacket(WorldPacket const& packet, bool encrypt) : WorldPacket(packet), _encrypt(encrypt)
36 {
37 SocketQueueLink.store(nullptr, std::memory_order_relaxed);
38 }
39
40 bool NeedsEncryption() const { return _encrypt; }
41
42 std::atomic<EncryptablePacket*> SocketQueueLink;
43
44private:
46};
47
48namespace WorldPackets
49{
50 class ServerPacket;
51}
52#pragma pack(push, 1)
53
55{
58
59 bool IsValidSize() const { return size >= 4 && size < 10240; }
60 bool IsValidOpcode() const { return cmd < NUM_OPCODE_HANDLERS; }
61};
62
63#pragma pack(pop)
64
65struct AuthSession;
66
68{
69 using BaseSocket = Socket;
70
71public:
74
75 WorldSocket(WorldSocket const& right) = delete;
76 WorldSocket(WorldSocket&& right) = delete;
77 WorldSocket& operator=(WorldSocket const& right) = delete;
78 WorldSocket& operator=(WorldSocket&& right) = delete;
79
80 void Start() override;
81 bool Update() override;
82
83 void SendPacket(WorldPacket const& packet);
84
85 void SetSendBufferSize(std::size_t sendBufferSize) { _sendBufferSize = sendBufferSize; }
86
87 void OnClose() override;
89
90 void QueueQuery(QueryCallback&& queryCallback);
91
92 void SendAuthSession();
93
94protected:
95 bool ReadHeaderHandler();
96
98 {
99 Ok = 0,
100 Error = 1,
101 WaitingForQuery = 2
102 };
103
104 ReadDataHandlerResult ReadDataHandler();
105
106private:
109 void LogOpcodeText(OpcodeClient opcode, std::unique_lock<std::mutex> const& guard) const;
111 void SendPacketAndLogOpcode(WorldPacket const& packet);
112 void HandleAuthSession(WorldPacket& recvPacket);
113 void HandleAuthSessionCallback(std::shared_ptr<AuthSession> authSession, PreparedQueryResult result);
114 void LoadSessionPermissionsCallback(PreparedQueryResult result);
115 void SendAuthResponseError(uint8 code);
116
117 bool HandlePing(WorldPacket& recvPacket);
118
119 std::array<uint8, 4> _serverChallenge;
120 std::array<uint8, 32> _dosChallenge;
122
125
129
133 std::size_t _sendBufferSize;
134
136 std::string _ipCountry;
137};
138
139#endif
std::shared_ptr< PreparedResultSet > PreparedQueryResult
#define TC_GAME_API
Definition Define.h:114
uint8_t uint8
Definition Define.h:135
uint16_t uint16
Definition Define.h:134
uint32_t uint32
Definition Define.h:133
std::chrono::steady_clock::time_point TimePoint
time_point shorthand typedefs
Definition Duration.h:36
std::conditional_t< IntrusiveLink !=nullptr, Trinity::Impl::MPSCQueueIntrusive< T, IntrusiveLink >, Trinity::Impl::MPSCQueueNonIntrusive< T > > MPSCQueue
Definition MPSCQueue.h:167
bool NeedsEncryption() const
Definition WorldSocket.h:40
EncryptablePacket(WorldPacket const &packet, bool encrypt)
Definition WorldSocket.h:35
std::atomic< EncryptablePacket * > SocketQueueLink
Definition WorldSocket.h:42
virtual SocketReadCallbackResult ReadHandler()
Definition Socket.h:225
virtual void OnClose()
Definition Socket.h:223
virtual bool Update()
Definition Socket.h:137
virtual void Start()
Definition Socket.h:135
Player session in the World.
MessageBuffer _headerBuffer
WorldSocket(WorldSocket &&right)=delete
MessageBuffer _packetBuffer
QueryCallbackProcessor _queryProcessor
WorldPacketCrypt _authCrypt
std::array< uint8, 4 > _serverChallenge
Socket BaseSocket
Definition WorldSocket.h:69
WorldSocket & operator=(WorldSocket const &right)=delete
std::size_t _sendBufferSize
std::array< uint8, 32 > _dosChallenge
uint32 _OverSpeedPings
TimePoint _LastPingTime
WorldSession * _worldSession
std::string _ipCountry
void SetSendBufferSize(std::size_t sendBufferSize)
Definition WorldSocket.h:85
WorldSocket & operator=(WorldSocket &&right)=delete
MPSCQueue< EncryptablePacket, &EncryptablePacket::SocketQueueLink > _bufferQueue
WorldSocket(WorldSocket const &right)=delete
std::mutex _worldSessionLock
Opcodes
Definition Opcodes.h:29
@ NUM_OPCODE_HANDLERS
Definition Opcodes.h:1345
SocketReadCallbackResult
Definition Socket.h:44
boost::asio::basic_stream_socket< boost::asio::ip::tcp, boost::asio::io_context::executor_type > IoContextTcpSocket
Definition Socket.h:41
bool IsValidSize() const
Definition WorldSocket.h:59
bool IsValidOpcode() const
Definition WorldSocket.h:60