maint(stats_handler): refactor code (#94)

This commit is contained in:
6arelyFuture 2024-08-29 18:17:50 +02:00 committed by GitHub
parent 3b11b9bab9
commit 268e3961e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 98 additions and 52 deletions

View File

@ -85,7 +85,6 @@ jobs:
arch:
- x86
- x64
- arm64
steps:
- name: Check out files
uses: actions/checkout@main
@ -99,7 +98,7 @@ jobs:
if: matrix.arch == 'arm64'
run: |
sudo apt-get update
sudo apt-get install crossbuild-essential-arm64 -y
sudo apt-get -y install crossbuild-essential-arm64
- name: Install dependencies (x86)
if: matrix.arch == 'x86'
@ -146,7 +145,7 @@ jobs:
build-macos:
name: Build macOS
runs-on: macos-14
runs-on: macos-latest
strategy:
fail-fast: false
matrix:
@ -246,10 +245,10 @@ jobs:
shell: bash
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3.3.0
uses: docker/setup-buildx-action@v3.6.1
- name: Login to DockerHub
uses: docker/login-action@v3.2.0
uses: docker/login-action@v3.3.0
with:
username: ${{ secrets.DOCKERHUB_USER }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
@ -265,7 +264,7 @@ jobs:
- name: Build and Push Docker Image
id: build-and-push
uses: docker/build-push-action@v6.1.0
uses: docker/build-push-action@v6.7.0
with:
context: .
platforms: linux/amd64

2
deps/libtomcrypt vendored

@ -1 +1 @@
Subproject commit f7e6519fae1e11ff5ff9d36c84101a673002133b
Subproject commit c4b423f809b59b9f198bc735c3cc9d96d66059d3

View File

@ -27,6 +27,7 @@ end
function libtomcrypt.project()
project "libtomcrypt"
language "C"
cdialect "C89"
libtomcrypt.includes()
libtommath.import()

View File

@ -30,6 +30,7 @@ end
function libtommath.project()
project "libtommath"
language "C"
cdialect "C89"
libtommath.includes()
@ -46,10 +47,6 @@ function libtommath.project()
"_USRDLL"
}
linkoptions {
"-IGNORE:4221"
}
warnings "Off"
kind "StaticLib"
end

2
deps/rapidjson vendored

@ -1 +1 @@
Subproject commit ab1842a2dae061284c0a62dca1cc6d5e7e37e346
Subproject commit 7c73dd7de7c4f14379b781418c6e947ad464c818

2
deps/zlib vendored

@ -1 +1 @@
Subproject commit 0f51fb4933fc9ce18199cb2554dacea8033e7fd3
Subproject commit 545f1949635949159fa6282e81712aec32b5d4f1

View File

@ -216,9 +216,9 @@ namespace console
lock _{};
#ifdef _WIN32
SetConsoleTitleA(title.data());
SetConsoleTitleA(title.c_str());
#else
printf("\033]0;%s\007", title.data());
printf("\033]0;%s\007", title.c_str());
fflush(stdout);
#endif
}

View File

