Skip to content

Commit

Permalink
maint(stats_handler): refactor code (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
diamante0018 authored Aug 29, 2024
1 parent 3b11b9b commit 268e396
Show file tree
Hide file tree
Showing 24 changed files with 98 additions and 52 deletions.
11 changes: 5 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ jobs:
arch:
- x86
- x64
- arm64
steps:
- name: Check out files
uses: actions/checkout@main
Expand All @@ -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'
Expand Down Expand Up @@ -146,7 +145,7 @@ jobs:
build-macos:
name: Build macOS
runs-on: macos-14
runs-on: macos-latest
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -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 }}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion deps/libtomcrypt
Submodule libtomcrypt updated 383 files
1 change: 1 addition & 0 deletions deps/premake/libtomcrypt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ end
function libtomcrypt.project()
project "libtomcrypt"
language "C"
cdialect "C89"

libtomcrypt.includes()
libtommath.import()
Expand Down
5 changes: 1 addition & 4 deletions deps/premake/libtommath.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ end
function libtommath.project()
project "libtommath"
language "C"
cdialect "C89"

libtommath.includes()

Expand All @@ -46,10 +47,6 @@ function libtommath.project()
"_USRDLL"
}

linkoptions {
"-IGNORE:4221"
}

warnings "Off"
kind "StaticLib"
end
Expand Down
2 changes: 1 addition & 1 deletion deps/rapidjson
4 changes: 2 additions & 2 deletions src/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/network/address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/network_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class network_list

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;
}

Expand Down
11 changes: 5 additions & 6 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
}
2 changes: 1 addition & 1 deletion src/server_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/services/elimination_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/getbots_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
6 changes: 3 additions & 3 deletions src/services/getservers_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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());
}
10 changes: 5 additions & 5 deletions src/services/info_response_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/services/kill_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
14 changes: 11 additions & 3 deletions src/services/statistics_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "statistics_handler.hpp"
#include "../console.hpp"

#include <utils/env.hpp>
#include <utils/io.hpp>
#include <utils/string.hpp>

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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());
}
}

Expand Down
1 change: 1 addition & 0 deletions src/std_include.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

Expand Down
6 changes: 3 additions & 3 deletions src/utils/compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -145,15 +145,15 @@ 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;
}

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_)
Expand Down
34 changes: 34 additions & 0 deletions src/utils/env.cpp
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading

0 comments on commit 268e396

Please sign in to comment.