TrinityCore
Loading...
Searching...
No Matches
WorldserverGameUtilitiesService.cpp
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
20#include "IpAddress.h"
21#include "Log.h"
22#include "MapUtils.h"
23#include "ProtobufJSON.h"
24#include "RealmList.h"
25#include "RealmList.pb.h"
26#include <zlib.h>
27
28std::unordered_map<std::string, Battlenet::Services::GameUtilitiesService::ClientRequestHandler> const Battlenet::Services::GameUtilitiesService::ClientRequestHandlers =
29{
30 { "Command_RealmListRequest_v1", &GameUtilitiesService::HandleRealmListRequest },
31 { "Command_RealmJoinRequest_v1", &GameUtilitiesService::HandleRealmJoinRequest }
32};
33
37
38uint32 Battlenet::Services::GameUtilitiesService::HandleProcessClientRequest(game_utilities::v1::ClientRequest const* request, game_utilities::v1::ClientResponse* response, std::function<void(ServiceBase*, uint32, ::google::protobuf::Message const*)>& /*continuation*/)
39{
40 Attribute const* command = nullptr;
41 std::unordered_map<std::string, Variant const*> params;
42 auto removeSuffix = [](std::string const& string) -> std::string
43 {
44 size_t pos = string.rfind('_');
45 if (pos != std::string::npos)
46 return string.substr(0, pos);
47
48 return string;
49 };
50
51 for (int32 i = 0; i < request->attribute_size(); ++i)
52 {
53 Attribute const& attr = request->attribute(i);
54 if (strstr(attr.name().c_str(), "Command_") == attr.name().c_str())
55 {
56 command = &attr;
57 params[removeSuffix(attr.name())] = &attr.value();
58 }
59 else
60 params[attr.name()] = &attr.value();
61 }
62
63 if (!command)
64 {
65 TC_LOG_ERROR("session.rpc", "{} sent ClientRequest with no command.", GetCallerInfo());
67 }
68
69 auto itr = ClientRequestHandlers.find(removeSuffix(command->name()));
70 if (itr == ClientRequestHandlers.end())
71 {
72 TC_LOG_ERROR("session.rpc", "{} sent ClientRequest with unknown command {}.", GetCallerInfo(), removeSuffix(command->name()));
74 }
75
76 return (this->*itr->second)(params, response);
77}
78
80{
81 std::string subRegionId;
82 if (Variant const* subRegion = Trinity::Containers::MapGetValuePtr(params, "Command_RealmListRequest_v1"))
83 subRegionId = subRegion->string_value();
84
85 std::vector<uint8> compressed = sRealmList->GetRealmList(_session->GetClientBuild(), _session->GetSecurity(), subRegionId);
86
87 if (compressed.empty())
89
90 Attribute* attribute = response->add_attribute();
91 attribute->set_name("Param_RealmList");
92 attribute->mutable_value()->set_blob_value(compressed.data(), compressed.size());
93
95 for (auto const& characterCount : _session->GetRealmCharacterCounts())
96 {
97 ::JSON::RealmList::RealmCharacterCountEntry* countEntry = realmCharacterCounts.add_counts();
98 countEntry->set_wowrealmaddress(characterCount.first);
99 countEntry->set_count(characterCount.second);
100 }
101
102 std::string json = "JSONRealmCharacterCountList:" + JSON::Serialize(realmCharacterCounts);
103
104 uLongf compressedLength = compressBound(json.length());
105 compressed.resize(4 + compressedLength);
106 *reinterpret_cast<uint32*>(compressed.data()) = json.length() + 1;
107
108 if (compress(compressed.data() + 4, &compressedLength, reinterpret_cast<uint8 const*>(json.c_str()), json.length() + 1) != Z_OK)
110
111 attribute = response->add_attribute();
112 attribute->set_name("Param_CharacterCountList");
113 attribute->mutable_value()->set_blob_value(compressed.data(), compressedLength + 4);
114
115 return ERROR_OK;
116}
117
119{
120 if (Variant const* realmAddress = Trinity::Containers::MapGetValuePtr(params, "Param_RealmAddress"))
121 return sRealmList->JoinRealm(uint32(realmAddress->uint_value()), _session->GetClientBuild(), _session->GetClientBuildVariant(),
122 Trinity::Net::make_address(_session->GetRemoteAddress()), _session->GetRealmListSecret(), _session->GetSessionDbcLocale(),
123 _session->GetOS(), _session->GetTimezoneOffset(), _session->GetAccountName(), _session->GetSecurity(), response);
124
126}
127
129{
130 if (request->attribute_key().find("Command_RealmListRequest_v1") == 0)
131 {
132 sRealmList->WriteSubRegions(response);
133 return ERROR_OK;
134 }
135
137}
@ ERROR_RPC_NOT_IMPLEMENTED
@ ERROR_RPC_MALFORMED_REQUEST
@ ERROR_WOW_SERVICES_INVALID_JOIN_TICKET
@ ERROR_UTIL_SERVER_FAILED_TO_SERIALIZE_RESPONSE
uint8_t uint8
Definition Define.h:156
int32_t int32
Definition Define.h:150
uint32_t uint32
Definition Define.h:154
std::unordered_set< uint32 > params[2]
#define TC_LOG_ERROR(filterType__, message__,...)
Definition Log.h:190
#define sRealmList
Definition RealmList.h:93
uint32 HandleGetAllValuesForAttribute(game_utilities::v1::GetAllValuesForAttributeRequest const *request, game_utilities::v1::GetAllValuesForAttributeResponse *response, std::function< void(ServiceBase *, uint32, ::google::protobuf::Message const *)> &continuation) override
static std::unordered_map< std::string, ClientRequestHandler > const ClientRequestHandlers
uint32 HandleRealmJoinRequest(std::unordered_map< std::string, Variant const * > const &params, game_utilities::v1::ClientResponse *response)
uint32 HandleRealmListRequest(std::unordered_map< std::string, Variant const * > const &params, game_utilities::v1::ClientResponse *response)
uint32 HandleProcessClientRequest(game_utilities::v1::ClientRequest const *request, game_utilities::v1::ClientResponse *response, std::function< void(ServiceBase *, uint32, ::google::protobuf::Message const *)> &continuation) override
void set_wowrealmaddress(::google::protobuf::uint32 value)
void set_count(::google::protobuf::uint32 value)
inline ::JSON::RealmList::RealmCharacterCountEntry * add_counts()
Player session in the World.
const ::bgs::protocol::Variant & value() const
const ::std::string & name() const
inline ::bgs::protocol::Variant * mutable_value()
void set_name(const ::std::string &value)
void set_blob_value(const ::std::string &value)
TC_SHARED_API std::string Serialize(google::protobuf::Message const &message)
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition MapUtils.h:37