TrinityCore
Loading...
Searching...
No Matches
Session.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_SESSION_H
19#define TRINITYCORE_SESSION_H
20
22#include "ClientBuildInfo.h"
23#include "DatabaseEnvFwd.h"
24#include "Duration.h"
25#include "Realm.h"
26#include "Socket.h"
27#include "SslStream.h"
28#include <boost/asio/ip/tcp.hpp>
29#include <google/protobuf/message.h>
30#include <memory>
31
32namespace pb = google::protobuf;
33
34class ServiceBase;
35
37{
38class Variant;
39
40namespace account::v1
41{
42 class GetAccountStateRequest;
43 class GetAccountStateResponse;
44 class GetGameAccountStateRequest;
45 class GetGameAccountStateResponse;
46}
47
48namespace authentication::v1
49{
50 class GenerateWebCredentialsRequest;
51 class LogonRequest;
52 class VerifyWebCredentialsRequest;
53}
54
55namespace game_utilities::v1
56{
57 class ClientRequest;
58 class ClientResponse;
59 class GetAllValuesForAttributeRequest;
60 class GetAllValuesForAttributeResponse;
61}
62}
63
64using namespace bgs::protocol;
65
66namespace Battlenet
67{
68 class Session final : public std::enable_shared_from_this<Session>
69 {
70 public:
78
80 {
81 void LoadResult(Field const* fields);
82
84 std::string Name;
85 std::string DisplayName;
90
91 std::unordered_map<uint32 /*realmAddress*/, uint8> CharacterCounts;
92 std::unordered_map<std::string /*subRegion*/, LastPlayedCharacterInfo> LastPlayedCharacters;
93 };
94
96 {
98
100 std::string Login;
102 std::string LockCountry;
103 std::string LastIP;
107
108 std::unordered_map<uint32, GameAccountInfo> GameAccounts;
109 };
110
111 explicit Session(Trinity::Net::IoContextTcpSocket&& socket);
113
114 void Start();
115 bool Update();
116 boost::asio::ip::address const& GetRemoteIpAddress() const { return _socket->GetRemoteIpAddress(); }
117 bool IsOpen() const { return _socket->IsOpen(); }
118 void CloseSocket() { return _socket->CloseSocket(); }
119 void DelayedCloseSocket() { return _socket->DelayedCloseSocket(); }
120
121 uint32 GetAccountId() const { return _accountInfo->Id; }
123
124 void SendResponse(uint32 token, pb::Message const* response);
125 void SendResponse(uint32 token, uint32 status);
126
127 void SendRequest(uint32 serviceHash, uint32 methodId, pb::Message const* request, std::function<void(MessageBuffer)> callback)
128 {
129 _responseCallbacks[_requestToken] = std::move(callback);
130 SendRequest(serviceHash, methodId, request);
131 }
132
133 void SendRequest(uint32 serviceHash, uint32 methodId, pb::Message const* request);
134
135 void QueueQuery(QueryCallback&& queryCallback);
136
137 uint32 HandleLogon(authentication::v1::LogonRequest const* logonRequest, std::function<void(ServiceBase*, uint32, ::google::protobuf::Message const*)>& continuation);
138 uint32 HandleVerifyWebCredentials(authentication::v1::VerifyWebCredentialsRequest const* verifyWebCredentialsRequest, std::function<void(ServiceBase*, uint32, ::google::protobuf::Message const*)>& continuation);
139 uint32 HandleGenerateWebCredentials(authentication::v1::GenerateWebCredentialsRequest const* request, std::function<void(ServiceBase*, uint32, google::protobuf::Message const*)>& continuation);
144
145 std::string GetClientInfo() const;
146
148
149 protected:
151 bool ReadHeaderHandler();
152 bool ReadDataHandler();
153
154 private:
155 void AsyncWrite(MessageBuffer* packet);
156
157 uint32 VerifyWebCredentials(std::string const& webCredentials, std::function<void(ServiceBase*, uint32, ::google::protobuf::Message const*)>& continuation);
158
159 typedef uint32(Session::*ClientRequestHandler)(std::unordered_map<std::string, Variant const*> const&, game_utilities::v1::ClientResponse*);
160 static std::unordered_map<std::string, ClientRequestHandler> const ClientRequestHandlers;
161
162 uint32 GetRealmListTicket(std::unordered_map<std::string, Variant const*> const& params, game_utilities::v1::ClientResponse* response);
163 uint32 GetLastCharPlayed(std::unordered_map<std::string, Variant const*> const& params, game_utilities::v1::ClientResponse* response);
164 uint32 GetRealmList(std::unordered_map<std::string, Variant const*> const& params, game_utilities::v1::ClientResponse* response);
165 uint32 JoinRealm(std::unordered_map<std::string, Variant const*> const& params, game_utilities::v1::ClientResponse* response);
166
168
169 static std::shared_ptr<Socket> CreateSocket(Trinity::Net::IoContextTcpSocket&& socket);
170 std::shared_ptr<Socket> _socket;
174
175 std::shared_ptr<AccountInfo> _accountInfo;
176 GameAccountInfo* _gameAccountInfo; // Points at selected game account (inside _gameAccounts)
177
178 std::string _locale;
179 std::string _os;
183
184 std::string _ipCountry;
185
186 std::array<uint8, 32> _clientSecret;
187
189
191
192 std::unordered_map<uint32, std::function<void(MessageBuffer)>> _responseCallbacks;
194 };
195}
196
197#endif // TRINITYCORE_SESSION_H
AccountTypes
Definition Common.h:42
std::shared_ptr< PreparedResultSet > PreparedQueryResult
uint8_t uint8
Definition Define.h:156
uint64_t uint64
Definition Define.h:153
uint32_t uint32
Definition Define.h:154
std::unordered_set< uint32 > params[2]
std::chrono::minutes Minutes
Minutes shorthand typedef.
Definition Duration.h:32
uint32 GetGameAccountId() const
Definition Session.h:122
uint32 VerifyWebCredentials(std::string const &webCredentials, std::function< void(ServiceBase *, uint32, ::google::protobuf::Message const *)> &continuation)
Definition Session.cpp:300
uint32 JoinRealm(std::unordered_map< std::string, Variant const * > const &params, game_utilities::v1::ClientResponse *response)
Definition Session.cpp:686
std::string GetClientInfo() const
Definition Session.cpp:806
ClientBuild::VariantId _clientInfo
Definition Session.h:181
void DelayedCloseSocket()
Definition Session.h:119
uint32 HandleGenerateWebCredentials(authentication::v1::GenerateWebCredentialsRequest const *request, std::function< void(ServiceBase *, uint32, google::protobuf::Message const *)> &continuation)
Definition Session.cpp:264
static std::unordered_map< std::string, ClientRequestHandler > const ClientRequestHandlers
Definition Session.h:501
void SendResponse(uint32 token, pb::Message const *response)
Definition Session.cpp:131
uint32 GetAccountId() const
Definition Session.h:121
Trinity::Net::SocketReadCallbackResult ReadHandler()
Definition Session.cpp:742
uint32 GetRealmList(std::unordered_map< std::string, Variant const * > const &params, game_utilities::v1::ClientResponse *response)
Definition Session.cpp:645
MessageBuffer _headerBuffer
Definition Session.h:172
uint32 HandleProcessClientRequest(game_utilities::v1::ClientRequest const *request, game_utilities::v1::ClientResponse *response)
Definition Session.cpp:509
uint32 HandleVerifyWebCredentials(authentication::v1::VerifyWebCredentialsRequest const *verifyWebCredentialsRequest, std::function< void(ServiceBase *, uint32, ::google::protobuf::Message const *)> &continuation)
Definition Session.cpp:256
uint32(Session::* ClientRequestHandler)(std::unordered_map< std::string, Variant const * > const &, game_utilities::v1::ClientResponse *)
Definition Session.h:159
std::string _ipCountry
Definition Session.h:184
uint32 GetLastCharPlayed(std::unordered_map< std::string, Variant const * > const &params, game_utilities::v1::ClientResponse *response)
Definition Session.cpp:610
static std::shared_ptr< Socket > CreateSocket(Trinity::Net::IoContextTcpSocket &&socket)
Definition Session.cpp:93
uint32 HandleGetGameAccountState(account::v1::GetGameAccountStateRequest const *request, account::v1::GetGameAccountStateResponse *response)
Definition Session.cpp:467
bool ReadHeaderLengthHandler()
Definition Session.cpp:763
void AsyncWrite(MessageBuffer *packet)
Definition Session.cpp:123
MessageBuffer _packetBuffer
Definition Session.h:173
uint32 HandleGetAllValuesForAttribute(game_utilities::v1::GetAllValuesForAttributeRequest const *request, game_utilities::v1::GetAllValuesForAttributeResponse *response)
Definition Session.cpp:695
QueryCallbackProcessor _queryProcessor
Definition Session.h:190
std::array< uint8, 32 > _clientSecret
Definition Session.h:186
bool IsOpen() const
Definition Session.h:117
GameAccountInfo * _gameAccountInfo
Definition Session.h:176
uint32 HandleLogon(authentication::v1::LogonRequest const *logonRequest, std::function< void(ServiceBase *, uint32, ::google::protobuf::Message const *)> &continuation)
Definition Session.cpp:201
void QueueQuery(QueryCallback &&queryCallback)
Definition Session.cpp:196
void SendRequest(uint32 serviceHash, uint32 methodId, pb::Message const *request, std::function< void(MessageBuffer)> callback)
Definition Session.h:127
uint32 _requestToken
Definition Session.h:193
std::string _locale
Definition Session.h:178
std::shared_ptr< Socket > _socket
Definition Session.h:170
boost::asio::ip::address const & GetRemoteIpAddress() const
Definition Session.h:116
std::string _os
Definition Session.h:179
Minutes _timezoneOffset
Definition Session.h:182
MessageBuffer _headerLengthBuffer
Definition Session.h:171
uint32 HandleGetAccountState(account::v1::GetAccountStateRequest const *request, account::v1::GetAccountStateResponse *response)
Definition Session.cpp:450
bool ReadHeaderHandler()
Definition Session.cpp:771
std::unordered_map< uint32, std::function< void(MessageBuffer)> > _responseCallbacks
Definition Session.h:192
uint32 GetRealmListTicket(std::unordered_map< std::string, Variant const * > const &params, game_utilities::v1::ClientResponse *response)
Definition Session.cpp:553
std::shared_ptr< AccountInfo > _accountInfo
Definition Session.h:175
Class used to access individual fields of database query result.
Definition Field.h:94
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
STL namespace.
std::unordered_map< uint32, GameAccountInfo > GameAccounts
Definition Session.h:108
void LoadResult(PreparedQueryResult result)
Definition Session.cpp:45
void LoadResult(Field const *fields)
Definition Session.cpp:67
std::unordered_map< uint32, uint8 > CharacterCounts
Definition Session.h:91
std::unordered_map< std::string, LastPlayedCharacterInfo > LastPlayedCharacters
Definition Session.h:92