9 #include <qr/constants.h>
10 #include <battlefield/gameserver.h>
12 #include <qr/client.h>
14 typedef void (
QR::Client::*RequestActionFunc)(
const std::vector<unsigned char>&)
const;
16 static std::map<int, RequestActionFunc> mRequestActions =
39 this->_LogTransaction(
"-->", Util::Buffer::ToString(request));
41 int action = request[0];
44 auto it = mRequestActions.find(action);
45 if (it != mRequestActions.end())
48 RequestActionFunc func = it->second;
51 (this->*(func))(request);
55 Logger::warning(
"action \"" + std::to_string(action) +
"\" not implemented!", Server::Type::QR);
61 std::vector<unsigned char> response = {
62 RESPONSE_MAGIC_1, RESPONSE_MAGIC_2,
64 request[1], request[2], request[3], request[4],
67 this->UDPSend(response);
69 this->_LogTransaction(
"<--", Util::Buffer::ToString(response));
74 std::vector<unsigned char> response = {
75 RESPONSE_MAGIC_1, RESPONSE_MAGIC_2,
80 this->UDPSend(response);
82 this->_LogTransaction(
"<--", Util::Buffer::ToString(response));
140 std::string key, value, player, score, skill, ping, team, deaths, pid;
144 game_server.SetIp(this->GetIP());
145 game_server.SetPort(this->GetPort());
151 game_server.SetClan1Id(-1);
152 game_server.SetClan1Name(
"");
153 game_server.SetClan1Claimed(0);
154 game_server.SetClan2Id(-1);
155 game_server.SetClan2Name(
"");
156 game_server.SetClan2Claimed(0);
159 while(Util::Buffer::ReadString(request, offset, key) && key.size() > 0)
162 Util::Buffer::ReadString(request, offset, value);
164 auto it = Battlefield::GameServer::SetterMap.find(key);
165 if(it != Battlefield::GameServer::SetterMap.end())
167 (game_server.*(it->second))(value);
176 if(Util::Buffer::ReadString(request, offset, player) && player.size() > 0)
178 Util::Buffer::ReadString(request, offset, score);
179 Util::Buffer::ReadString(request, offset, skill);
180 Util::Buffer::ReadString(request, offset, ping);
181 Util::Buffer::ReadString(request, offset, team);
182 Util::Buffer::ReadString(request, offset, deaths);
183 Util::Buffer::ReadString(request, offset, pid);
186 while(Util::Buffer::ReadString(request, offset, player) && player.size() > 0)
188 Util::Buffer::ReadString(request, offset, score);
189 Util::Buffer::ReadString(request, offset, skill);
190 Util::Buffer::ReadString(request, offset, ping);
191 Util::Buffer::ReadString(request, offset, team);
192 Util::Buffer::ReadString(request, offset, deaths);
193 Util::Buffer::ReadString(request, offset, pid);
197 gsplayer.SetName(player);
198 gsplayer.SetScore(score);
199 gsplayer.SetSkill(skill);
200 gsplayer.SetPing(ping);
201 gsplayer.SetTeam(team);
202 gsplayer.SetDeaths(deaths);
203 gsplayer.SetProfileId(pid);
220 if(Util::Buffer::ReadString(request, offset, key) && key.size() > 0)
222 Util::Buffer::ReadString(request, offset, key);
226 Util::Buffer::ReadString(request, offset, team);
227 Util::Buffer::ReadString(request, offset, score);
230 game_server.SetTeam1Name(team);
231 game_server.SetTeam1Score(score);
238 Util::Buffer::ReadString(request, offset, team);
239 Util::Buffer::ReadString(request, offset, score);
242 game_server.SetTeam2Name(team);
243 game_server.SetTeam2Score(score);
253 if(game_server.GetId() == -1)
263 std::vector<unsigned char> response = {
264 RESPONSE_MAGIC_1, RESPONSE_MAGIC_2,
266 request[1], request[2], request[3], request[4],
267 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
268 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
269 0x30, 0x30, 0x30, 0x30,
273 this->UDPSend(response);
275 this->_LogTransaction(
"<--", Util::Buffer::ToString(response));
280 std::vector<unsigned char> response = {
281 RESPONSE_MAGIC_1, RESPONSE_MAGIC_2,
283 request[1], request[2], request[3], request[4],
284 0x00, 0x00, 0x00, 0x00,
287 this->UDPSend(response);
289 this->_LogTransaction(
"<--", Util::Buffer::ToString(response));
296 std::shared_lock<std::shared_mutex> guard(g_settings_mutex);
298 if ((g_logger_mode & Logger::Mode::Development) == 0)
303 bool show_console = (g_settings[
"qr"][
"show_requests"].asBool() && direction ==
"-->") ||
304 (g_settings[
"qr"][
"show_responses"].asBool() && direction ==
"<--");
306 Logger::info(this->GetAddress() +
" " + direction +
" " + response,
307 Server::Type::QR, show_console);
Represents a player in a game server.
Class representing game server information.
void AddPlayer(const GameServerPlayer &gsplayer)
Adds a player to the game server.
bool queryGameServerByIpAndPort(Battlefield::GameServer &game_server)
Queries a game server by its IP address and port.
bool insertGameServer(Battlefield::GameServer &game_server)
Inserts a game server into the database.
bool updateGameServer(const Battlefield::GameServer &game_server)
Updates a game server in the database.
struct sockaddr_in _address
Represents a client for QR protocol.
void requestAvailable(const std::vector< unsigned char > &request) const
Sends a request for available servers to the client.
~Client()
Destructor for Client.
void _LogTransaction(const std::string &direction, const std::string &response) const
Logs a transaction with direction and response.
void requestChallenge(const std::vector< unsigned char > &request) const
Sends a request for challenge to the client.
void requestKeepAlive(const std::vector< unsigned char > &request) const
Sends a keep-alive request to the client.
Client(int socket, struct sockaddr_in address)
Constructor for Client.
void requestHeartbeat(const std::vector< unsigned char > &request) const
Sends a heartbeat request to the client.
void onRequest(const std::vector< unsigned char > &request)
Event handler for incoming requests.