TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
WorldSocket.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef __WORLDSOCKET_H__
20 #define __WORLDSOCKET_H__
21 
22 #include "Common.h"
23 #include "WorldPacketCrypt.h"
24 #include "ServerPktHeader.h"
25 #include "Socket.h"
26 #include "Util.h"
27 #include "WorldPacket.h"
28 #include "WorldSession.h"
29 #include "MPSCQueue.h"
30 #include <chrono>
31 #include <boost/asio/ip/tcp.hpp>
32 
33 using boost::asio::ip::tcp;
34 struct z_stream_s;
35 class EncryptablePacket;
36 
37 namespace WorldPackets
38 {
39  class ServerPacket;
40  namespace Auth
41  {
42  class AuthSession;
43  class AuthContinuedSession;
44  class ConnectToFailed;
45  }
46 }
47 
48 #pragma pack(push, 1)
49 
51 {
52  struct
53  {
56  } Setup;
57 
58  struct
59  {
62  } Normal;
63 
64  static bool IsValidSize(uint32 size) { return size < 10240; }
65  static bool IsValidOpcode(uint32 opcode) { return opcode < NUM_OPCODE_HANDLERS; }
66 };
67 
68 #pragma pack(pop)
69 
70 class TC_GAME_API WorldSocket : public Socket<WorldSocket>
71 {
73  static std::string const ServerConnectionInitialize;
74  static std::string const ClientConnectionInitialize;
76 
77  static uint8 const AuthCheckSeed[16];
78  static uint8 const SessionKeySeed[16];
79  static uint8 const ContinuedSessionSeed[16];
80 
82 
83 public:
84  WorldSocket(tcp::socket&& socket);
85  ~WorldSocket();
86 
87  WorldSocket(WorldSocket const& right) = delete;
88  WorldSocket& operator=(WorldSocket const& right) = delete;
89 
90  void Start() override;
91  bool Update() override;
92 
93  void SendPacket(WorldPacket const& packet);
94 
95  ConnectionType GetConnectionType() const { return _type; }
96 
97  void SendAuthResponseError(uint32 code);
98  void SetWorldSession(WorldSession* session);
99 
100 protected:
101  void OnClose() override;
102  void ReadHandler() override;
103  bool ReadHeaderHandler();
104 
106  {
107  Ok = 0,
108  Error = 1,
109  WaitingForQuery = 2
110  };
111 
112  ReadDataHandlerResult ReadDataHandler();
113 private:
114  void CheckIpCallback(PreparedQueryResult result);
115  void InitializeHandler(boost::system::error_code error, std::size_t transferedBytes);
116 
119  void LogOpcodeText(OpcodeClient opcode, std::unique_lock<std::mutex> const& guard) const;
121  void SendPacketAndLogOpcode(WorldPacket const& packet);
122  void WritePacketToBuffer(EncryptablePacket const& packet, MessageBuffer& buffer);
123  uint32 CompressPacket(uint8* buffer, WorldPacket const& packet);
124 
125  void HandleSendAuthSession();
126  void HandleAuthSession(std::shared_ptr<WorldPackets::Auth::AuthSession> authSession);
127  void HandleAuthSessionCallback(std::shared_ptr<WorldPackets::Auth::AuthSession> authSession, PreparedQueryResult result);
128  void HandleAuthContinuedSession(std::shared_ptr<WorldPackets::Auth::AuthContinuedSession> authSession);
129  void HandleAuthContinuedSessionCallback(std::shared_ptr<WorldPackets::Auth::AuthContinuedSession> authSession, PreparedQueryResult result);
130  void LoadSessionPermissionsCallback(PreparedQueryResult result);
131  void HandleConnectToFailed(WorldPackets::Auth::ConnectToFailed& connectToFailed);
132 
133  bool HandlePing(WorldPacket& recvPacket);
134 
135  void ExtractOpcodeAndSize(ClientPktHeader const* header, uint32& opcode, uint32& size) const;
136 
138 
143 
144  std::chrono::steady_clock::time_point _LastPingTime;
146 
147  std::mutex _worldSessionLock;
149  bool _authed;
150 
154 
156 
158  std::function<void(PreparedQueryResult&&)> _queryCallback;
159  std::string _ipCountry;
160 };
161 
162 #endif
Definition: Socket.h:65
Definition: BigNumber.h:28
std::chrono::steady_clock::time_point _LastPingTime
Definition: WorldSocket.h:144
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: QueryResult.h:107
uint16 Command
Definition: WorldSocket.h:55
Definition: WorldPacketCrypt.h:26
BigNumber _serverChallenge
Definition: WorldSocket.h:139
BigNumber _decryptSeed
Definition: WorldSocket.h:142
std::function< void(PreparedQueryResult &&)> _queryCallback
Definition: WorldSocket.h:158
virtual bool Update()
Definition: Socket.h:83
static std::string const ClientConnectionInitialize
Definition: WorldSocket.h:74
Definition: WorldSocket.cpp:44
WorldSession * _worldSession
Definition: WorldSocket.h:148
static bool IsValidSize(uint32 size)
Definition: WorldSocket.h:64
struct ClientPktHeader::@324 Setup
uint32 _OverSpeedPings
Definition: WorldSocket.h:145
Socket< WorldSocket > BaseSocket
Definition: WorldSocket.h:81
MessageBuffer _headerBuffer
Definition: WorldSocket.h:151
MessageBuffer _packetBuffer
Definition: WorldSocket.h:152
Definition: Opcodes.h:41
std::mutex _worldSessionLock
Definition: WorldSocket.h:147
struct ClientPktHeader::@325 Normal
Definition: AuthenticationPackets.h:203
Definition: WorldSocket.h:70
Vector2int16 & operator=(const Any &a)
uint32_t uint32
Definition: Define.h:150
uint16_t uint16
Definition: Define.h:151
Definition: WorldSocket.h:50
static bool IsValidOpcode(uint32 opcode)
Definition: WorldSocket.h:65
static std::string const ServerConnectionInitialize
Definition: WorldSocket.h:73
Definition: inftrees.h:24
Definition: BattlegroundMgr.h:57
bool _authed
Definition: WorldSocket.h:149
std::future< PreparedQueryResult > PreparedQueryResultFuture
Definition: Callback.h:26
static uint32 const MinSizeForCompression
Definition: WorldSocket.h:75
WorldPacketCrypt _authCrypt
Definition: WorldSocket.h:140
virtual void Start()=0
ConnectionType GetConnectionType() const
Definition: WorldSocket.h:95
virtual void ReadHandler()=0
#define TC_GAME_API
Definition: Define.h:134
z_stream_s * _compressionStream
Definition: WorldSocket.h:155
uint8_t uint8
Definition: Define.h:152
std::string _ipCountry
Definition: WorldSocket.h:159
OpcodeClient
Definition: Opcodes.h:46
MPSCQueue< EncryptablePacket > _bufferQueue
Definition: WorldSocket.h:153
Player session in the World.
Definition: WorldSession.h:882
uint16 Size
Definition: WorldSocket.h:54
void Error(char const *file, int line, char const *function, char const *message)
Definition: Errors.cpp:77
PreparedQueryResultFuture _queryFuture
Definition: WorldSocket.h:157
virtual void OnClose()
Definition: Socket.h:162
Definition: WorldPacket.h:26
ConnectionType _type
Definition: WorldSocket.h:137
Definition: MessageBuffer.h:24
ConnectionType
Definition: Opcodes.h:29
BigNumber _encryptSeed
Definition: WorldSocket.h:141
Definition: zlib.h:85
static uint32 const ConnectionInitializeMagic
Definition: WorldSocket.h:72
uint32 Size
Definition: WorldSocket.h:60
ReadDataHandlerResult
Definition: WorldSocket.h:105