Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@
*.dwo

.github/workflow
build/
CMakeFiles/
thirdparty/
build/
logs/

CMakeCache.txt
add-*-repo.sh
18 changes: 12 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.21)
project(GameServer)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(fmt REQUIRED)

find_package(asio::asio QUIET)
if(NOT TARGET asio::asio)
message(STATUS "ASIO not found via find_package, using manual configuration")
set(ASIO_DIR "${CMAKE_SOURCE_DIR}/thirdparty/asio")
Expand All @@ -23,21 +24,20 @@ set(GLM_DIR "${CMAKE_SOURCE_DIR}/thirdparty/glm")
set(GLM_INCLUDE_DIRS "${GLM_DIR}")
include_directories(${GLM_INCLUDE_DIRS})

find_package(nlohmann_json 3.2.0 QUIET)
find_package(nlohmann_json QUIET)
if(NOT nlohmann_json_FOUND)
message(WARNING "nlohmann_json not found, using FetchContent...")
include(FetchContent)
FetchContent_Declare(
nlohmann_json
GIT_REPOSITORY https://github.com/nlohmann/json.git
GIT_TAG v3.11.2
GIT_TAG develop
)
FetchContent_MakeAvailable(nlohmann_json)
endif()

find_package(spdlog 1.13.0 QUIET)
find_package(spdlog QUIET)
if(NOT spdlog_FOUND)
# Fall back to cloned version
message(STATUS "System spdlog not found, using local clone")
set(SPDLOG_DIR "${CMAKE_SOURCE_DIR}/thirdparty/spdlog")
add_subdirectory(${SPDLOG_DIR} EXCLUDE_FROM_ALL)
Expand Down Expand Up @@ -95,6 +95,7 @@ set(LOGGING_SOURCES

# Process system
set(PROCESS_SOURCES
src/process/IPCChannel.cpp
src/process/ProcessPool.cpp
)

Expand All @@ -112,6 +113,7 @@ set(NETWORK_SOURCES
src/network/BinarySession.cpp
src/network/ConnectionManager.cpp
src/network/ClientListener.cpp
src/network/MasterRouter.cpp
src/network/MasterServer.cpp
src/network/NetworkQualityMonitor.cpp
src/network/PredictionSystem.cpp
Expand Down Expand Up @@ -196,6 +198,10 @@ add_executable(gameserver
${DATABASE_SOURCES}
)

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(gameserver PRIVATE -fcoroutines)
endif()

if(ENABLE_ASAN)
message(STATUS "Enabling AddressSanitizer and UndefinedBehaviorSanitizer")
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
Expand Down
69 changes: 41 additions & 28 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,52 +50,31 @@ fi

cd ..

# Install system dependencies (NO Boost packages)
sudo apt-get update
sudo apt-get install -y \
build-essential \
cmake \
libpq-dev \
python3-dev \
libssl-dev \
zlib1g-dev \
postgresql \
libglm-dev \
libasio-dev \
libspdlog-dev \
nlohmann-json3-dev \
libgl1-mesa-dev \
libglu1-mesa-dev \
libx11-dev \
libxi-dev \
libxrandr-dev \
mesa-common-dev \
uuid-dev \
libcrypt-dev \
libfmt-dev

# Parse command line arguments
USE_CITUS=OFF
USE_SQLITE=OFF
ENABLE_ASAN=OFF
CLEAR_PREVIOUS=OFF
APT_UPDATE=OFF

for arg in "$@"; do
case $arg in
--with-citus)
echo "Installing Citus extension..."
sudo apt-get install -y postgresql-citus
USE_CITUS=ON
;;
--with-sqlite)
echo "Installing SQLite3 development libraries..."
sudo apt-get install -y libsqlite3-dev
USE_SQLITE=ON
;;
--with-asan)
echo "Enabling AddressSanitizer and UndefinedBehaviorSanitizer"
ENABLE_ASAN=ON
;;
--apt-update)
echo "Enabling linux apt update"
APT_UPDATE=ON
;;
--clear)
echo "Enabling clear previous compilations"
CLEAR_PREVIOUS=ON
Expand All @@ -106,7 +85,41 @@ for arg in "$@"; do
done

