TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Session.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
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 Session_h__
19 #define Session_h__
20 
21 #include "Realm.h"
22 #include "SslContext.h"
23 #include "SslSocket.h"
24 #include "Socket.h"
25 #include "BigNumber.h"
26 #include "Callback.h"
27 #include <boost/asio/ip/tcp.hpp>
28 #include <boost/asio/ssl.hpp>
30 #include <memory>
31 
32 using boost::asio::ip::tcp;
33 
34 namespace pb = google::protobuf;
35 
36 namespace bgs
37 {
38  namespace protocol
39  {
40  class Variant;
41 
42  namespace account
43  {
44  namespace v1
45  {
50  }
51  }
52 
53  namespace authentication
54  {
55  namespace v1
56  {
57  class LogonRequest;
59  }
60  }
61 
62  namespace game_utilities
63  {
64  namespace v1
65  {
66  class ClientRequest;
67  class ClientResponse;
70  }
71  }
72  }
73 }
74 
75 using namespace bgs::protocol;
76 
77 namespace Battlenet
78 {
79  class Session : public Socket<Session, SslSocket<SslContext>>
80  {
82 
83  public:
85  {
87  std::string CharacterName;
90  };
91 
93  {
94  void LoadResult(Field* fields);
95 
97  std::string Name;
98  std::string DisplayName;
99  bool IsBanned;
102 
103  std::unordered_map<uint32 /*realmAddress*/, uint8> CharacterCounts;
104  std::unordered_map<std::string /*subRegion*/, LastPlayedCharacterInfo> LastPlayedCharacters;
105  };
106 
107  struct AccountInfo
108  {
109  void LoadResult(PreparedQueryResult result);
110 
112  std::string Login;
114  std::string LockCountry;
115  std::string LastIP;
117  bool IsBanned;
119 
120  std::unordered_map<uint32, GameAccountInfo> GameAccounts;
121  };
122 
123  explicit Session(tcp::socket&& socket);
124  ~Session();
125 
126  void Start() override;
127  bool Update() override;
128 
129  uint32 GetAccountId() const { return _accountInfo->Id; }
130  uint32 GetGameAccountId() const { return _gameAccountInfo->Id; }
131 
132  void SendResponse(uint32 token, pb::Message const* response);
133  void SendResponse(uint32 token, uint32 status);
134 
135  void SendRequest(uint32 serviceHash, uint32 methodId, pb::Message const* request, std::function<void(MessageBuffer)> callback)
136  {
137  _responseCallbacks[_requestToken] = std::move(callback);
138  SendRequest(serviceHash, methodId, request);
139  }
140 
141  void SendRequest(uint32 serviceHash, uint32 methodId, pb::Message const* request);
142 
143  uint32 HandleLogon(authentication::v1::LogonRequest const* logonRequest);
144  uint32 HandleVerifyWebCredentials(authentication::v1::VerifyWebCredentialsRequest const* verifyWebCredentialsRequest);
145  uint32 HandleGetAccountState(account::v1::GetAccountStateRequest const* request, account::v1::GetAccountStateResponse* response);
146  uint32 HandleGetGameAccountState(account::v1::GetGameAccountStateRequest const* request, account::v1::GetGameAccountStateResponse* response);
147  uint32 HandleProcessClientRequest(game_utilities::v1::ClientRequest const* request, game_utilities::v1::ClientResponse* response);
149 
150  std::string GetClientInfo() const;
151 
152  protected:
153  void HandshakeHandler(boost::system::error_code const& error);
154  void ReadHandler() override;
155  bool ReadHeaderLengthHandler();
156  bool ReadHeaderHandler();
157  bool ReadDataHandler();
158 
159  private:
160  void AsyncWrite(MessageBuffer* packet);
161 
162  void AsyncHandshake();
163 
164  void CheckIpCallback(PreparedQueryResult result);
165 
166  typedef uint32(Session::*ClientRequestHandler)(std::unordered_map<std::string, Variant const*> const&, game_utilities::v1::ClientResponse*);
167  static std::unordered_map<std::string, ClientRequestHandler> const ClientRequestHandlers;
168 
169  uint32 GetRealmListTicket(std::unordered_map<std::string, Variant const*> const& params, game_utilities::v1::ClientResponse* response);
170  uint32 GetLastCharPlayed(std::unordered_map<std::string, Variant const*> const& params, game_utilities::v1::ClientResponse* response);
171  uint32 GetRealmList(std::unordered_map<std::string, Variant const*> const& params, game_utilities::v1::ClientResponse* response);
172  uint32 JoinRealm(std::unordered_map<std::string, Variant const*> const& params, game_utilities::v1::ClientResponse* response);
173 
177 
178  std::unique_ptr<AccountInfo> _accountInfo;
179  GameAccountInfo* _gameAccountInfo; // Points at selected game account (inside _gameAccounts)
180 
181  std::string _locale;
182  std::string _os;
184 
185  std::string _ipCountry;
186 
187  std::array<uint8, 32> _clientSecret;
188 
189  bool _authed;
190 
192  std::function<void(PreparedQueryResult)> _queryCallback;
193 
194  std::unordered_map<uint32, std::function<void(MessageBuffer)>> _responseCallbacks;
196  };
197 }
198 
199 #endif // Session_h__
Definition: Socket.h:65
Definition: attribute_types.pb.h:67
std::shared_ptr< PreparedResultSet > PreparedQueryResult
Definition: QueryResult.h:107
Definition: game_utilities_service.pb.h:1143
std::unordered_map< uint32, GameAccountInfo > GameAccounts
Definition: Session.h:120
Definition: Session.h:36
GameAccountInfo * _gameAccountInfo
Definition: Session.h:179
std::array< uint8, 32 > _clientSecret
Definition: Session.h:187
uint32 GetAccountId() const
Definition: Session.h:129
Class used to access individual fields of database query result.
Definition: Field.h:56
Definition: method_options.pb.h:28
std::unordered_map< std::string, LastPlayedCharacterInfo > LastPlayedCharacters
Definition: Session.h:104
Definition: account_service.pb.h:1680
bool _authed
Definition: Session.h:189
std::function< void(PreparedQueryResult)> _queryCallback
Definition: Session.h:192
Definition: BnetFileGenerator.h:49
std::string LastIP
Definition: Session.h:115
Battlenet::RealmHandle RealmId
Definition: Session.h:86
uint32 _requestToken
Definition: Session.h:195
std::string Name
Definition: Session.h:97
Definition: game_utilities_service.pb.h:64
bool IsBanned
Definition: Session.h:99
uint32 GetGameAccountId() const
Definition: Session.h:130
Definition: message.h:165
Definition: Session.h:38
Definition: Session.h:92
std::string CharacterName
Definition: Session.h:87
std::string LockCountry
Definition: Session.h:114
Definition: account_service.pb.h:1462
static std::unordered_map< std::string, ClientRequestHandler > const ClientRequestHandlers
Definition: Session.h:167
std::unordered_map< uint32, uint8 > CharacterCounts
Definition: Session.h:103
std::string DisplayName
Definition: Session.h:98
AccountTypes SecurityLevel
Definition: Session.h:101
std::string Login
Definition: Session.h:112
std::string _locale
Definition: Session.h:181
uint32 FailedLogins
Definition: Session.h:116
uint64 CharacterGUID
Definition: Session.h:88
std::unordered_map< uint32, std::function< void(MessageBuffer)> > _responseCallbacks
Definition: Session.h:194
MessageBuffer _headerBuffer
Definition: Session.h:175
uint32_t uint32
Definition: Define.h:150
AccountTypes
Definition: Common.h:106
bool IsBanned
Definition: Session.h:117
uint64_t uint64
Definition: Define.h:149
Definition: account_service.pb.h:1797
std::unique_ptr< AccountInfo > _accountInfo
Definition: Session.h:178
bool IsPermanenetlyBanned
Definition: Session.h:118
void SendRequest(uint32 serviceHash, uint32 methodId, pb::Message const *request, std::function< void(MessageBuffer)> callback)
Definition: Session.h:135
Definition: game_utilities_service.pb.h:204
std::future< PreparedQueryResult > PreparedQueryResultFuture
Definition: Callback.h:26
uint32 LastPlayedTime
Definition: Session.h:89
Definition: Session.h:79
uint32 Id
Definition: Session.h:96
Definition: Realm.h:44
MessageBuffer _headerLengthBuffer
Definition: Session.h:174
uint8_t uint8
Definition: Define.h:152
std::string _ipCountry
Definition: Session.h:185
bool IsLockedToIP
Definition: Session.h:113
Definition: Session.h:107
Socket< Session, SslSocket< SslContext > > BattlenetSocket
Definition: Session.h:81
uint32_t uint32
Definition: g3dmath.h:168
std::string _os
Definition: Session.h:182
uint32 _build
Definition: Session.h:183
std::set< uint32 > params[2]
Definition: DisableMgr.cpp:45
Definition: account_service.pb.h:1587
bool IsPermanenetlyBanned
Definition: Session.h:100
MessageBuffer _packetBuffer
Definition: Session.h:176
Definition: MessageBuffer.h:24
void Update(uint32 diff)
Definition: WeatherMgr.cpp:150
uint32 Id
Definition: Session.h:111
Definition: authentication_service.pb.h:349
PreparedQueryResultFuture _queryFuture
Definition: Session.h:191
Definition: authentication_service.pb.h:2087