@ -121,7 +121,7 @@ namespace network
if (pos != std::string::npos)
{
auto port = addr.substr(pos + 1);
this->set_port(uint16_t(atoi(port.data())));
this->set_port(static_cast<uint16_t>(atoi(port.c_str())));
addr = addr.substr(0, pos);
}
@ -132,7 +132,7 @@ namespace network
void address::resolve(const std::string& hostname)
{
addrinfo* result = nullptr;
if (!getaddrinfo(hostname.data(), nullptr, nullptr, &result))
if (!getaddrinfo(hostname.c_str(), nullptr, nullptr, &result))
{
for (auto* i = result; i; i = i->ai_next)
{

View File

@ -133,7 +133,7 @@ protected:
if (!is_insertion_allowed(list, address))
{
console::log("Insertion rejected for target:\t%s", address.to_string().data());
console::log("Insertion rejected for target:\t%s", address.to_string().c_str());
return;
}

View File

@ -72,19 +72,19 @@ void server::handle_command(const network::address& target, const std::string_vi
const auto handler = this->command_services_.find(std::string{command});
if (handler == this->command_services_.end())
{
console::warn("Unhandled command (%s): %.*s", target.to_string().data(), command.size(), command.data());
console::warn("Unhandled command (%s): %.*s", target.to_string().c_str(), command.size(), command.data());
return;
}
std::string ban_reason;
if (this->get_service<kill_list>()->contains(target, ban_reason))
{
console::log("Refused command from server %s - target is on the kill list (%s)", target.to_string().data(), ban_reason.data());
console::log("Refused command from server %s - target is on the kill list (%s)", target.to_string().c_str(), ban_reason.c_str());
return;
}
#ifdef DEBUG
console::log("Handling command (%s): %.*s - %.*s", target.to_string().data(), command.size(), command.data(), data.size(), data.data());
console::log("Handling command (%s): %.*s - %.*s", target.to_string().c_str(), command.size(), command.data(), data.size(), data.data());
#endif
try
@ -93,11 +93,10 @@ void server::handle_command(const network::address& target, const std::string_vi
}
catch (const service::execution_exception& e)
{
console::warn("Exception in command %.*s (%s): %s", command.size(), command.data(), target.to_string().data(),
e.what());
console::warn("Exception in command %.*s (%s): %s", command.size(), command.data(), target.to_string().c_str(), e.what());
}
catch (const std::exception& e)
{
console::error("Fatal exception in command %.*s (%s): %s", command.size(), command.data(), target.to_string().data(), e.what());
console::error("Fatal exception in command %.*s (%s): %s", command.size(), command.data(), target.to_string().c_str(), e.what());
}
}

View File

@ -90,7 +90,7 @@ bool server_base::receive_data()
if (!is_command(data))
{
console::warn("Received invalid data from: %s", address.to_string().data());
console::warn("Received invalid data from: %s", address.to_string().c_str());
return false;
}

View File

@ -41,7 +41,7 @@ void elimination_handler::run_frame()
if (server.game == game_type::t7 && server.protocol < T7_PROTOCOL)
{
#ifdef _DEBUG
console::log("Removing T7 server '%s' because they are using an outdated protocol (%i)", context.get_address().to_string().data(), server.protocol);
console::log("Removing T7 server '%s' because they are using an outdated protocol (%i)", context.get_address().to_string().c_str(), server.protocol);
#endif
context.remove();
return;
@ -50,7 +50,7 @@ void elimination_handler::run_frame()
++server_count[server.game][context.get_address().to_string(false)];
if (server_count[server.game][context.get_address().to_string(false)] > MAX_SERVERS_PER_GAME)
{
console::log("Removing server '%s' because it exceeds MAX_SERVERS_PER_GAME", context.get_address().to_string().data());
console::log("Removing server '%s' because it exceeds MAX_SERVERS_PER_GAME", context.get_address().to_string().c_str());
context.remove();
return;
}
@ -60,7 +60,7 @@ void elimination_handler::run_frame()
{
if (const auto pos = name.find(entry); pos != std::string::npos)
{
console::log("Removing server '%s' (%s) because it contains a bad name", server.name.data(), context.get_address().to_string().data());
console::log("Removing server '%s' (%s) because it contains a bad name", server.name.c_str(), context.get_address().to_string().c_str());
context.remove();
return;
}

View File

@ -154,5 +154,5 @@ void getbots_command::handle_command(const network::address& target, const std::
}
this->get_server().send(target, "getbotsResponse", stream.str());
console::log("Sent bot names: %s", target.to_string().data());
console::log("Sent bot names to: %s", target.to_string().c_str());
}

View File

@ -36,9 +36,9 @@ void getservers_command::handle_command(const network::address& target, const st
const auto& game = params[0];
const auto* p = params[1].data();
const auto* p = params[1].c_str();
char* end;
const auto protocol = strtol(params[1].data(), &end, 10);
const auto protocol = strtol(params[1].c_str(), &end, 10);
if (p == end)
{
throw execution_exception("Invalid protocol");
@ -92,5 +92,5 @@ void getservers_command::handle_command(const network::address& target, const st
}
}
console::log("Sent %zu servers in %zu parts for game %s:\t%s", prepared_servers.size(), packet_count, game.data(), target.to_string().data());
console::log("Sent %zu servers in %zu parts for game %s:\t%s", prepared_servers.size(), packet_count, game.c_str(), target.to_string().c_str());
}

View File

@ -35,22 +35,22 @@ void info_response_command::handle_command(const network::address& target, const
throw execution_exception{"Invalid challenge"};
}
const auto player_count = atoi(info.get("clients").data());
const auto bot_count = atoi(info.get("bots").data());
const auto player_count = atoi(info.get("clients").c_str());
const auto bot_count = atoi(info.get("bots").c_str());
auto real_player_count = player_count - bot_count;
real_player_count = std::clamp(real_player_count, 0, 18);
server.registered = true;
server.game = game_type;
server.state = game_server::state::can_ping;
server.protocol = atoi(info.get("protocol").data());
server.protocol = atoi(info.get("protocol").c_str());
server.clients = static_cast<unsigned int>(real_player_count);
server.name = info.get("hostname");
server.heartbeat = std::chrono::high_resolution_clock::now();
server.info_string = std::move(info);
console::log("Server registered for game %s (%i):\t%s\t- %s", game.data(), server.protocol,
address.to_string().data(), server.name.data());
console::log("Server registered for game %s (%i):\t%s\t- %s", game.c_str(), server.protocol,
address.to_string().c_str(), server.name.c_str());
});
if (!found)

View File

@ -33,7 +33,7 @@ void kill_list::add_to_kill_list(const kill_list_entry& add)
const auto existing_entry = entries.find(add.ip_address_);
if (existing_entry == entries.end() || existing_entry->second.reason_ != add.reason_)
{
console::info("Added %s to kill list (reason: %s)", add.ip_address_.data(), add.reason_.data());
console::info("Added %s to kill list (reason: %s)", add.ip_address_.c_str(), add.reason_.c_str());
entries[add.ip_address_] = add;
return true;
}
@ -43,7 +43,7 @@ void kill_list::add_to_kill_list(const kill_list_entry& add)
if (!any_change)
{
console::info("%s already in kill list, doing nothing", add.ip_address_.data());
console::info("%s already in kill list, doing nothing", add.ip_address_.c_str());
return;
}
@ -61,7 +61,7 @@ void kill_list::remove_from_kill_list(const std::string& remove)
{
if (entries.erase(remove))
{
console::info("Removed %s from kill list", remove.data());
console::info("Removed %s from kill list", remove.c_str());
return true;
}
@ -70,7 +70,7 @@ void kill_list::remove_from_kill_list(const std::string& remove)
if (!any_change)
{
console::info("%s not in kill list, doing nothing", remove.data());
console::info("%s not in kill list, doing nothing", remove.c_str());
return;
}

View File

@ -2,6 +2,7 @@
#include "statistics_handler.hpp"
#include "../console.hpp"
#include <utils/env.hpp>
#include <utils/io.hpp>
#include <utils/string.hpp>
@ -37,7 +38,14 @@ namespace
root.Accept(root_writer);
std::string root_data(root_buffer.GetString(), root_buffer.GetLength());
utils::io::write_file("/var/www/server.alterware.dev/html/stats.json", root_data);
const auto location = utils::env::get_value("AW_STATS_LOCATION");
if (location.empty())
{
console::error("The environment variable 'AW_STATS_LOCATION' is not set. Please set 'AW_STATS_LOCATION' to specify the save location for 'stats.json'\n");
return;
}
utils::io::write_file(location, root_data);
}
}
@ -73,12 +81,12 @@ void statistics_handler::run_frame()
for (const auto& game_servers : servers)
{
console::log("%s (%d):", resolve_game_type_name(game_servers.first).data(),
console::log("%s (%d):", resolve_game_type_name(game_servers.first).c_str(),
static_cast<uint32_t>(game_servers.second.size()));
for (const auto& server : game_servers.second)
{
console::log("\t%s\t%s", server.second.to_string().data(), server.first.data());
console::log("\t%s\t%s", server.second.to_string().c_str(), server.first.c_str());
}
}

View File

@ -36,6 +36,7 @@
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

View File

@ -119,7 +119,7 @@ namespace utils::compression
bool add_file(zipFile& zip_file, const std::string& filename, const std::string& data)
{
const auto zip_64 = data.size() > 0xffffffff ? 1 : 0;
if (ZIP_OK != zipOpenNewFileInZip64(zip_file, filename.data(), nullptr, nullptr, 0, nullptr, 0, nullptr,
if (ZIP_OK != zipOpenNewFileInZip64(zip_file, filename.c_str(), nullptr, nullptr, 0, nullptr, 0, nullptr,
Z_DEFLATED, Z_BEST_COMPRESSION, zip_64))
{
return false;
@ -145,7 +145,7 @@ namespace utils::compression
io::write_file(filename, {});
io::remove_file(filename);
auto* zip_file = zipOpen64(filename.data(), 0);
auto* zip_file = zipOpen64(filename.c_str(), 0);
if (!zip_file)
{
return false;
@ -153,7 +153,7 @@ namespace utils::compression
const auto _ = gsl::finally([&zip_file, &comment]()
{
zipClose(zip_file, comment.empty() ? nullptr : comment.data());
zipClose(zip_file, comment.empty() ? nullptr : comment.c_str());
});
for (const auto& file : this->files_)

34
src/utils/env.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <std_include.hpp>
#include "env.hpp"
namespace utils::env
{
std::string get_value(const std::string& name)
{
const char* var_value = nullptr;
#ifdef _WIN32
char* buffer = nullptr;
size_t len = 0;
if (_dupenv_s(&buffer, &len, name.c_str()) == 0 && buffer != nullptr)
{
var_value = buffer;
}
const auto _ = gsl::finally([&]
{
std::free(buffer);
});
if (var_value)
{
return var_value;
}
return {};
#else
var_value = std::getenv(name.c_str());
return var_value ? std::string{ var_value } : std::string{};
#endif
}
}

7
src/utils/env.hpp Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <string>
namespace utils::env
{
std::string get_value(const std::string& name);
}

View File

@ -56,10 +56,10 @@ namespace utils
std::string info_string;
for (const auto& [key, val] : this->key_value_pairs_)
{
info_string.append("\\");
info_string.push_back('\\');
info_string.append(key);
info_string.append("\\");
info_string.push_back('\\');
info_string.append(val);
}

View File

@ -8,12 +8,12 @@ namespace utils::io
{
bool remove_file(const std::string& file)
{
return remove(file.data()) == 0;
return remove(file.c_str()) == 0;
}
bool move_file(const std::string& src, const std::string& target)
{
return rename(src.data(), target.data()) == 0;
return rename(src.c_str(), target.c_str()) == 0;
}
bool file_exists(const std::string& file)

View File

@ -71,7 +71,7 @@ namespace utils
char* memory::duplicate_string(const std::string& string)
{
const auto new_string = allocate_array<char>(string.size() + 1);
std::memcpy(new_string, string.data(), string.size());
std::memcpy(new_string, string.c_str(), string.size());
return new_string;
}