# Build configuration
echo "Building with Citus: $USE_CITUS, SQLite: $USE_SQLITE, ASan: $ENABLE_ASAN"
echo "Building with Citus: $USE_CITUS, SQLite: $USE_SQLITE, ASan: $ENABLE_ASAN, apt update: $APT_UPDATE"

# Install system dependencies
if [ $APT_UPDATE == ON ]; then
sudo apt update
sudo apt install -y \
build-essential \
cmake \
libpq-dev \
python3-dev \
libssl-dev \
zlib1g-dev \
postgresql \
libglm-dev \
libasio-dev \
libspdlog-dev \
nlohmann-json3-dev \
libgl1-mesa-dev \
libglu1-mesa-dev \
libx11-dev \
libxi-dev \
libxrandr-dev \
mesa-common-dev \
uuid-dev \
libcrypt-dev \
libfmt-dev
if [ $USE_CITUS == ON ]; then
sudo apt install -y postgresql-citus
fi

if [ $USE_SQLITE == ON ]; then
sudo apt install -y libsqlite3-dev
fi
fi


# Clean previous build artifacts
rm -f CMakeCache.txt Makefile cmake_install.cmake
Expand All @@ -115,7 +128,7 @@ rm -rf CMakeFiles
# Create build directory and copy related folders
mkdir -p build
cd build
if [ "$CLEAR_PREVIOUS" = true ]; then
if [ "$CLEAR_PREVIOUS" == ON ]; then
echo "Clearing previous compilations..."
find . -mindepth 1 -maxdepth 1 ! -name "certs" -exec rm -rf {} +
fi
Expand Down
11 changes: 7 additions & 4 deletions config/core.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"process": {
"workers": {
"max_message_size": 1048576,
"receive_timeout_ms": 1000,
"workers": [
"game_logic": {
"check_interval_ms": 100,
"max_restart_attempts": 5
},
"clients": [
{
"protocol": "binary",
"host": "127.0.0.1",
Expand Down Expand Up @@ -144,7 +147,7 @@
"file": "logs/server.log",
"max_file_size": 1048576,
"max_files": 10,
"pattern": "[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] [%P] [%n] %v"
"pattern": "[%Y-%m-%d %H:%M:%S.%e] [%P] [%^%l%$] [%n] %v"
},

"scripting": {
Expand Down
2 changes: 1 addition & 1 deletion include/game/GameLogic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class GameLogic : public LogicCore, public std::enable_shared_from_this<GameLogi
void SetDatabaseBackend(std::unique_ptr<DatabaseBackend> backend);
DatabaseBackend* GetDatabaseBackend() const;

private:
public:
GameLogic();
~GameLogic();

Expand Down
24 changes: 9 additions & 15 deletions include/logging/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
#include <thread>
#include <vector>

#include <asio.hpp>
#include <spdlog/spdlog.h>
#include <spdlog/fmt/fmt.h>
#include <spdlog/sinks/base_sink.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/daily_file_sink.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>

#include <asio.hpp>

#include <nlohmann/json.hpp>
//#include "config/ConfigManager.hpp"
//#include "network/BinaryProtocol.hpp"

template<typename Mutex>
class null_sink : public spdlog::sinks::base_sink<Mutex> {
Expand Down Expand Up @@ -103,34 +103,28 @@ class Logger {
static std::shared_ptr<spdlog::logger> GetLogger(const std::string& name="WorkerMain");
static void AddSink(spdlog::sink_ptr sink);

// template<typename... Args> static void Trace(const std::string& fmt, Args... args);
// template<typename... Args> static void Debug(const std::string& fmt, Args... args);
// template<typename... Args> static void Info(const std::string& fmt, Args... args);
// template<typename... Args> static void Warn(const std::string& fmt, Args... args);
// template<typename... Args> static void Error(const std::string& fmt, Args... args);
// template<typename... Args> static void Critical(const std::string& fmt, Args... args);
template<typename... Args> static void Trace(const std::string& fmt, Args... args) {
GetLogger()->trace(fmt, args...);
GetLogger()->trace(fmt::runtime(fmt), args...);
}

template<typename... Args> static void Debug(const std::string& fmt, Args... args) {
GetLogger()->debug(fmt, args...);
GetLogger()->debug(fmt::runtime(fmt), args...);
}

template<typename... Args> static void Info(const std::string& fmt, Args... args) {
GetLogger()->info(fmt, args...);
GetLogger()->info(fmt::runtime(fmt), args...);
}

template<typename... Args> static void Warn(const std::string& fmt, Args... args) {
GetLogger()->warn(fmt, args...);
GetLogger()->warn(fmt::runtime(fmt), args...);
}

template<typename... Args> static void Error(const std::string& fmt, Args... args) {
GetLogger()->error(fmt, args...);
GetLogger()->error(fmt::runtime(fmt), args...);
}

template<typename... Args> static void Critical(const std::string& fmt, Args... args) {
GetLogger()->critical(fmt, args...);
GetLogger()->critical(fmt::runtime(fmt), args...);
}

static void Flush();
Expand Down
31 changes: 10 additions & 21 deletions include/network/BinaryProtocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace BinaryProtocol {

// Message types
enum MessageType : uint16_t {
MESSAGE_TYPE_INVALID = 0,

Expand Down Expand Up @@ -101,28 +100,23 @@ namespace BinaryProtocol {
uint32_t timestamp;
uint32_t length;
uint32_t checksum;

NetworkHeader(uint16_t type = 0, uint32_t seq = 0, uint8_t ver = 1, uint8_t flgs = 0)
: version(ver), flags(flgs), message_type(type),
sequence(seq), timestamp(0), length(0), checksum(0) {}
NetworkHeader(uint16_t type = 0, uint32_t seq = 0, uint8_t ver = 1, uint8_t flgs = 0);
};

struct BinaryMessage {
NetworkHeader header;
std::vector<uint8_t> data;

std::vector<uint8_t> Serialize() const;
static BinaryMessage Deserialize(const uint8_t* buffer, size_t length);

bool IsCompressed() const { return (header.flags & FLAG_COMPRESSED) != 0; }
bool IsEncrypted() const { return (header.flags & FLAG_ENCRYPTED) != 0; }
bool IsReliable() const { return (header.flags & FLAG_RELIABLE) != 0; }
bool IsCompressed() const;
bool IsEncrypted() const;
bool IsReliable() const;
};

class BinaryWriter {
public:
BinaryWriter();

void WriteRaw(const uint8_t* data, size_t length);
void WriteUInt8(uint8_t value);
void WriteUInt16(uint16_t value);
void WriteUInt32(uint32_t value);
Expand All @@ -136,10 +130,8 @@ namespace BinaryProtocol {
void WriteVector3(const glm::vec3& vec);
void WriteQuaternion(const glm::quat& quaternion);
void WriteJson(const nlohmann::json& json);

const std::vector<uint8_t>& GetBuffer() const { return buffer_; }
size_t GetSize() const { return buffer_.size(); }

const std::vector<uint8_t>& GetBuffer() const;
size_t GetSize() const;
void Clear();

private:
Expand All @@ -149,7 +141,6 @@ namespace BinaryProtocol {
class BinaryReader {
public:
BinaryReader(const uint8_t* data, size_t length);

uint8_t ReadUInt8();
uint16_t ReadUInt16();
uint32_t ReadUInt32();
Expand All @@ -163,11 +154,9 @@ namespace BinaryProtocol {
glm::vec3 ReadVector3();
glm::quat ReadQuaternion();
nlohmann::json ReadJson();

size_t Remaining() const { return length_ - position_; }
bool CanRead(size_t size) const { return position_ + size <= length_; }

size_t GetPosition() const { return position_; }
size_t Remaining() const;
bool CanRead(size_t size) const;
size_t GetPosition() const;

private:
const uint8_t* data_;
Expand Down
2 changes: 1 addition & 1 deletion include/network/BinarySession.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class BinarySession : public IConnection, public std::enable_shared_from_this<Bi
using Pointer = std::shared_ptr<BinarySession>;

explicit BinarySession(asio::ip::tcp::socket socket,
std::shared_ptr<asio::ssl::context> ssl_context = nullptr);
std::shared_ptr<asio::ssl::context> ssl_context=nullptr, uint64_t sessionId=0);
~BinarySession();

ProtocolMode GetProtocolMode() const override;
Expand Down
Loading
Loading