generated from AfonsoCMSousa/CPP-Template
Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0bb2efb088 | |||
| 29a92e8974 | |||
| fe193cd648 | |||
| f2e97f1214 | |||
| 4f37834e20 | |||
|
|
c40075ca94 | ||
|
|
ef73f16fea | ||
|
|
ef0da72123 | ||
|
|
7072f17842 | ||
|
|
e5d09e9741 | ||
|
|
8e0b1ff50e | ||
|
|
bd32f95cd1 | ||
|
|
8bc4312c9a | ||
|
|
7a648f8b21 | ||
|
|
6e4ed57e8a | ||
|
|
2f4f46a77b | ||
|
|
c67c6d56f7 | ||
|
|
ff07d6dcdf | ||
|
|
ba606c8619 | ||
|
|
aa7b7d87b4 | ||
|
|
48156350c9 | ||
|
|
7aa98209fb | ||
|
|
dc071a2fc9 | ||
|
|
5e7aa014fc | ||
|
|
ecc303ca2a | ||
|
|
159a93317e | ||
|
|
9da9cd3c13 | ||
|
|
0bdcbb9a9c | ||
|
|
4901c4b523 | ||
|
|
43827f8f12 | ||
| b2f283fff2 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
|
||||
*.a
|
||||
*.log
|
||||
*.so
|
||||
@ -8,3 +9,6 @@
|
||||
/.DS_store
|
||||
/.env
|
||||
/imgui.ini
|
||||
|
||||
PlayerTracker
|
||||
compile_commands.json
|
||||
|
||||
165
CMakeLists.txt
165
CMakeLists.txt
@ -1,29 +1,57 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
project(CPP_TEMPLATE VERSION 0.1.0 LANGUAGES C CXX)
|
||||
|
||||
# Set C++ standard
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
# Set C standard
|
||||
set(CMAKE_C_STANDARD 17)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_C_EXTENSIONS OFF)
|
||||
|
||||
# Export compile commands for IDE support (clangd, etc.)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
# Create symlink to compile_commands.json in project root for LSP
|
||||
if(CMAKE_EXPORT_COMPILE_COMMANDS)
|
||||
add_custom_target(symlink_compile_commands ALL
|
||||
COMMAND ${CMAKE_COMMAND} -E create_symlink
|
||||
${CMAKE_BINARY_DIR}/compile_commands.json
|
||||
${CMAKE_SOURCE_DIR}/compile_commands.json
|
||||
COMMENT "Creating symlink to compile_commands.json in project root"
|
||||
)
|
||||
endif()
|
||||
|
||||
# Set output directories
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
|
||||
# Add include directories
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||
include_directories(${CMAKE_SOURCE_DIR}/libraries)
|
||||
# Build type defaults
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type" FORCE)
|
||||
endif()
|
||||
|
||||
# Gather all source files (.cpp, .c)
|
||||
# Options
|
||||
option(ENABLE_SANITIZERS "Enable address and undefined behavior sanitizers" ON)
|
||||
option(ENABLE_STATIC_ANALYSIS "Enable static analysis warnings" ON)
|
||||
set(OUTPUT_NAME "" CACHE STRING "Name of the output executable (defaults to project name)")
|
||||
|
||||
# Gather source files (avoid GLOB_RECURSE - explicitly list files is better practice)
|
||||
# But keeping it for template flexibility
|
||||
file(GLOB_RECURSE PROJECT_SOURCES
|
||||
${CMAKE_SOURCE_DIR}/include/*.cpp
|
||||
${CMAKE_SOURCE_DIR}/include/*.c
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.cpp
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.c
|
||||
${CMAKE_SOURCE_DIR}/source/*.cpp
|
||||
${CMAKE_SOURCE_DIR}/source/*.c
|
||||
)
|
||||
|
||||
# Gather all header files (.hpp, .h)
|
||||
file(GLOB_RECURSE LIBRARY_SOURCES
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.cpp
|
||||
${CMAKE_SOURCE_DIR}/libraries/*.c
|
||||
)
|
||||
|
||||
# Don't glob headers from include/ as sources (they should only be included)
|
||||
file(GLOB_RECURSE PROJECT_HEADERS
|
||||
${CMAKE_SOURCE_DIR}/include/*.hpp
|
||||
${CMAKE_SOURCE_DIR}/include/*.h
|
||||
@ -33,25 +61,51 @@ file(GLOB_RECURSE PROJECT_HEADERS
|
||||
${CMAKE_SOURCE_DIR}/source/*.h
|
||||
)
|
||||
|
||||
# Allow user to set output program name
|
||||
option(OUTPUT_NAME "Name of the output executable" "")
|
||||
if(OUTPUT_NAME STREQUAL "")
|
||||
set(EXECUTABLE_NAME ${PROJECT_NAME})
|
||||
else()
|
||||
# Combine all sources
|
||||
set(ALL_SOURCES ${PROJECT_SOURCES} ${LIBRARY_SOURCES})
|
||||
|
||||
# Determine executable name
|
||||
if(OUTPUT_NAME)
|
||||
set(EXECUTABLE_NAME ${OUTPUT_NAME})
|
||||
else()
|
||||
set(EXECUTABLE_NAME ${PROJECT_NAME})
|
||||
endif()
|
||||
|
||||
# Add executable with all sources
|
||||
add_executable(${EXECUTABLE_NAME}
|
||||
${PROJECT_SOURCES}
|
||||
# Create executable
|
||||
add_executable(${EXECUTABLE_NAME} ${ALL_SOURCES})
|
||||
|
||||
# Set target properties
|
||||
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
|
||||
OUTPUT_NAME ${EXECUTABLE_NAME}
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
|
||||
)
|
||||
|
||||
set_target_properties(${EXECUTABLE_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_NAME})
|
||||
# Include directories - use target-specific commands
|
||||
target_include_directories(${EXECUTABLE_NAME} PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/include
|
||||
${CMAKE_SOURCE_DIR}/libraries
|
||||
)
|
||||
|
||||
# Enable warnings and extra diagnostics
|
||||
if (MSVC)
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE /W4 /permissive- /analyze)
|
||||
else()
|
||||
# Compiler-specific flags
|
||||
if(MSVC)
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||
/W4 # Warning level 4
|
||||
/permissive- # Standards conformance
|
||||
/Zc:__cplusplus # Correct __cplusplus macro
|
||||
/Zc:inline # Remove unreferenced COMDAT
|
||||
/WX- # Don't treat warnings as errors by default
|
||||
)
|
||||
|
||||
if(ENABLE_STATIC_ANALYSIS)
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE /analyze)
|
||||
endif()
|
||||
|
||||
# MSVC debug flags
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE /Zi /Od)
|
||||
endif()
|
||||
|
||||
else() # GCC/Clang
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||
-Wall
|
||||
-Wextra
|
||||
@ -62,12 +116,73 @@ else()
|
||||
-Wuninitialized
|
||||
-Wunused
|
||||
-Werror=return-type
|
||||
-fsanitize=address,undefined
|
||||
-g
|
||||
-Wcast-align
|
||||
-Wformat=2
|
||||
-Wnull-dereference
|
||||
)
|
||||
target_link_options(${EXECUTABLE_NAME} PRIVATE -fsanitize=address,undefined)
|
||||
|
||||
# Additional warnings for static analysis
|
||||
if(ENABLE_STATIC_ANALYSIS)
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||
-Wcast-qual
|
||||
-Wdouble-promotion
|
||||
-Wold-style-cast
|
||||
)
|
||||
endif()
|
||||
|
||||
# Sanitizers (Debug builds)
|
||||
if(ENABLE_SANITIZERS AND CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
# Check if sanitizers are available
|
||||
include(CheckCXXCompilerFlag)
|
||||
check_cxx_compiler_flag("-fsanitize=address" HAS_ASAN)
|
||||
check_cxx_compiler_flag("-fsanitize=undefined" HAS_UBSAN)
|
||||
|
||||
if(HAS_ASAN AND HAS_UBSAN)
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||
-fsanitize=address,undefined,leak
|
||||
-fno-omit-frame-pointer
|
||||
-g
|
||||
)
|
||||
target_link_options(${EXECUTABLE_NAME} PRIVATE
|
||||
-fsanitize=address,undefined,leak
|
||||
)
|
||||
message(STATUS "Sanitizers enabled: address, undefined, leak")
|
||||
else()
|
||||
message(WARNING "Sanitizers requested but not available - skipping")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Optimization flags for Release
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||
-O3
|
||||
-march=native
|
||||
-DNDEBUG
|
||||
)
|
||||
endif()
|
||||
|
||||
# Debug flags
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||
-O0
|
||||
-g3
|
||||
-ggdb
|
||||
)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Print configuration summary
|
||||
message(STATUS "=== Configuration Summary ===")
|
||||
message(STATUS "Project: ${PROJECT_NAME} v${PROJECT_VERSION}")
|
||||
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
|
||||
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
|
||||
message(STATUS "C Standard: ${CMAKE_C_STANDARD}")
|
||||
message(STATUS "Executable Name: ${EXECUTABLE_NAME}")
|
||||
message(STATUS "Sanitizers: ${ENABLE_SANITIZERS}")
|
||||
message(STATUS "Static Analysis: ${ENABLE_STATIC_ANALYSIS}")
|
||||
message(STATUS "Compiler: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
|
||||
message(STATUS "============================")
|
||||
|
||||
# Optionally, enable testing
|
||||
# enable_testing()
|
||||
# add_subdirectory(tests)
|
||||
|
||||
BIN
PlayerTracker
BIN
PlayerTracker
Binary file not shown.
277
build.sh
277
build.sh
@ -1,37 +1,250 @@
|
||||
if [ -z "$1" ]; then
|
||||
echo "Error: Invalid Argument"
|
||||
echo "Usage: $0 <executable_name>"
|
||||
#!/bin/bash
|
||||
|
||||
# Colors
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
CYAN='\033[0;36m'
|
||||
MAGENTA='\033[0;35m'
|
||||
NC='\033[0m' # No Color
|
||||
BOLD='\033[1m'
|
||||
|
||||
# Unicode symbols
|
||||
CHECK="✓"
|
||||
CROSS="✗"
|
||||
ARROW="➜"
|
||||
GEAR="⚙"
|
||||
HAMMER="🔨"
|
||||
ROCKET="🚀"
|
||||
|
||||
# Default values
|
||||
BUILD_TYPE="Debug"
|
||||
CLEAN_BUILD=false
|
||||
VERBOSE=false
|
||||
JOBS=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
|
||||
|
||||
# Print functions
|
||||
print_header() {
|
||||
echo -e "${BOLD}${BLUE}╔════════════════════════════════════════════════════════╗${NC}"
|
||||
echo -e "${BOLD}${BLUE}║${NC} ${HAMMER} ${BOLD}${CYAN} C/C++ Project Builder${NC} ${BOLD}${BLUE}║${NC}"
|
||||
echo -e "${BOLD}${BLUE}╚════════════════════════════════════════════════════════╝${NC}"
|
||||
echo ""
|
||||
}
|
||||
|
||||
print_separator() {
|
||||
echo -e "${BLUE}────────────────────────────────────────────────────────${NC}"
|
||||
}
|
||||
|
||||
print_success() {
|
||||
echo -e "${GREEN}${CHECK}${NC} $1"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}${CROSS}${NC} $1"
|
||||
}
|
||||
|
||||
print_info() {
|
||||
echo -e "${CYAN}${ARROW}${NC} $1"
|
||||
}
|
||||
|
||||
print_step() {
|
||||
echo -e "${YELLOW}${GEAR}${NC} ${BOLD}$1${NC}"
|
||||
}
|
||||
|
||||
show_progress() {
|
||||
local duration=$1
|
||||
local prefix=$2
|
||||
local size=40
|
||||
already_done() { for ((done=0; done<$elapsed; done++)); do printf "▓"; done }
|
||||
remaining() { for ((remain=$elapsed; remain<$size; remain++)); do printf " "; done }
|
||||
percentage() { printf "| %s%%" $(( (($elapsed)*100)/($size)*100/100 )); }
|
||||
|
||||
for (( elapsed=1; elapsed<=$size; elapsed++ )); do
|
||||
printf "\r${CYAN}${prefix}${NC} [$(already_done)$(remaining)] $(percentage)"
|
||||
sleep $(echo "scale=3; $duration/$size" | bc)
|
||||
done
|
||||
printf "\n"
|
||||
}
|
||||
|
||||
usage() {
|
||||
echo -e "${BOLD}Usage:${NC} $0 [OPTIONS] <executable_name>"
|
||||
echo ""
|
||||
echo -e "${BOLD}Options:${NC}"
|
||||
echo -e " -r, --release Build in Release mode (default: Debug)"
|
||||
echo -e " -c, --clean Clean build directory before building"
|
||||
echo -e " -v, --verbose Verbose make output"
|
||||
echo -e " -j, --jobs <N> Number of parallel jobs (default: $JOBS)"
|
||||
echo -e " -h, --help Show this help message"
|
||||
echo ""
|
||||
echo -e "${BOLD}Examples:${NC}"
|
||||
echo -e " $0 myprogram"
|
||||
echo -e " $0 -r -j8 myprogram"
|
||||
echo -e " $0 --clean --release myprogram"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
if [ ! -d "./build" ]; then
|
||||
echo "Creating build directory..."
|
||||
mkdir build
|
||||
fi
|
||||
|
||||
echo ">>> Building C++ Project <<<"
|
||||
cd ./build
|
||||
cmake -DOUTPUT_NAME=$1 ..
|
||||
echo ">>> Compiling... <<<"
|
||||
make
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error: Build failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp ./bin/$1 ../
|
||||
cd ..
|
||||
|
||||
echo ">>> Copying PlayerTracker to AC Servers <<<"
|
||||
for server in ../servers/*; do
|
||||
if [ -d "$server/utils" ]; then
|
||||
target="$server/utils/ACPlayer_tracker"
|
||||
mkdir -p "$target"
|
||||
cp ./PlayerTracker "$target/"
|
||||
echo "Installed PlayerTracker to: $target"
|
||||
fi
|
||||
# Parse arguments
|
||||
POSITIONAL_ARGS=()
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
-r|--release)
|
||||
BUILD_TYPE="Release"
|
||||
shift
|
||||
;;
|
||||
-c|--clean)
|
||||
CLEAN_BUILD=true
|
||||
shift
|
||||
;;
|
||||
-v|--verbose)
|
||||
VERBOSE=true
|
||||
shift
|
||||
;;
|
||||
-j|--jobs)
|
||||
JOBS="$2"
|
||||
shift 2
|
||||
;;
|
||||
-h|--help)
|
||||
usage
|
||||
;;
|
||||
*)
|
||||
POSITIONAL_ARGS+=("$1")
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
echo ">>> Build Complete <<<"
|
||||
echo ">>> Executable: $1 <<<"
|
||||
set -- "${POSITIONAL_ARGS[@]}"
|
||||
|
||||
# Check if executable name is provided
|
||||
if [ -z "$1" ]; then
|
||||
print_header
|
||||
print_error "No executable name provided"
|
||||
echo ""
|
||||
usage
|
||||
fi
|
||||
|
||||
EXECUTABLE_NAME=$1
|
||||
|
||||
# Start build process
|
||||
print_header
|
||||
|
||||
# Build configuration info
|
||||
print_info "Build Configuration:"
|
||||
echo -e " ${BOLD}Executable:${NC} $EXECUTABLE_NAME"
|
||||
echo -e " ${BOLD}Build Type:${NC} $BUILD_TYPE"
|
||||
echo -e " ${BOLD}Jobs:${NC} $JOBS"
|
||||
echo -e " ${BOLD}Clean Build:${NC} $CLEAN_BUILD"
|
||||
echo ""
|
||||
print_separator
|
||||
echo ""
|
||||
|
||||
# Clean build directory if requested
|
||||
if [ "$CLEAN_BUILD" = true ] && [ -d "./build" ]; then
|
||||
print_step "Cleaning build directory..."
|
||||
rm -rf ./build
|
||||
print_success "Build directory cleaned"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# Create build directory
|
||||
if [ ! -d "./build" ]; then
|
||||
print_step "Creating build directory..."
|
||||
mkdir -p build
|
||||
print_success "Build directory created"
|
||||
else
|
||||
print_info "Using existing build directory"
|
||||
fi
|
||||
echo ""
|
||||
|
||||
# CMake configuration
|
||||
print_step "Configuring CMake..."
|
||||
print_separator
|
||||
echo ""
|
||||
|
||||
cd ./build
|
||||
|
||||
CMAKE_CMD="cmake -DOUTPUT_NAME=$EXECUTABLE_NAME -DCMAKE_BUILD_TYPE=$BUILD_TYPE .."
|
||||
|
||||
if [ "$VERBOSE" = true ]; then
|
||||
eval $CMAKE_CMD
|
||||
else
|
||||
eval $CMAKE_CMD > /dev/null 2>&1
|
||||
fi
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
print_error "CMake configuration failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
print_success "CMake configuration complete"
|
||||
echo ""
|
||||
|
||||
# Compilation
|
||||
print_step "Compiling project..."
|
||||
print_separator
|
||||
echo ""
|
||||
|
||||
MAKE_CMD="make -j$JOBS"
|
||||
if [ "$VERBOSE" = true ]; then
|
||||
MAKE_CMD="$MAKE_CMD VERBOSE=1"
|
||||
fi
|
||||
|
||||
START_TIME=$(date +%s)
|
||||
|
||||
if [ "$VERBOSE" = true ]; then
|
||||
eval $MAKE_CMD
|
||||
BUILD_RESULT=$?
|
||||
else
|
||||
eval $MAKE_CMD 2>&1 | tee build.log | while IFS= read -r line; do
|
||||
if echo "$line" | grep -q "\[.*%\]"; then
|
||||
printf "\r${CYAN}${ARROW}${NC} Compiling: %s" "$line"
|
||||
elif echo "$line" | grep -qE "error:|Error|ERROR"; then
|
||||
echo ""
|
||||
print_error "$line"
|
||||
fi
|
||||
done
|
||||
BUILD_RESULT=${PIPESTATUS[0]}
|
||||
fi
|
||||
|
||||
END_TIME=$(date +%s)
|
||||
BUILD_TIME=$((END_TIME - START_TIME))
|
||||
|
||||
echo ""
|
||||
|
||||
if [ $BUILD_RESULT -ne 0 ]; then
|
||||
print_error "Build failed!"
|
||||
echo ""
|
||||
print_info "Check build/build.log for details"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
print_success "Compilation complete (${BUILD_TIME}s)"
|
||||
echo ""
|
||||
|
||||
# Copy executable
|
||||
print_step "Copying executable to project root..."
|
||||
|
||||
if [ ! -f "./bin/$EXECUTABLE_NAME" ]; then
|
||||
print_error "Executable not found: ./bin/$EXECUTABLE_NAME"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cp ./bin/$EXECUTABLE_NAME ../
|
||||
print_success "Executable copied"
|
||||
echo ""
|
||||
|
||||
# Build summary
|
||||
print_separator
|
||||
echo -e "${BOLD}${GREEN}${ROCKET} Build Complete!${NC}"
|
||||
print_separator
|
||||
echo ""
|
||||
echo -e "${BOLD}Summary:${NC}"
|
||||
echo -e " ${BOLD}Executable:${NC} ./$EXECUTABLE_NAME"
|
||||
echo -e " ${BOLD}Build Type:${NC} $BUILD_TYPE"
|
||||
echo -e " ${BOLD}Build Time:${NC} ${BUILD_TIME}s"
|
||||
echo -e " ${BOLD}Binary Location:${NC} ./build/bin/$EXECUTABLE_NAME"
|
||||
|
||||
echo ""
|
||||
print_info "Run with: ${BOLD}./$EXECUTABLE_NAME${NC}"
|
||||
echo ""
|
||||
|
||||
@ -1 +0,0 @@
|
||||
build/compile_commands.json
|
||||
19
include/app.hpp
Normal file
19
include/app.hpp
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef APP_CONFIG_HPP
|
||||
#define APP_CONFIG_HPP
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
|
||||
struct app_info {
|
||||
// From args
|
||||
uint16_t app_id;
|
||||
uint16_t app_port_in;
|
||||
uint16_t app_port_out;
|
||||
|
||||
// From .env
|
||||
std::string app_api_socket_path;
|
||||
uint16_t max_players;
|
||||
std::string app_server_out_ip;
|
||||
};
|
||||
|
||||
#endif
|
||||
19
include/file.hpp
Normal file
19
include/file.hpp
Normal file
@ -0,0 +1,19 @@
|
||||
#ifndef FILE_H
|
||||
#define FILE_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <map>
|
||||
|
||||
#include "app.hpp"
|
||||
#include "server_structs.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
vector<string> read_file(const char *filePath);
|
||||
app_info parce_args(int argc, char *argv[]);
|
||||
|
||||
#endif
|
||||
193
include/handle.hpp
Normal file
193
include/handle.hpp
Normal file
@ -0,0 +1,193 @@
|
||||
#ifndef HANDLE_HPP
|
||||
#define HANDLE_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "file.hpp" // for parce_args
|
||||
#include "net.hpp" // for socket operations
|
||||
#include "server_structs.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class PacketReader {
|
||||
private:
|
||||
const u_char *buffer_;
|
||||
size_t buffer_size_;
|
||||
size_t offset_ = 0;
|
||||
bool error_ = false;
|
||||
|
||||
public:
|
||||
PacketReader(); // TODO:
|
||||
~PacketReader(); // TODO:
|
||||
|
||||
bool read_uint8(uint8_t &value) {
|
||||
size_t __bytes = sizeof(value);
|
||||
|
||||
if (offset_ + __bytes > buffer_size_) {
|
||||
error_ = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(&value, buffer_ + offset_, sizeof(value));
|
||||
|
||||
offset_++;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read_uint16(uint16_t &value) {
|
||||
size_t __bytes = sizeof(value);
|
||||
|
||||
if (offset_ + __bytes > buffer_size_) {
|
||||
error_ = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(&value, buffer_ + offset_, sizeof(value));
|
||||
value = ntohs(value);
|
||||
|
||||
offset_ += 2;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read_uint32(uint32_t &value) {
|
||||
size_t __bytes = sizeof(value);
|
||||
|
||||
if (offset_ + __bytes > buffer_size_) {
|
||||
error_ = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(&value, buffer_ + offset_, sizeof(value));
|
||||
value = ntohl(value);
|
||||
|
||||
offset_ += 4;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read_float(float &value) {
|
||||
size_t __bytes = sizeof(value);
|
||||
|
||||
if (offset_ + __bytes > buffer_size_) {
|
||||
error_ = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t v_int;
|
||||
memcpy(&v_int, buffer_ + offset_, sizeof(v_int));
|
||||
v_int = ntohl(v_int);
|
||||
|
||||
memcpy(&value, &v_int, sizeof(value));
|
||||
|
||||
offset_ += 4;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool read_string_utf32l(string &value) {
|
||||
uint8_t __length;
|
||||
if (!read_uint8(__length))
|
||||
return false;
|
||||
|
||||
if (offset_ + (__length * sizeof(uint32_t)) > buffer_size_) {
|
||||
error_ = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
value.clear();
|
||||
for (size_t i = 0; i < __length; i++) {
|
||||
uint32_t codeunit;
|
||||
if (!read_uint32(codeunit))
|
||||
return false;
|
||||
|
||||
if (codeunit == 0) {
|
||||
break; // null terminator
|
||||
}
|
||||
|
||||
value.push_back(static_cast<char>(codeunit));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool has_error() const {
|
||||
return error_;
|
||||
}
|
||||
};
|
||||
|
||||
class ProtocolHandler {
|
||||
private:
|
||||
trackAtributes track_info_;
|
||||
carAtributes player_info_[64];
|
||||
|
||||
public:
|
||||
ProtocolHandler();
|
||||
~ProtocolHandler();
|
||||
|
||||
bool handle_new_session(PacketReader &reader) {
|
||||
|
||||
if (!reader.read_uint8(track_info_.protocol_version))
|
||||
return false;
|
||||
if (!reader.read_uint8(track_info_.session_index))
|
||||
return false;
|
||||
if (!reader.read_uint8(track_info_.current_session_index))
|
||||
return false;
|
||||
if (!reader.read_uint8(track_info_.session_count))
|
||||
return false;
|
||||
|
||||
/*
|
||||
* str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.server_name, str_len_8, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.track, str_len_8, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.track_config, str_len_8, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.session_name, str_len_8, &ok);
|
||||
|
||||
trackInfo.typ = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.time = read_uint16((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.laps = read_uint16((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.wait_time = read_uint16((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.ambient_temp = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.road_temp = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.weather_graphics, str_len_8, &ok);
|
||||
*/
|
||||
|
||||
if (!reader.read_string_utf32l(track_info_.server_name))
|
||||
return false;
|
||||
if (!reader.read_string_utf32l(track_info_.track))
|
||||
return false;
|
||||
if (!reader.read_string_utf32l(track_info_.track_config))
|
||||
return false;
|
||||
if (!reader.read_string_utf32l(track_info_.session_name))
|
||||
return false;
|
||||
if (!reader.read_uint8(track_info_.typ))
|
||||
return false;
|
||||
if (!reader.read_uint16(track_info_.time))
|
||||
return false;
|
||||
if (!reader.read_uint16(track_info_.laps))
|
||||
return false;
|
||||
if (!reader.read_uint16(track_info_.wait_time))
|
||||
return false;
|
||||
if (!reader.read_uint8(track_info_.ambient_temp))
|
||||
return false;
|
||||
if (!reader.read_uint8(track_info_.road_temp))
|
||||
return false;
|
||||
if (!reader.read_string_utf32l(track_info_.weather_graphics))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
#endif // HANDLE_HPP
|
||||
23
include/log.h
Normal file
23
include/log.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef LOG_H
|
||||
#define LOG_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
|
||||
typedef enum {
|
||||
LOG_INFO,
|
||||
LOG_DEBUG,
|
||||
LOG_WARN,
|
||||
LOG_ERROR
|
||||
} LogLevel;
|
||||
|
||||
void log_print(LogLevel level, const char* format, ...);
|
||||
|
||||
#define log_info(...) log_print(LOG_INFO, __VA_ARGS__)
|
||||
#define log_debug(...) log_print(LOG_DEBUG, __VA_ARGS__)
|
||||
#define log_warn(...) log_print(LOG_WARN, __VA_ARGS__)
|
||||
#define log_error(...) log_print(LOG_ERROR, __VA_ARGS__)
|
||||
|
||||
#endif // LOG_H
|
||||
|
||||
45
include/mapper.hpp
Normal file
45
include/mapper.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
#ifndef MAPPER_HPP
|
||||
#define MAPPER_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
|
||||
#include "parcer.h"
|
||||
#include "server_structs.h"
|
||||
|
||||
// INFO: This is basically a identity mapper between raw byte buffer and structs defined in server_structs.h
|
||||
class Mapper {
|
||||
private:
|
||||
const uint_least8_t *buffer;
|
||||
size_t buffer_size;
|
||||
size_t offset;
|
||||
bool ok;
|
||||
|
||||
public:
|
||||
Mapper(const uint_least8_t *buf, size_t buf_size);
|
||||
|
||||
uint8_t get_message_type();
|
||||
bool is_ok() const { return ok; }
|
||||
|
||||
void parse_new_session(trackAtributes &track);
|
||||
void parse_new_connection(carAtributes &car);
|
||||
void parse_connection_closed(carAtributes &car);
|
||||
void parse_car_update(carAtributes &car);
|
||||
void parse_car_info(carAtributes &car);
|
||||
void parse_lap_completed(uint8_t &car_id, uint32_t &lap_time, uint32_t &cuts);
|
||||
void parse_collision_event(uint8_t &car1, uint8_t &car2, uint8_t &event_type);
|
||||
void parse_chat(uint8_t &car_id, char *message, size_t max_len);
|
||||
void parse_client_loaded(uint8_t &car_id);
|
||||
|
||||
void set_size(size_t size) { this->buffer_size = size; }
|
||||
void update_buffer(const uint8_t *buf, size_t size) {
|
||||
this->buffer = buf;
|
||||
this->buffer_size = size;
|
||||
reset();
|
||||
}
|
||||
private:
|
||||
// INFO: Reset the offset to 1 because the first byte is message type
|
||||
void reset() { offset = 1; ok = true; }
|
||||
};
|
||||
|
||||
#endif // MAPPER_HPP
|
||||
52
include/net.hpp
Normal file
52
include/net.hpp
Normal file
@ -0,0 +1,52 @@
|
||||
#ifndef NET_HPP
|
||||
#define NET_HPP
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "server_structs.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Socket {
|
||||
private:
|
||||
// Socket file descriptor
|
||||
int sock_server;
|
||||
int sock_unix;
|
||||
struct sockaddr_in server_addr;
|
||||
struct sockaddr_un server_addr_unix;
|
||||
|
||||
// Server port for input (listening)
|
||||
int server_port_input;
|
||||
char buffer[1024];
|
||||
|
||||
// Socket Data
|
||||
api_packet packet_data;
|
||||
|
||||
public:
|
||||
Socket();
|
||||
~Socket();
|
||||
|
||||
void connect_server(const char *ip, uint16_t port);
|
||||
void bind_server(const char *ip, uint16_t port);
|
||||
void send_server();
|
||||
void send_server(const api_packet &data);
|
||||
void send_server(const void *data, size_t len);
|
||||
ssize_t receive_server(void *buffer, size_t len);
|
||||
|
||||
void connect_unix(const char *ip, uint16_t port);
|
||||
void send_unix();
|
||||
void send_unix(const api_packet &data);
|
||||
void set_packet(const api_packet &data);
|
||||
|
||||
api_packet create_packet(uint8_t tracker_id);
|
||||
api_packet get_packet();
|
||||
};
|
||||
|
||||
#endif // NET_HPP
|
||||
@ -27,6 +27,10 @@ u_int8_t read_uint8(const u_int8_t *buf, size_t recv_len, size_t *offset, int *o
|
||||
// Advances the offset by 2 bytes.
|
||||
u_int16_t read_uint16(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||
|
||||
// Reads a 16-bit unsigned integer from the buffer in little-endian byte order.
|
||||
// Advances the offset by 2 bytes.
|
||||
u_int16_t read_uint16_le(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||
|
||||
// Reads a 32-bit unsigned integer from the buffer in network byte order.
|
||||
// Advances the offset by 4 bytes.
|
||||
u_int32_t read_uint32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||
|
||||
62
include/parcer.h.old
Normal file
62
include/parcer.h.old
Normal file
@ -0,0 +1,62 @@
|
||||
#ifndef PARCER_H
|
||||
#define PARCER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "server_structs.hpp"
|
||||
|
||||
// Ensures that there are at least 'need' bytes available in the buffer
|
||||
// starting from 'offset'. Returns 1 if enough bytes are available, 0 otherwise.
|
||||
int ensure(size_t recv_len, size_t offset, size_t need);
|
||||
|
||||
// Reads an 8-bit unsigned integer from the buffer.
|
||||
// Advances the offset by 1 byte.
|
||||
u_int8_t read_uint8(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||
|
||||
// Reads a 16-bit unsigned integer from the buffer in network byte order.
|
||||
// Advances the offset by 2 bytes.
|
||||
u_int16_t read_uint16(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||
|
||||
// Reads a 32-bit unsigned integer from the buffer in network byte order.
|
||||
// Advances the offset by 4 bytes.
|
||||
u_int32_t read_uint32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||
|
||||
// Reads a 32-bit signed integer from the buffer in network byte order.
|
||||
// Advances the offset by 4 bytes.
|
||||
int32_t read_int32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||
|
||||
// Reads a 32-bit float from the buffer in network byte order.
|
||||
// Advances the offset by 4 bytes.
|
||||
float read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||
|
||||
// Reads a fixed number of bytes into the output buffer.
|
||||
// The output buffer must be at least 'len' bytes long.
|
||||
void read_bytes(const u_int8_t *buf, size_t recv_len, size_t *offset, u_int8_t *out, size_t len, int *ok);
|
||||
|
||||
// Reads a length-prefixed string. The length is a single byte.
|
||||
// The string is not null-terminated in the buffer, but the function
|
||||
void read_utf32le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok);
|
||||
|
||||
// reads up to max_len - 1 characters and null-terminates the destination buffer.
|
||||
// The string is encoded in UTF-32LE.
|
||||
void read_utf16le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok);
|
||||
|
||||
// Reads a null-terminated string from the buffer.
|
||||
// The string is read into the output buffer, which must be at least max_len bytes long
|
||||
void read_string(const u_int8_t *buf, size_t recv_len, size_t *offset, char *out, size_t max_len, int *ok);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // PARCER_H
|
||||
@ -4,6 +4,8 @@
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define MAX_PLAYERS 64
|
||||
|
||||
typedef struct handshake {
|
||||
// [not used in the current Remote Telemtry version by AC]
|
||||
// In future versions it will identify the platform type of the client.
|
||||
@ -68,8 +70,8 @@ typedef enum flag {
|
||||
|
||||
typedef struct carAtributes {
|
||||
// Related to ACSP_CAR_INFO
|
||||
u_char isConnected; // 1 = connected, 0 = disconnected
|
||||
u_char isLoading; // 1 = loading, 0 = not isLoading
|
||||
char isConnected; // 1 = connected, 0 = disconnected
|
||||
char isLoading; // 1 = loading, 0 = not isLoading
|
||||
|
||||
char *car_model;
|
||||
char *car_skin;
|
||||
@ -99,8 +101,8 @@ typedef struct carAtributes {
|
||||
|
||||
typedef struct carAtributesAPI {
|
||||
// Related to ACSP_CAR_INFO
|
||||
u_char isConnected; // 1 = connected, 0 = disconnected
|
||||
u_char isLoading; // 1 = loading, 0 = not isLoading
|
||||
char isConnected; // 1 = connected, 0 = disconnected
|
||||
char isLoading; // 1 = loading, 0 = not isLoading
|
||||
|
||||
char car_model[64];
|
||||
char car_skin[64];
|
||||
@ -184,7 +186,7 @@ typedef struct trackAtributesAPI {
|
||||
} __attribute__((packed)) trackAtributesAPI;
|
||||
|
||||
typedef struct api_packet {
|
||||
u_char message_type; // ACSP_MessageType
|
||||
char message_type; // ACSP_MessageType
|
||||
u_int8_t tracker_id;
|
||||
u_int8_t connected_players;
|
||||
|
||||
@ -197,6 +199,7 @@ enum ACSP_MessageType {
|
||||
// ============================
|
||||
// PROTOCOL VERSION
|
||||
// ============================
|
||||
// DONE: Update this when protocol changes
|
||||
PROTOCOL_VERSION = 4,
|
||||
|
||||
// ============================
|
||||
|
||||
158
include/server_structs.hpp
Normal file
158
include/server_structs.hpp
Normal file
@ -0,0 +1,158 @@
|
||||
#ifndef SERVER_STRUCTS_H
|
||||
#define SERVER_STRUCTS_H
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
typedef struct app_info {
|
||||
// From args
|
||||
u_int16_t app_id;
|
||||
u_int16_t app_port_in;
|
||||
u_int16_t app_port_out;
|
||||
|
||||
// From .env
|
||||
const char *app_api_socket_path;
|
||||
u_int16_t max_players;
|
||||
const char *app_server_out_ip;
|
||||
} __attribute__((packed)) app_info;
|
||||
|
||||
typedef struct postion {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
} __attribute__((packed)) postion;
|
||||
|
||||
typedef enum flag {
|
||||
NO_FLAG = 0,
|
||||
YELLOW_FLAG = 1,
|
||||
BLUE_FLAG = 2,
|
||||
BLACK_FLAG = 3,
|
||||
CHECKERED_FLAG = 4,
|
||||
} __attribute__((packed)) flag;
|
||||
|
||||
typedef struct carAtributes {
|
||||
// Related to ACSP_CAR_INFO
|
||||
u_char isConnected; // 1 = connected, 0 = disconnected
|
||||
u_char isLoading; // 1 = loading, 0 = not isLoading
|
||||
|
||||
string car_model[64];
|
||||
string car_skin[64];
|
||||
string driver_name[64];
|
||||
string driver_team[64];
|
||||
string driver_GUID[64];
|
||||
|
||||
// Related to ACSP_CAR_UPDATE
|
||||
u_int8_t carID;
|
||||
postion position;
|
||||
postion velocity;
|
||||
u_int8_t carGear;
|
||||
u_int16_t carRPM;
|
||||
u_int32_t lap_time;
|
||||
u_int32_t cuts;
|
||||
u_int32_t total_cuts;
|
||||
u_int32_t total_cuts_alltime;
|
||||
u_int16_t total_laps_completed;
|
||||
u_int16_t contacts;
|
||||
u_int16_t total_contacts;
|
||||
|
||||
flag current_flag; // TODO: implement flag status updates
|
||||
// TAG:3
|
||||
|
||||
float normalizedSplinePos;
|
||||
} __attribute__((packed)) carAtributes;
|
||||
|
||||
|
||||
typedef enum SessionType {
|
||||
PRACTICE = 0,
|
||||
RACE = 1,
|
||||
QUALIFYING = 2,
|
||||
} __attribute__((packed)) SessionType;
|
||||
|
||||
typedef struct trackAtributes{
|
||||
u_int8_t protocol_version;
|
||||
|
||||
u_int8_t session_index;
|
||||
u_int8_t current_session_index;
|
||||
u_int8_t session_count;
|
||||
SessionType session_type;
|
||||
|
||||
string server_name;
|
||||
string track[64];
|
||||
string track_config[64];
|
||||
string session_name[64];
|
||||
|
||||
u_int8_t typ;
|
||||
u_int16_t time;
|
||||
u_int16_t laps;
|
||||
u_int16_t wait_time;
|
||||
u_int8_t ambient_temp;
|
||||
u_int8_t road_temp;
|
||||
|
||||
string weather_graphics[64];
|
||||
u_int32_t elapsed_ms;
|
||||
} __attribute__((packed)) trackAtributes;
|
||||
|
||||
typedef struct api_packet {
|
||||
u_char message_type; // ACSP_MessageType
|
||||
u_int8_t tracker_id;
|
||||
u_int8_t connected_players;
|
||||
|
||||
carAtributes cars[64];
|
||||
trackAtributes track_info;
|
||||
} __attribute__((packed)) api_packet;
|
||||
|
||||
enum ACSP_MessageType {
|
||||
// ============================
|
||||
// PROTOCOL VERSION
|
||||
// ============================
|
||||
PROTOCOL_VERSION = 4,
|
||||
|
||||
// ============================
|
||||
// SERVER → CLIENT MESSAGES
|
||||
// ============================
|
||||
ACSP_NEW_SESSION = 50,
|
||||
ACSP_NEW_CONNECTION = 51,
|
||||
ACSP_CONNECTION_CLOSED = 52,
|
||||
ACSP_CAR_UPDATE = 53,
|
||||
ACSP_CAR_INFO = 54, // Response to ACSP_GET_CAR_INFO
|
||||
ACSP_END_SESSION = 55,
|
||||
ACSP_VERSION = 56,
|
||||
ACSP_CHAT = 57,
|
||||
ACSP_CLIENT_LOADED = 58,
|
||||
ACSP_SESSION_INFO = 59,
|
||||
ACSP_ERROR = 60,
|
||||
ACSP_LAP_COMPLETED = 73,
|
||||
|
||||
// ============================
|
||||
// EVENTS
|
||||
// ============================
|
||||
ACSP_CLIENT_EVENT = 130,
|
||||
|
||||
// ============================
|
||||
// EVENT TYPES
|
||||
// ============================
|
||||
ACSP_CE_COLLISION_WITH_CAR = 10,
|
||||
ACSP_CE_COLLISION_WITH_ENV = 11,
|
||||
|
||||
// ============================
|
||||
// CLIENT → SERVER COMMANDS
|
||||
// ============================
|
||||
ACSP_REALTIMEPOS_INTERVAL = 200,
|
||||
ACSP_GET_CAR_INFO = 201,
|
||||
ACSP_SEND_CHAT = 202, // Sends chat to one car
|
||||
ACSP_BROADCAST_CHAT = 203, // Sends chat to everybody
|
||||
ACSP_GET_SESSION_INFO = 204,
|
||||
ACSP_SET_SESSION_INFO = 205,
|
||||
ACSP_KICK_USER = 206,
|
||||
ACSP_NEXT_SESSION = 207,
|
||||
ACSP_RESTART_SESSION = 208,
|
||||
ACSP_ADMIN_COMMAND = 209 // Send message plus a string
|
||||
};
|
||||
|
||||
#endif // SERVER_STRUCTS_H
|
||||
33
include/session.hpp
Normal file
33
include/session.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
#ifndef SESSION_MANAGER_HPP
|
||||
#define SESSION_MANAGER_HPP
|
||||
|
||||
#include "server_structs.h"
|
||||
#include <string.h>
|
||||
#include <cstring>
|
||||
|
||||
class SessionManager {
|
||||
private:
|
||||
trackAtributes track_info;
|
||||
carAtributes players[MAX_PLAYERS];
|
||||
u_int8_t connected_players;
|
||||
u_int8_t server_id;
|
||||
|
||||
public:
|
||||
SessionManager(u_int8_t sid);
|
||||
|
||||
void on_new_session(const trackAtributes &track);
|
||||
void on_player_connected(const carAtributes &car);
|
||||
void on_player_finished_loading(u_int8_t car_id);
|
||||
void on_player_disconnected(u_int8_t car_id);
|
||||
void on_car_update(const carAtributes &car);
|
||||
void on_lap_completed(u_int8_t car_id, u_int32_t lap_time, u_int32_t cuts);
|
||||
void on_collision(u_int8_t car1, u_int8_t car2);
|
||||
|
||||
api_packet build_packet(u_int8_t message_type);
|
||||
|
||||
const trackAtributes& get_track_info() const { return track_info; }
|
||||
const carAtributes* get_players() const { return players; }
|
||||
u_int8_t get_connected_players() const { return connected_players; }
|
||||
};
|
||||
|
||||
#endif
|
||||
42
include/socket.h.old
Normal file
42
include/socket.h.old
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef SOCKET_H
|
||||
#define SOCKET_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#pragma comment(lib, "ws2_32.lib") // link Winsock
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
// =========================
|
||||
// UDP SOCKET FUCNTIONS
|
||||
// =========================
|
||||
// Server hosts a UDP socket at 127.0.0.1:12000
|
||||
// Client sends a message to the server at 11000
|
||||
|
||||
// UDP socket creation & management
|
||||
int create_udp_socket(void);
|
||||
int connect_udp_socket(const char *ip, uint16_t port);
|
||||
int create_bound_udp_socket(const char *ip, uint16_t port);
|
||||
|
||||
// UDP messaging
|
||||
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, uint16_t dest_port);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // SOCKET_H
|
||||
58
source/file.cpp
Normal file
58
source/file.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#include "file.hpp"
|
||||
|
||||
vector<string> read_file(const char *filePath) {
|
||||
ifstream file(filePath);
|
||||
if (!file.is_open()) {
|
||||
return vector<string>();
|
||||
}
|
||||
|
||||
vector<string> lines;
|
||||
string line;
|
||||
|
||||
while (getline(file, line)) {
|
||||
lines.push_back(line);
|
||||
}
|
||||
|
||||
file.close();
|
||||
return lines;
|
||||
}
|
||||
|
||||
app_info parce_args(int argc, char *argv[]) {
|
||||
// example
|
||||
// ./player_tracker 130 12000 13000
|
||||
// -id -serverin -serverout
|
||||
app_info __processed_info;
|
||||
|
||||
if (argc <= 1) {
|
||||
throw invalid_argument("No argc provided");
|
||||
} else if (argc != 4) {
|
||||
throw invalid_argument("Invalid number of args provided");
|
||||
}
|
||||
|
||||
__processed_info.app_id = (u_int16_t)atoi(argv[1]);
|
||||
__processed_info.app_port_in = (u_int16_t)atoi(argv[2]);
|
||||
__processed_info.app_port_out = (u_int16_t)atoi(argv[3]);
|
||||
|
||||
// Parce .env
|
||||
vector<string> __read_lines = read_file("./.env");
|
||||
map<string, string> __env_args;
|
||||
|
||||
for (size_t i = 0; i < __read_lines.size() - 1; i++) {
|
||||
string token = __read_lines[i].substr(0, __read_lines[i].find(" = "));
|
||||
|
||||
__read_lines[i].erase(0, __read_lines[i].find(" = ") + 3);
|
||||
|
||||
__env_args[token] = __read_lines[i];
|
||||
}
|
||||
|
||||
// DEBUG
|
||||
// for (const auto& [key, value] : __env_args) {
|
||||
// std::cout << '[' << key << "] = " << value << "; ";
|
||||
// }
|
||||
|
||||
__processed_info.app_api_socket_path = __env_args["API_SOCKET_PATH"];
|
||||
__processed_info.max_players = (u_int16_t)atoi(__env_args["MAX_PLAYERS"].c_str());
|
||||
__processed_info.app_server_out_ip = __env_args["SERVER_OUT_IP"];
|
||||
|
||||
return __processed_info;
|
||||
}
|
||||
24
source/log.c
Normal file
24
source/log.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include "log.h"
|
||||
|
||||
void log_print(LogLevel level, const char* format, ...) {
|
||||
time_t now = time(NULL);
|
||||
struct tm *t = localtime(&now);
|
||||
|
||||
const char* level_tag;
|
||||
switch(level) {
|
||||
case LOG_INFO: level_tag = "INF"; break;
|
||||
case LOG_DEBUG: level_tag = "DBG"; break;
|
||||
case LOG_ERROR: level_tag = "ERR"; break;
|
||||
case LOG_WARN: level_tag = "WRN"; break;
|
||||
default: level_tag = "UNK"; break;
|
||||
}
|
||||
|
||||
// %02d ensures it prints "05" instead of just "5"
|
||||
printf("[%02d:%02d:%02d %s] ",
|
||||
t->tm_hour, t->tm_min, t->tm_sec, level_tag);
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vprintf(format, args); // vprintf takes a va_list instead of ...
|
||||
va_end(args);
|
||||
}
|
||||
897
source/main.cpp
897
source/main.cpp
@ -1,801 +1,154 @@
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <exception>
|
||||
#include <iterator>
|
||||
#include <pthread.h>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
#include "parcer.h"
|
||||
#include "server_structs.h"
|
||||
#include "socket.h"
|
||||
#include "app.hpp" // for app_info struct
|
||||
#include "file.hpp" // for parce_args
|
||||
#include "log.h" // for logging
|
||||
#include "net.hpp" // for socket operations
|
||||
#include "server_structs.h" // for api_packet and ACSP_MessageType
|
||||
#include "session.hpp" // for SessionManager
|
||||
#include "mapper.hpp" // for Mapper
|
||||
|
||||
#define DEBUG_CAR_INFO 1
|
||||
const u_int8_t UPDATE_INTERVAL = 120; // in milliseconds
|
||||
|
||||
const char *API_SOCKET_PATH = "/tmp/ACplayer_socket";
|
||||
using namespace std;
|
||||
|
||||
const int MAX_PLAYERS = 64;
|
||||
const char *SERVER_OUT_IP = "127.0.0.1";
|
||||
volatile bool STOP_PROGRAM = false;
|
||||
|
||||
u_int8_t SERVER_ID;
|
||||
api_packet current_packet;
|
||||
u_int8_t apiSent = 0;
|
||||
|
||||
// INFO: This assumes Players pointer to be the same size of MAX_PLAYERS
|
||||
void update_api_packet(trackAtributes trackInfo, carAtributes *players) {
|
||||
current_packet.tracker_id = SERVER_ID;
|
||||
|
||||
// Update track info
|
||||
current_packet.track_info.protocol_version = trackInfo.protocol_version;
|
||||
current_packet.track_info.session_index = trackInfo.session_index;
|
||||
current_packet.track_info.current_session_index = trackInfo.current_session_index;
|
||||
current_packet.track_info.session_count = trackInfo.session_count;
|
||||
current_packet.track_info.session_type = trackInfo.session_type;
|
||||
strncpy(current_packet.track_info.server_name, trackInfo.server_name, sizeof(current_packet.track_info.server_name) - 1);
|
||||
strncpy(current_packet.track_info.track, trackInfo.track, sizeof(current_packet.track_info.track) - 1);
|
||||
strncpy(current_packet.track_info.track_config, trackInfo.track_config, sizeof(current_packet.track_info.track_config) - 1);
|
||||
strncpy(current_packet.track_info.session_name, trackInfo.session_name, sizeof(current_packet.track_info.session_name) - 1);
|
||||
current_packet.track_info.typ = trackInfo.typ;
|
||||
current_packet.track_info.time = trackInfo.time;
|
||||
current_packet.track_info.laps = trackInfo.laps;
|
||||
current_packet.track_info.wait_time = trackInfo.wait_time;
|
||||
current_packet.track_info.ambient_temp = trackInfo.ambient_temp;
|
||||
current_packet.track_info.road_temp = trackInfo.road_temp;
|
||||
strncpy(current_packet.track_info.weather_graphics, trackInfo.weather_graphics, sizeof(current_packet.track_info.weather_graphics) - 1);
|
||||
current_packet.track_info.elapsed_ms = trackInfo.elapsed_ms;
|
||||
|
||||
// Update car INFO
|
||||
for (int i = 0; i < MAX_PLAYERS; i++) {
|
||||
current_packet.cars[i].isConnected = players[i].isConnected;
|
||||
current_packet.cars[i].isLoading = players[i].isLoading;
|
||||
|
||||
strncpy(current_packet.cars[i].car_model, players[i].car_model, sizeof(current_packet.cars[i].car_model) - 1);
|
||||
strncpy(current_packet.cars[i].driver_GUID, players[i].driver_GUID, sizeof(current_packet.cars[i].driver_GUID) - 1);
|
||||
strncpy(current_packet.cars[i].car_skin, players[i].car_skin, sizeof(current_packet.cars[i].car_skin) - 1);
|
||||
strncpy(current_packet.cars[i].driver_name, players[i].driver_name, sizeof(current_packet.cars[i].driver_name) - 1);
|
||||
strncpy(current_packet.cars[i].driver_team, players[i].driver_team, sizeof(current_packet.cars[i].driver_team) - 1);
|
||||
|
||||
current_packet.cars[i].carID = players[i].carID;
|
||||
|
||||
current_packet.cars[i].position = players[i].position;
|
||||
current_packet.cars[i].velocity = players[i].velocity;
|
||||
|
||||
current_packet.cars[i].carGear = players[i].carGear;
|
||||
current_packet.cars[i].carRPM = players[i].carRPM;
|
||||
current_packet.cars[i].lap_time = players[i].lap_time;
|
||||
current_packet.cars[i].cuts = players[i].cuts;
|
||||
current_packet.cars[i].total_cuts = players[i].total_cuts;
|
||||
current_packet.cars[i].total_cuts_alltime = players[i].total_cuts_alltime;
|
||||
|
||||
current_packet.cars[i].total_laps_completed = players[i].total_laps_completed;
|
||||
current_packet.cars[i].contacts = players[i].contacts;
|
||||
current_packet.cars[i].total_contacts = players[i].total_contacts;
|
||||
|
||||
current_packet.cars[i].normalizedSplinePos = players[i].normalizedSplinePos;
|
||||
void signal_handler(int signum) {
|
||||
if (signum == SIGINT || signum == SIGTERM) {
|
||||
STOP_PROGRAM = true;
|
||||
}
|
||||
|
||||
apiSent = 0;
|
||||
}
|
||||
|
||||
void *send_to_api_socket(void *arg) {
|
||||
int sock_fd;
|
||||
struct sockaddr_un addr;
|
||||
|
||||
addr.sun_family = AF_UNIX;
|
||||
strcpy(addr.sun_path, API_SOCKET_PATH);
|
||||
|
||||
// --- Connect loop ---
|
||||
while (1) {
|
||||
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sock_fd < 0) {
|
||||
usleep(500000);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (connect(sock_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
close(sock_fd);
|
||||
usleep(500000);
|
||||
continue;
|
||||
}
|
||||
break; // connected
|
||||
}
|
||||
|
||||
printf("[+] Connected to API socket at %s\n", API_SOCKET_PATH);
|
||||
|
||||
// Handshake
|
||||
api_packet handshake;
|
||||
|
||||
handshake.tracker_id = 65;
|
||||
send(sock_fd, &handshake, sizeof(api_packet), MSG_NOSIGNAL);
|
||||
|
||||
while (1) {
|
||||
if (current_packet.tracker_id == (u_int8_t)-1) {
|
||||
usleep(10000);
|
||||
apiSent = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (apiSent) {
|
||||
usleep(10000);
|
||||
continue;
|
||||
}
|
||||
|
||||
ssize_t sent_bytes = send(sock_fd, ¤t_packet, sizeof(api_packet), MSG_NOSIGNAL);
|
||||
if (sent_bytes < 0) {
|
||||
fprintf(stderr, "[!] Failed to send data to API socket, reconnecting...\n");
|
||||
close(sock_fd);
|
||||
|
||||
// Reconnect loop
|
||||
while (1) {
|
||||
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sock_fd < 0) {
|
||||
usleep(500000);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (connect(sock_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
close(sock_fd);
|
||||
usleep(500000);
|
||||
continue;
|
||||
}
|
||||
break; // connected
|
||||
}
|
||||
|
||||
printf("[+] Reconnected to API socket at %s\n", API_SOCKET_PATH);
|
||||
}
|
||||
|
||||
apiSent = 1;
|
||||
|
||||
usleep(10000); // 10ms
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void init_carupdate(carAtributes *car) {
|
||||
car->isConnected = 0;
|
||||
car->isLoading = 0;
|
||||
|
||||
car->car_model = (char *)malloc(64);
|
||||
car->driver_GUID = (char *)malloc(64);
|
||||
car->car_skin = (char *)malloc(64);
|
||||
car->driver_name = (char *)malloc(64);
|
||||
car->driver_team = (char *)malloc(64);
|
||||
|
||||
strcpy(car->car_model, "");
|
||||
strcpy(car->driver_GUID, "");
|
||||
strcpy(car->car_skin, "");
|
||||
strcpy(car->driver_name, "");
|
||||
strcpy(car->driver_team, "");
|
||||
|
||||
car->carID = 0;
|
||||
|
||||
car->position.x = 0.0f;
|
||||
car->position.y = 0.0f;
|
||||
car->position.z = 0.0f;
|
||||
|
||||
car->velocity.x = 0.0f;
|
||||
car->velocity.y = 0.0f;
|
||||
car->velocity.z = 0.0f;
|
||||
|
||||
car->carGear = 0;
|
||||
car->carRPM = 0;
|
||||
car->lap_time = 0;
|
||||
car->cuts = 0;
|
||||
car->total_cuts = 0;
|
||||
car->total_cuts_alltime = 0; // TODO: SQL querry to get total cuts of all time; TAG:1
|
||||
|
||||
car->total_laps_completed = 0;
|
||||
car->contacts = 0;
|
||||
car->total_contacts = 0; // TODO: SQL querry to get total contacts of all time; TAG:2
|
||||
|
||||
car->normalizedSplinePos = 0.0f;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
app_info app = parce_args(argc, argv);
|
||||
api_packet packet;
|
||||
|
||||
// Parse command-line arguments
|
||||
if (argc != 4) {
|
||||
fprintf(stderr, "Usage: %s <server_id> <in_port> <out_port>\n", argv[0]);
|
||||
fprintf(stderr, "Example: %s 131 11001 12001\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
Socket sock;
|
||||
|
||||
SERVER_ID = (u_int8_t)atoi(argv[1]);
|
||||
u_int16_t SERVER_IN_PORT = (u_int16_t)atoi(argv[2]);
|
||||
u_int16_t SERVER_OUT_PORT = (u_int16_t)atoi(argv[3]);
|
||||
SessionManager session_manager(app.app_id);
|
||||
|
||||
printf("[+] Starting server...\n");
|
||||
printf("[+] Server listening on port %d\n", SERVER_IN_PORT);
|
||||
printf("[+] Server sending to %s:%d\n\n", SERVER_OUT_IP, SERVER_OUT_PORT);
|
||||
uint8_t buffer[1028];
|
||||
Mapper map(buffer, 1028);
|
||||
|
||||
current_packet.tracker_id = (u_int8_t)-1; // Mark as uninitialized
|
||||
trackAtributes track;
|
||||
|
||||
int send_sock_fd = connect_udp_socket(SERVER_OUT_IP, SERVER_IN_PORT); // SEND requests to Server
|
||||
int recv_sock_fd = create_bound_udp_socket("0.0.0.0", SERVER_OUT_PORT); // LISTEN to server updates
|
||||
track.server_name = new char[256];
|
||||
track.track = new char[64];
|
||||
track.track_config = new char[64];
|
||||
track.session_name = new char[64];
|
||||
track.weather_graphics = new char[64];
|
||||
|
||||
if (send_sock_fd < 0 || recv_sock_fd < 0) {
|
||||
fprintf(stderr, "[!] Failed to create sockets (%d)(%d)\n", send_sock_fd, recv_sock_fd);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
try {
|
||||
// Connect socket to API
|
||||
// sock.connect_unix(app.app_api_socket_path.c_str(), app.app_port_out);
|
||||
// Connect socket to Server
|
||||
sock.connect_server(app.app_server_out_ip.c_str(), app.app_port_in);
|
||||
sock.bind_server("127.0.0.1", app.app_port_out);
|
||||
|
||||
unsigned long offset = 0;
|
||||
int ok = 1;
|
||||
char buffer[1024];
|
||||
// Await server for initial data
|
||||
sock.receive_server(buffer, sizeof(buffer));
|
||||
log_info("Connected to server, awaiting version confirmation...\n");
|
||||
|
||||
// Start thread to send data to API socket
|
||||
pthread_t api_thread;
|
||||
if (pthread_create(&api_thread, NULL, send_to_api_socket, NULL) != 0) {
|
||||
fprintf(stderr, "[!] Failed to create API socket thread\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
pthread_detach(api_thread);
|
||||
|
||||
trackAtributes trackInfo;
|
||||
carAtributes players[MAX_PLAYERS];
|
||||
|
||||
current_packet.connected_players = 0;
|
||||
carAtributes update;
|
||||
|
||||
// Basically a contrutor for carAtributes (because all strings in carAtributes are pointers)
|
||||
for (int i = 0; i < MAX_PLAYERS; i++) {
|
||||
init_carupdate(&players[i]);
|
||||
}
|
||||
|
||||
init_carupdate(&update);
|
||||
|
||||
trackInfo.server_name = (char *)malloc(128);
|
||||
trackInfo.track = (char *)malloc(128);
|
||||
trackInfo.track_config = (char *)malloc(64);
|
||||
trackInfo.weather_graphics = (char *)malloc(64);
|
||||
trackInfo.session_name = (char *)malloc(64);
|
||||
|
||||
char message[124] = {0}; // just a place to put info messages from the server itself
|
||||
|
||||
printf("\n");
|
||||
while (1) {
|
||||
offset = 0;
|
||||
u_int8_t str_len_8 = 0;
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
// FIX: Its not garanted to receive a full packet in one recvfrom call, but the error -1 is not handled by a unsigned size_t
|
||||
size_t recv_bytes = (size_t)recvfrom(recv_sock_fd, buffer, sizeof(buffer), 0, NULL, NULL);
|
||||
|
||||
// Convert buffer to utf-8 string and print it
|
||||
#if DEBUG_CAR_INFO
|
||||
printf("[+] Received %zd bytes from client\n", recv_bytes);
|
||||
#endif
|
||||
|
||||
current_packet.message_type = (u_char)buffer[0];
|
||||
// The first byte of the messages is always an ACSP_MessageType
|
||||
switch ((u_char)buffer[0]) {
|
||||
// ============================
|
||||
// SERVER → CLIENT MESSAGES
|
||||
// ============================
|
||||
case ACSP_SESSION_INFO:
|
||||
printf("[+] Message Type: ACSP_SESSION_INFO\n");
|
||||
goto skip_message;
|
||||
case ACSP_NEW_SESSION: // DONE:
|
||||
printf("[+] Message Type: ACSP_NEW_SESSION\n");
|
||||
skip_message:
|
||||
offset = 1; // Reset offset to 1 to read after message types
|
||||
|
||||
trackInfo.protocol_version = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.session_index = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.current_session_index = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.session_count = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
trackInfo.session_type = (SessionType)trackInfo.session_index; // FOR BACKWARD COMPATIBILITY
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.server_name, str_len_8, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.track, str_len_8, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.track_config, str_len_8, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.session_name, str_len_8, &ok);
|
||||
|
||||
trackInfo.typ = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.time = read_uint16((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.laps = read_uint16((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.wait_time = read_uint16((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.ambient_temp = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
trackInfo.road_temp = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.weather_graphics, str_len_8, &ok);
|
||||
|
||||
// FIX: elapsed_ms was/is incorrectly read
|
||||
// TEST: Verify with actual server
|
||||
trackInfo.elapsed_ms = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
if (!ok) {
|
||||
fprintf(stderr, "[-] Error parsing NEW_SESSION packet (offset=%zu)\n", offset);
|
||||
break;
|
||||
}
|
||||
|
||||
printf("\tServer Name: \"%s\"\n", trackInfo.server_name);
|
||||
printf("\tTrack: \"%s\"\n", trackInfo.track);
|
||||
printf("\tTrack Config: \"%s\"\n", trackInfo.track_config);
|
||||
printf("\tSession Name: \"%s\"\n", trackInfo.session_name);
|
||||
|
||||
printf("\tProtocol Version: %d\tSession Index: %d/%d\tCurrent Session Index: %d\n", trackInfo.protocol_version, trackInfo.session_index, trackInfo.session_count,
|
||||
trackInfo.current_session_index);
|
||||
|
||||
printf("\tType: %d\tTime: %d mins\tLaps: %d\tWait Time: %d secs\n", trackInfo.typ, trackInfo.time, trackInfo.laps, trackInfo.wait_time);
|
||||
printf("\tAmbient Temp: %d C\tRoad Temp: %d C\n", trackInfo.ambient_temp, trackInfo.road_temp);
|
||||
printf("\tWeather Graphics: %s\n", trackInfo.weather_graphics);
|
||||
printf("\tElapsed Time: %d ms (%2d:%2d:%2d)\n\n", trackInfo.elapsed_ms, trackInfo.elapsed_ms / 60000, (trackInfo.elapsed_ms % 60000) / 1000,
|
||||
(trackInfo.elapsed_ms % 60000) % 1000);
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
case ACSP_NEW_CONNECTION: // DONE:
|
||||
printf("[+] Message Type: ACSP_NEW_CONNECTION\n");
|
||||
offset = 1; // Reset offset to 1 to read after message types
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, update.driver_name, str_len_8, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, update.driver_GUID, str_len_8, &ok);
|
||||
|
||||
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, update.car_model, str_len_8, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, update.car_skin, str_len_8, &ok);
|
||||
|
||||
if (!ok) {
|
||||
fprintf(stderr, "[-] Error parsing NEW_CONNECTION packet (offset=%zu)\n", offset);
|
||||
break;
|
||||
}
|
||||
|
||||
update.isConnected = 1; // Mark as connected
|
||||
update.isLoading = 1; // Mark as Loading
|
||||
current_packet.connected_players++;
|
||||
|
||||
printf("\tDriver Name: \"%s\"\n", update.driver_name);
|
||||
printf("\tDriver GUID: \"%s\"\n", update.driver_GUID);
|
||||
printf("\tCar ID: %d\n", update.carID);
|
||||
printf("\tCar Model: \"%s\"\n", update.car_model);
|
||||
printf("\tCar Skin: \"%s\"\n\n", update.car_skin);
|
||||
|
||||
// Store player player into the respective Index
|
||||
if (update.carID < MAX_PLAYERS) {
|
||||
players[update.carID].isConnected = update.isConnected;
|
||||
players[update.carID].isLoading = update.isLoading;
|
||||
players[update.carID].carID = update.carID;
|
||||
|
||||
strncpy(players[update.carID].driver_GUID, update.driver_GUID, 63);
|
||||
strncpy(players[update.carID].driver_name, update.driver_name, 63);
|
||||
strncpy(players[update.carID].driver_team, update.driver_team, 63);
|
||||
strncpy(players[update.carID].car_model, update.car_model, 63);
|
||||
strncpy(players[update.carID].car_skin, update.car_skin, 63);
|
||||
|
||||
players[update.carID].driver_GUID[63] = '\0';
|
||||
players[update.carID].driver_name[63] = '\0';
|
||||
players[update.carID].driver_team[63] = '\0';
|
||||
players[update.carID].car_model[63] = '\0';
|
||||
players[update.carID].car_skin[63] = '\0';
|
||||
}
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
case ACSP_CONNECTION_CLOSED: // DONE:
|
||||
printf("[+] Message Type: ACSP_CONNECTION_CLOSED\n");
|
||||
offset = 1; // Reset offset to 1 to read after message types
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, update.driver_name, str_len_8, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, update.driver_GUID, str_len_8, &ok);
|
||||
|
||||
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, update.car_model, str_len_8, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_string((const u_int8_t *)buffer, recv_bytes, &offset, update.car_skin, str_len_8, &ok);
|
||||
|
||||
if (!ok) {
|
||||
fprintf(stderr, "[-] Error parsing NEW_CONNECTION packet (offset=%zu)\n", offset);
|
||||
break;
|
||||
}
|
||||
|
||||
update.isConnected = 0; // Mark as disconnected
|
||||
current_packet.connected_players--;
|
||||
if (current_packet.connected_players > MAX_PLAYERS) {
|
||||
current_packet.connected_players = 0;
|
||||
}
|
||||
|
||||
printf("\tDriver Name: \"%s\"\n", update.driver_name);
|
||||
printf("\tDriver GUID: \"%s\"\n", update.driver_GUID);
|
||||
printf("\tCar ID: %d\n", update.carID);
|
||||
printf("\tCar Model: \"%s\"\n", update.car_model);
|
||||
printf("\tCar Skin: \"%s\"\n\n", update.car_skin);
|
||||
|
||||
// Store player player into the respective Index
|
||||
if (update.carID < MAX_PLAYERS) {
|
||||
players[update.carID].isConnected = 0; // Mark disconnected
|
||||
players[update.carID].isLoading = 0;
|
||||
}
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
case ACSP_CAR_UPDATE: // DONE:
|
||||
#if DEBUG_CAR_INFO
|
||||
printf("[+] Message Type: ACSP_CAR_UPDATE\n");
|
||||
#endif
|
||||
offset = 1;
|
||||
|
||||
memcpy(&update.carID, buffer + offset, sizeof(uint8_t));
|
||||
offset = 1 + sizeof(uint8_t);
|
||||
memcpy(&update.position, buffer + offset, sizeof(postion));
|
||||
offset += sizeof(postion);
|
||||
memcpy(&update.velocity, buffer + offset, sizeof(postion));
|
||||
offset += sizeof(postion);
|
||||
memcpy(&update.carGear, buffer + offset, sizeof(u_int8_t));
|
||||
offset += sizeof(u_int8_t);
|
||||
memcpy(&update.carRPM, buffer + offset, sizeof(uint16_t));
|
||||
offset += sizeof(uint16_t);
|
||||
memcpy(&update.normalizedSplinePos, buffer + offset, sizeof(float_t));
|
||||
offset += sizeof(float_t);
|
||||
|
||||
#if DEBUG_CAR_INFO
|
||||
printf("\tCar %d Position: X: %.2f Y: %.2f Z: %.2f ", update.carID, update.position.x, update.position.y, update.position.z);
|
||||
printf("Gear: %d RPM: %d\n", update.carGear, update.carRPM);
|
||||
#endif
|
||||
|
||||
// Store player player into the respective Index
|
||||
if (update.carID < MAX_PLAYERS) {
|
||||
players[update.carID].position = update.position;
|
||||
players[update.carID].velocity = update.velocity;
|
||||
players[update.carID].carGear = update.carGear;
|
||||
players[update.carID].carRPM = update.carRPM;
|
||||
players[update.carID].normalizedSplinePos = update.normalizedSplinePos;
|
||||
|
||||
players[update.carID].isConnected = update.isConnected;
|
||||
players[update.carID].isLoading = update.isLoading;
|
||||
players[update.carID].carID = update.carID;
|
||||
}
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
case ACSP_CAR_INFO: // DONE:
|
||||
printf("[+] Message Type: ACSP_CAR_INFO\n");
|
||||
offset = 1; // Reset offset to 1 to read after message types;
|
||||
|
||||
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
update.isConnected = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, update.car_model, str_len_8, &ok);
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, update.car_skin, str_len_8, &ok);
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, update.driver_name, str_len_8, &ok);
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, update.driver_team, str_len_8, &ok);
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, update.driver_GUID, str_len_8, &ok);
|
||||
|
||||
if (!ok) {
|
||||
fprintf(stderr, "[-] Error parsing CAR_INFO packet (offset=%zu)\n", offset);
|
||||
}
|
||||
|
||||
printf("\tCar ID: %d\n", update.carID);
|
||||
printf("\tIs Connected: %d\n", update.isConnected);
|
||||
printf("\tCar Model: \"%s\"\n", update.car_model);
|
||||
printf("\tCar Skin: \"%s\"\n", update.car_skin);
|
||||
printf("\tDriver Name: \"%s\"\n", update.driver_name);
|
||||
printf("\tDriver Team: \"%s\"\n", update.driver_team);
|
||||
printf("\tDriver GUID: \"%s\"\n\n", update.driver_GUID);
|
||||
|
||||
// Store player player into the respective Index
|
||||
if (update.carID < MAX_PLAYERS) {
|
||||
|
||||
players[update.carID].isConnected = update.isConnected;
|
||||
players[update.carID].isLoading = update.isLoading;
|
||||
players[update.carID].carID = update.carID;
|
||||
|
||||
strncpy(players[update.carID].driver_GUID, update.driver_GUID, 63);
|
||||
strncpy(players[update.carID].driver_name, update.driver_name, 63);
|
||||
strncpy(players[update.carID].driver_team, update.driver_team, 63);
|
||||
strncpy(players[update.carID].car_model, update.car_model, 63);
|
||||
strncpy(players[update.carID].car_skin, update.car_skin, 63);
|
||||
|
||||
players[update.carID].driver_GUID[63] = '\0';
|
||||
players[update.carID].driver_name[63] = '\0';
|
||||
players[update.carID].driver_team[63] = '\0';
|
||||
players[update.carID].car_model[63] = '\0';
|
||||
players[update.carID].car_skin[63] = '\0';
|
||||
}
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
case ACSP_END_SESSION: // DONE: (only session type cycling)
|
||||
printf("[+] Message Type: ACSP_END_SESSION\n");
|
||||
// Advance session_type to the next in the enum;
|
||||
|
||||
trackInfo.session_type = (SessionType)((trackInfo.session_type + 1) % 3);
|
||||
|
||||
if (trackInfo.session_type == 0) {
|
||||
memcpy(trackInfo.session_name, "Practice", 9);
|
||||
} else if (trackInfo.session_type == 1) {
|
||||
memcpy(trackInfo.session_name, "Race", 5);
|
||||
} else if (trackInfo.session_type == 2) {
|
||||
memcpy(trackInfo.session_name, "Qualify", 8);
|
||||
}
|
||||
|
||||
printf("\tNext Session Name: %s\n", trackInfo.session_name);
|
||||
printf("\tNext Session Type: %d\n\n", trackInfo.session_type);
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
case ACSP_VERSION: // DONE:
|
||||
printf("[+] Message Type: ACSP_VERSION\n");
|
||||
offset = 1; // Reset offset to 1 to read after message types
|
||||
trackInfo.protocol_version = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
printf("\tProtocol Version: %d\n\n", trackInfo.protocol_version);
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
case ACSP_CHAT: // DONE: Receive chat messages
|
||||
printf("[+] Message Type: ACSP_CHAT\n");
|
||||
offset = 1; // Reset offset to 1 to read after message types
|
||||
|
||||
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, message, str_len_8, &ok);
|
||||
|
||||
printf("\tCar ID: %d (%s)\n", update.carID, players[update.carID].driver_name);
|
||||
printf("\tMessage: \"%s\"\n\n", message);
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
case ACSP_CLIENT_LOADED: // DONE: Check for client loaded status
|
||||
printf("[+] Message Type: ACSP_CLIENT_LOADED\n");
|
||||
offset = 1; // Reset offset to 1 to read after message types
|
||||
|
||||
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
players[update.carID].isLoading = 0;
|
||||
printf("\tCar ID: %d (%s) Finished Loading into the session\n\n", update.carID, players[update.carID].driver_name);
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
case ACSP_ERROR: // DONE: Simple error message from server
|
||||
printf("[+] Message Type: ACSP_ERROR\n");
|
||||
offset = 1; // Reset offset to 1 to read after message types
|
||||
|
||||
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, message, str_len_8, &ok);
|
||||
|
||||
printf("\tServer Message: \"%s\"\n\n", message);
|
||||
break;
|
||||
|
||||
case ACSP_LAP_COMPLETED: // DONE: Handle lap completed events
|
||||
printf("[+] Message Type: ACSP_LAP_COMPLETED\n");
|
||||
offset = 1; // Reset offset to 1 to read after message types
|
||||
|
||||
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
// TEST: Possiblity of lap_time not behing correct
|
||||
// TODO: Verify with actual serve
|
||||
// SOLUTION: Server sends all times, lap_times and lap counts in big-endian format
|
||||
update.lap_time = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
update.cuts = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
update.total_cuts = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
printf("\tCar ID: %d (%s)\n", update.carID, players[update.carID].driver_name);
|
||||
printf("\tLap Time: %5d ms\n", update.lap_time);
|
||||
printf("\tCuts this lap: %d\n", update.cuts);
|
||||
printf("\tTotal Cuts (this session): %d\n\n", update.total_cuts);
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
// ============================
|
||||
// EVENTS
|
||||
// ============================
|
||||
case ACSP_CLIENT_EVENT: {
|
||||
printf("[+] Message Type: ACSP_CLIENT_EVENT\n");
|
||||
offset = 1; // Reset offset to 1 to read after message types
|
||||
|
||||
u_int8_t event_type = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
u_int8_t event_car_id = 255; // If event doesnt involve another car, set to 255
|
||||
|
||||
switch (event_type) {
|
||||
// ============================
|
||||
// EVENT TYPES
|
||||
// ============================
|
||||
case ACSP_CE_COLLISION_WITH_CAR: {
|
||||
printf("[+] Event Type: ACSP_CE_COLLISION_WITH_CAR\n");
|
||||
event_car_id = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
|
||||
printf("Car ID: %d (%s) collided with Car ID: %d (%s)\n", update.carID, players[update.carID].driver_name, event_car_id, players[event_car_id].driver_name);
|
||||
|
||||
players[update.carID].contacts++;
|
||||
players[event_car_id].contacts++;
|
||||
|
||||
// TODO: Update total contacts for both players in the database
|
||||
// TAG:2 Update total contacts for both players
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
} break;
|
||||
|
||||
case ACSP_CE_COLLISION_WITH_ENV: {
|
||||
printf("[+] Event Type: ACSP_CE_COLLISION_WITH_ENV\n");
|
||||
|
||||
printf("Car ID: %d (%s) collided with the environment\n", update.carID, players[update.carID].driver_name);
|
||||
|
||||
players[update.carID].contacts++;
|
||||
|
||||
// TODO: Update total contacts for the player in the database
|
||||
// TAG:2 Update total contacts for the player
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
} break;
|
||||
}
|
||||
|
||||
// TODO: possible iRacing style impact severity system
|
||||
// With X velues and (e.g >5 m/s = 0x, >10 m/s = 2x, >15 m/s = 4x) && Blackflag limits (e.g x17 = DQ)
|
||||
|
||||
// FIX: read_float doesnt work, but memcpy does... weird...
|
||||
float impact_speed = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
postion world_pos = {0, 0, 0};
|
||||
/* world_pos.x = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
world_pos.y = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
world_pos.z = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
*/
|
||||
|
||||
memcpy(&world_pos.x, buffer + offset, sizeof(float));
|
||||
offset += sizeof(float);
|
||||
memcpy(&world_pos.y, buffer + offset, sizeof(float));
|
||||
offset += sizeof(float);
|
||||
memcpy(&world_pos.z, buffer + offset, sizeof(float));
|
||||
offset += sizeof(float);
|
||||
|
||||
postion rel_pos = {0, 0, 0};
|
||||
/*
|
||||
rel_pos.x = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
rel_pos.y = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
rel_pos.z = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||
*/
|
||||
|
||||
memcpy(&rel_pos.x, buffer + offset, sizeof(float));
|
||||
offset += sizeof(float);
|
||||
memcpy(&rel_pos.y, buffer + offset, sizeof(float));
|
||||
offset += sizeof(float);
|
||||
memcpy(&rel_pos.z, buffer + offset, sizeof(float));
|
||||
offset += sizeof(float);
|
||||
|
||||
printf("\tImpact Speed: %.2f m/s\n", impact_speed);
|
||||
printf("\tWorld Position: X: %.2f Y: %.2f Z: %.2f\n", world_pos.x, world_pos.y, world_pos.z);
|
||||
printf("\tRelative Position: X: %.2f Y: %.2f Z: %.2f\n\n", rel_pos.x, rel_pos.y, rel_pos.z);
|
||||
|
||||
} break;
|
||||
|
||||
// TODO: Add for ranking system
|
||||
// OPTIMIZE: Make sure DB queries are optimized for speed has there can be more that 30 players querying at the same time
|
||||
//
|
||||
// ============================
|
||||
// CLIENT → SERVER COMMANDS
|
||||
// NOTE: These are not meant to be here, these are comands to SEND to the server
|
||||
// ============================
|
||||
case ACSP_REALTIMEPOS_INTERVAL:
|
||||
printf("[+] Command: ACSP_REALTIMEPOS_INTERVAL\n");
|
||||
break;
|
||||
|
||||
case ACSP_GET_CAR_INFO:
|
||||
printf("[+] Command: ACSP_GET_CAR_INFO\n");
|
||||
break;
|
||||
|
||||
case ACSP_SEND_CHAT:
|
||||
printf("[+] Command: ACSP_SEND_CHAT\n");
|
||||
break;
|
||||
|
||||
case ACSP_BROADCAST_CHAT:
|
||||
printf("[+] Command: ACSP_BROADCAST_CHAT\n");
|
||||
break;
|
||||
|
||||
case ACSP_GET_SESSION_INFO:
|
||||
printf("[+] Command: ACSP_GET_SESSION_INFO\n");
|
||||
break;
|
||||
|
||||
case ACSP_SET_SESSION_INFO:
|
||||
printf("[+] Command: ACSP_SET_SESSION_INFO\n");
|
||||
break;
|
||||
|
||||
case ACSP_KICK_USER:
|
||||
printf("[+] Command: ACSP_KICK_USER\n");
|
||||
break;
|
||||
|
||||
case ACSP_NEXT_SESSION:
|
||||
printf("[+] Command: ACSP_NEXT_SESSION\n");
|
||||
break;
|
||||
|
||||
case ACSP_RESTART_SESSION:
|
||||
printf("[+] Command: ACSP_RESTART_SESSION\n");
|
||||
break;
|
||||
|
||||
case ACSP_ADMIN_COMMAND: { // DONE:
|
||||
printf("[+] Command: ACSP_ADMIN_COMMAND\n");
|
||||
char *__buffer = (char *)malloc(256);
|
||||
char command[256] = "/settime 19:00"; // TEST:
|
||||
|
||||
__buffer[0] = (char)ACSP_ADMIN_COMMAND;
|
||||
for (int i = 1; i <= strlen(command); i++) {
|
||||
buffer[i] = command[i - 1];
|
||||
}
|
||||
|
||||
sendto(send_sock_fd, buffer, strlen(command) + 1, 0, NULL, 0);
|
||||
printf("\tSent Admin Command: %s\n\n", command);
|
||||
free(__buffer);
|
||||
} break;
|
||||
|
||||
// ============================
|
||||
// DEFAULT HANDLER
|
||||
// ============================
|
||||
default:
|
||||
printf("[!] Unknown Message Type: %d\n\n", buffer[0]);
|
||||
break;
|
||||
if (buffer[0] == ACSP_VERSION) {
|
||||
log_info("Server version confirmed. Sending update rate request @ %ums\n", UPDATE_INTERVAL);
|
||||
} else {
|
||||
throw runtime_error("Did not receive version confirmation from server.");
|
||||
}
|
||||
|
||||
// OPTIMIZE: This should be done inside each case where data is updated otherwise we lose performance
|
||||
usleep(10000); // Sleep for 10ms
|
||||
}
|
||||
close(send_sock_fd);
|
||||
close(recv_sock_fd);
|
||||
char request[516] = {0};
|
||||
request[0] = ACSP_REALTIMEPOS_INTERVAL;
|
||||
request[1] = UPDATE_INTERVAL;
|
||||
|
||||
// Free allocated memory
|
||||
free(update.car_model);
|
||||
free(update.driver_GUID);
|
||||
free(update.car_skin);
|
||||
free(update.driver_name);
|
||||
free(update.driver_team);
|
||||
sock.send_server(request, sizeof(request));
|
||||
log_debug("Info:\n");
|
||||
log_debug("\t\tApp ID: %d\n", app.app_id);
|
||||
log_debug("\t\tAPI Socket Path: %s\n", app.app_api_socket_path.c_str());
|
||||
log_debug("\t\tServer Out IP: %s\n", app.app_server_out_ip.c_str());
|
||||
log_debug("\t\tApp Port In: %d\n", app.app_port_in);
|
||||
log_debug("\t\tApp Port Out: %d\n", app.app_port_out);
|
||||
|
||||
for (int i = 0; i < MAX_PLAYERS; i++) {
|
||||
free(players[i].car_model);
|
||||
free(players[i].driver_GUID);
|
||||
free(players[i].car_skin);
|
||||
free(players[i].driver_name);
|
||||
free(players[i].driver_team);
|
||||
} catch (const runtime_error &e) {
|
||||
cerr << "Error: " << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
free(trackInfo.server_name);
|
||||
free(trackInfo.track);
|
||||
free(trackInfo.track_config);
|
||||
free(trackInfo.weather_graphics);
|
||||
free(trackInfo.session_name);
|
||||
// TODO: Implement Cache
|
||||
// TAG: Because sometimes the parser doesnt have the name of the server because it started after the server init
|
||||
// we can cache it and reuse it to avoid NULL names in the DB
|
||||
|
||||
while (STOP_PROGRAM == false) {
|
||||
// Receive data from server
|
||||
ssize_t received = sock.receive_server(buffer, sizeof(buffer));
|
||||
|
||||
|
||||
if (received > 0) {
|
||||
map.update_buffer(buffer, static_cast<size_t>(received));
|
||||
switch (map.get_message_type()) {
|
||||
// DONE:
|
||||
case ACSP_VERSION: {
|
||||
log_warn("Received Version Again? (Probably server restart) Resending Update Request @ %ums\n", UPDATE_INTERVAL);
|
||||
|
||||
char request[516] = {0};
|
||||
request[0] = ACSP_REALTIMEPOS_INTERVAL;
|
||||
request[1] = UPDATE_INTERVAL;
|
||||
sock.send_server(request, sizeof(request));
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
case ACSP_CAR_UPDATE: {
|
||||
// log_info("Received car update.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
case ACSP_NEW_SESSION: {
|
||||
log_info("New session started.\n");
|
||||
map.parse_new_session(track);
|
||||
if (map.is_ok()) {
|
||||
session_manager.on_new_session(track);
|
||||
} else {
|
||||
log_error("Failed to parse new session data.\n");
|
||||
}
|
||||
|
||||
// TESTING: Print track info
|
||||
log_info("Track Info:\n");
|
||||
log_info("\t\tServer Name: %s\n", track.server_name);
|
||||
log_info("\t\tTrack: %s\n", track.track);
|
||||
log_info("\t\tTrack Config: %s\n", track.track_config);
|
||||
log_info("\t\tSession Name: %s\n", track.session_name);
|
||||
log_info("\t\tWeather Graphics: %s\n", track.weather_graphics);
|
||||
log_info("\t\tSession Type: %d\n", track.session_type);
|
||||
log_info("\t\tLaps: %d\n", track.laps);
|
||||
log_info("\t\tTime: %d\n", track.time);
|
||||
log_info("\t\tAmbient Temp: %d\n", track.ambient_temp);
|
||||
log_info("\t\tRoad Temp: %d\n", track.road_temp);
|
||||
log_info("\t\tElapsed MS: %u\n", track.elapsed_ms);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
delete[] track.server_name;
|
||||
delete[] track.track;
|
||||
delete[] track.track_config;
|
||||
delete[] track.session_name;
|
||||
delete[] track.weather_graphics;
|
||||
return 0;
|
||||
}
|
||||
|
||||
56
source/mapper.cpp
Normal file
56
source/mapper.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
#include "mapper.hpp"
|
||||
#include "parcer.h"
|
||||
|
||||
Mapper::Mapper(const uint8_t *buf, size_t size) : buffer(buf), buffer_size(size), offset(0), ok(1) {}
|
||||
|
||||
uint8_t Mapper::get_message_type() {
|
||||
if (buffer_size < 1) {
|
||||
this->ok = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return buffer[0];
|
||||
}
|
||||
|
||||
void Mapper::parse_new_session(trackAtributes &track) {
|
||||
reset();
|
||||
int __ok;
|
||||
|
||||
track.protocol_version = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
track.session_index = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
track.current_session_index = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
track.session_count = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
|
||||
track.session_type = (SessionType)track.session_index;
|
||||
|
||||
uint8_t str_len = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
read_utf32le_string(buffer, buffer_size, &offset, track.server_name, str_len, &__ok);
|
||||
|
||||
str_len = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
read_string(buffer, buffer_size, &offset, track.track, str_len, &__ok);
|
||||
|
||||
str_len = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
read_string(buffer, buffer_size, &offset, track.track_config, str_len, &__ok);
|
||||
|
||||
str_len = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
read_string(buffer, buffer_size, &offset, track.session_name, str_len, &__ok);
|
||||
|
||||
track.typ = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
track.time = read_uint16_le(buffer, buffer_size, &offset, &__ok);
|
||||
track.laps = read_uint16_le(buffer, buffer_size, &offset, &__ok);
|
||||
track.wait_time = read_uint16_le(buffer, buffer_size, &offset, &__ok);
|
||||
track.ambient_temp = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
track.road_temp = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
|
||||
str_len = read_uint8(buffer, buffer_size, &offset, &__ok);
|
||||
read_string(buffer, buffer_size, &offset, track.weather_graphics, str_len, &__ok);
|
||||
|
||||
track.elapsed_ms = read_uint32(buffer, buffer_size, &offset, &__ok);
|
||||
|
||||
if (__ok == 0) {
|
||||
this->ok = false;
|
||||
} else {
|
||||
this->ok = true;
|
||||
}
|
||||
}
|
||||
|
||||
116
source/net.cpp
Normal file
116
source/net.cpp
Normal file
@ -0,0 +1,116 @@
|
||||
#include "net.hpp"
|
||||
#include "log.c"
|
||||
#include <sys/types.h>
|
||||
|
||||
Socket::Socket() {
|
||||
sock_server = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
sock_unix = -1;
|
||||
if (sock_server < 0) {
|
||||
throw runtime_error("Failed to create socket");
|
||||
}
|
||||
memset(&server_addr, 0, sizeof(server_addr));
|
||||
server_addr.sin_family = AF_INET;
|
||||
}
|
||||
|
||||
Socket::~Socket() {
|
||||
close(sock_server);
|
||||
close(sock_unix);
|
||||
}
|
||||
|
||||
void Socket::connect_server(const char *ip, uint16_t port) {
|
||||
this->server_port_input = port;
|
||||
server_addr.sin_port = htons(port);
|
||||
if (inet_pton(AF_INET, ip, &server_addr.sin_addr) <= 0) {
|
||||
throw runtime_error("Invalid IP address");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::bind_server(const char *ip, uint16_t port) {
|
||||
struct sockaddr_in bind_addr;
|
||||
memset(&bind_addr, 0, sizeof(bind_addr));
|
||||
bind_addr.sin_family = AF_INET;
|
||||
bind_addr.sin_port = htons(port);
|
||||
bind_addr.sin_addr.s_addr = inet_addr(ip);
|
||||
|
||||
if (bind(sock_server, (const struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) {
|
||||
log_error("Failed to bind UDP socket on %s:%d\n", ip, port);
|
||||
throw runtime_error("Failed to bind UDP socket");
|
||||
}
|
||||
|
||||
log_info("Successfully bound UDP socket on %s:%d\n", ip, port);
|
||||
}
|
||||
|
||||
void Socket::connect_unix(const char *ip, uint16_t port) {
|
||||
sock_unix = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (sock_unix < 0) {
|
||||
throw std::runtime_error("Failed to create UNIX socket");
|
||||
}
|
||||
|
||||
this->server_addr_unix.sun_family = AF_UNIX;
|
||||
strncpy(this->server_addr_unix.sun_path, ip, sizeof(this->server_addr_unix.sun_path) - 1);
|
||||
|
||||
if (connect(sock_unix, (struct sockaddr *)&server_addr_unix, sizeof(server_addr_unix)) < 0) {
|
||||
throw std::runtime_error("Failed to connect to UNIX socket");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::send_server() {
|
||||
ssize_t sent_bytes = sendto(sock_server, &packet_data, sizeof(packet_data), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||
if (sent_bytes < 0) {
|
||||
throw runtime_error("Failed to send data");
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t Socket::receive_server(void *buffer, size_t len) {
|
||||
ssize_t recv_bytes = recv(sock_server, buffer, len, 0);
|
||||
if (recv_bytes < 0) {
|
||||
log_error("Failed to receive data from server socket\n");
|
||||
return -1;
|
||||
}
|
||||
return recv_bytes;
|
||||
}
|
||||
|
||||
void Socket::send_unix() {
|
||||
ssize_t sent_bytes = send(sock_unix, &packet_data, sizeof(packet_data), 0);
|
||||
if (sent_bytes < 0) {
|
||||
throw runtime_error("Failed to send data to UNIX socket");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::send_server(const api_packet &data) {
|
||||
ssize_t sent_bytes = sendto(sock_server, &data, sizeof(data), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||
if (sent_bytes < 0) {
|
||||
throw runtime_error("Failed to send data");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::send_server(const void *data, size_t len) {
|
||||
ssize_t sent_bytes = sendto(sock_server, data, len, 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||
if (sent_bytes < 0) {
|
||||
throw runtime_error("Failed to send data");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::send_unix(const api_packet &data) {
|
||||
ssize_t sent_bytes = send(sock_unix, &data, sizeof(data), 0);
|
||||
if (sent_bytes < 0) {
|
||||
throw runtime_error("Failed to send data to UNIX socket");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::set_packet(const api_packet &data) {
|
||||
packet_data = data;
|
||||
}
|
||||
|
||||
api_packet Socket::create_packet(uint8_t tracker_id) {
|
||||
api_packet pkt;
|
||||
memset(&pkt, 0, sizeof(pkt));
|
||||
|
||||
pkt.message_type = 65; // Handshake message type
|
||||
pkt.tracker_id = tracker_id;
|
||||
return pkt;
|
||||
}
|
||||
|
||||
api_packet Socket::get_packet() {
|
||||
return packet_data;
|
||||
}
|
||||
155
source/parcer.c
Normal file
155
source/parcer.c
Normal file
@ -0,0 +1,155 @@
|
||||
#include "parcer.h"
|
||||
#include "server_structs.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int ensure(size_t recv_len, size_t offset, size_t need) {
|
||||
return (offset + need <= recv_len);
|
||||
}
|
||||
|
||||
u_int8_t read_uint8(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
|
||||
if (!ensure(recv_len, *offset, sizeof(uint8_t))) {
|
||||
*ok = 0;
|
||||
return 0;
|
||||
}
|
||||
u_int8_t v;
|
||||
memcpy(&v, buf + *offset, sizeof(v));
|
||||
*offset += sizeof(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
u_int16_t read_uint16(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
|
||||
if (!ensure(recv_len, *offset, sizeof(uint16_t))) {
|
||||
*ok = 0;
|
||||
return 0;
|
||||
}
|
||||
u_int16_t v;
|
||||
memcpy(&v, buf + *offset, sizeof(v));
|
||||
*offset += sizeof(v);
|
||||
|
||||
return (u_int16_t)ntohs(v);
|
||||
}
|
||||
|
||||
u_int16_t read_uint16_le(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
|
||||
if (!ensure(recv_len, *offset, sizeof(uint16_t))) {
|
||||
*ok = 0;
|
||||
return 0;
|
||||
}
|
||||
u_int16_t v;
|
||||
memcpy(&v, buf + *offset, sizeof(v));
|
||||
*offset += sizeof(v);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
u_int32_t read_uint32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
|
||||
if (!ensure(recv_len, *offset, sizeof(uint32_t))) {
|
||||
*ok = 0;
|
||||
return 0;
|
||||
}
|
||||
u_int32_t v;
|
||||
memcpy(&v, buf + *offset, sizeof(v));
|
||||
*offset += sizeof(v);
|
||||
return (u_int32_t)ntohl(v);
|
||||
}
|
||||
|
||||
int32_t read_int32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
|
||||
if (!ensure(recv_len, *offset, sizeof(int32_t))) {
|
||||
*ok = 0;
|
||||
return 0;
|
||||
}
|
||||
int32_t v;
|
||||
memcpy(&v, buf + *offset, sizeof(v));
|
||||
*offset += sizeof(v);
|
||||
return (int32_t)ntohl((u_int32_t)v);
|
||||
}
|
||||
|
||||
float read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
|
||||
if (!ensure(recv_len, *offset, sizeof(float))) {
|
||||
*ok = 0;
|
||||
return 0.0f;
|
||||
}
|
||||
u_int32_t v_int;
|
||||
memcpy(&v_int, buf + *offset, sizeof(v_int));
|
||||
*offset += sizeof(v_int);
|
||||
v_int = ntohl(v_int);
|
||||
float v_float;
|
||||
memcpy(&v_float, &v_int, sizeof(v_float));
|
||||
return v_float;
|
||||
}
|
||||
|
||||
void read_bytes(const u_int8_t *buf, size_t recv_len, size_t *offset, u_int8_t *out, size_t len, int *ok) {
|
||||
if (!ensure(recv_len, *offset, len)) {
|
||||
*ok = 0;
|
||||
return;
|
||||
}
|
||||
memcpy(out, buf + *offset, len);
|
||||
*offset += len;
|
||||
}
|
||||
|
||||
void read_utf32le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok) {
|
||||
size_t i = *offset;
|
||||
size_t j = 0;
|
||||
|
||||
while (i + 3 < buf_size && j < max_len) {
|
||||
uint32_t codeunit = buffer[i] | (buffer[i + 1] << 8) | (buffer[i + 2] << 16) | (buffer[i + 3] << 24);
|
||||
|
||||
if (codeunit == 0) {
|
||||
i += 4; // termina a string
|
||||
break;
|
||||
}
|
||||
|
||||
if (codeunit < 0x80) {
|
||||
dest[j++] = (char)codeunit;
|
||||
} else {
|
||||
dest[j++] = '?'; // substitui caracteres fora de ASCII
|
||||
}
|
||||
|
||||
i += 4;
|
||||
}
|
||||
|
||||
dest[j] = '\0';
|
||||
*offset = i;
|
||||
*ok = 1;
|
||||
}
|
||||
|
||||
void read_utf16le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok) {
|
||||
size_t i = *offset;
|
||||
size_t j = 0;
|
||||
|
||||
while (i + 1 < buf_size && j < max_len - 1) {
|
||||
uint16_t codeunit = buffer[i] | (buffer[i + 1] << 8);
|
||||
|
||||
if (codeunit == 0) {
|
||||
i += 2; // termina a string
|
||||
break;
|
||||
}
|
||||
|
||||
if (codeunit < 0x80) {
|
||||
dest[j++] = (char)codeunit;
|
||||
} else {
|
||||
dest[j++] = '?'; // substitui caracteres fora de ASCII
|
||||
}
|
||||
|
||||
i += 2;
|
||||
}
|
||||
|
||||
dest[j] = '\0';
|
||||
*offset = i;
|
||||
*ok = 1;
|
||||
}
|
||||
|
||||
void read_string(const u_int8_t *buf, size_t recv_len, size_t *offset, char *out, size_t len, int *ok) {
|
||||
if (!ensure(recv_len, *offset, len)) {
|
||||
*ok = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i <= len; i++) {
|
||||
out[i] = (char)buf[*offset + i];
|
||||
}
|
||||
out[len] = '\0'; // Ensure null termination
|
||||
*offset += len;
|
||||
*ok = 1;
|
||||
}
|
||||
116
source/session.cpp
Normal file
116
source/session.cpp
Normal file
@ -0,0 +1,116 @@
|
||||
#include "session.hpp"
|
||||
|
||||
void SessionManager::on_new_session(const trackAtributes &track) {
|
||||
track_info = track;
|
||||
}
|
||||
|
||||
void SessionManager::on_player_connected(const carAtributes &car) {
|
||||
if (car.carID < MAX_PLAYERS) {
|
||||
players[car.carID] = car;
|
||||
players[car.carID].isConnected = 1;
|
||||
players[car.carID].isLoading = 1;
|
||||
connected_players++;
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManager::on_player_finished_loading(u_int8_t car_id) {
|
||||
if (car_id < MAX_PLAYERS) {
|
||||
players[car_id].isLoading = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManager::on_player_disconnected(u_int8_t car_id) {
|
||||
if (car_id < MAX_PLAYERS) {
|
||||
memset(&players[car_id], 0, sizeof(carAtributes));
|
||||
connected_players--;
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManager::on_car_update(const carAtributes &car) {
|
||||
if (car.carID < MAX_PLAYERS) {
|
||||
players[car.carID] = car;
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManager::on_lap_completed(u_int8_t car_id, u_int32_t lap_time, u_int32_t cuts) {
|
||||
if (car_id < MAX_PLAYERS) {
|
||||
players[car_id].lap_time = lap_time;
|
||||
players[car_id].cuts += cuts;
|
||||
}
|
||||
}
|
||||
|
||||
void SessionManager::on_collision(u_int8_t car1, u_int8_t car2) {
|
||||
if (car1 < MAX_PLAYERS) {
|
||||
players[car1].contacts++;
|
||||
}
|
||||
if (car2 < MAX_PLAYERS) {
|
||||
players[car2].contacts++;
|
||||
}
|
||||
}
|
||||
|
||||
api_packet SessionManager::build_packet(u_int8_t message_type) {
|
||||
api_packet pkt;
|
||||
memset(&pkt, 0, sizeof(pkt));
|
||||
|
||||
pkt.message_type = message_type;
|
||||
pkt.tracker_id = server_id;
|
||||
pkt.connected_players = connected_players;
|
||||
|
||||
for (u_int8_t i = 0; i < MAX_PLAYERS; i++) {
|
||||
pkt.cars[i].carID = players[i].carID;
|
||||
pkt.cars[i].position = players[i].position;
|
||||
pkt.cars[i].velocity = players[i].velocity;
|
||||
pkt.cars[i].carGear = players[i].carGear;
|
||||
pkt.cars[i].carRPM = players[i].carRPM;
|
||||
pkt.cars[i].lap_time = players[i].lap_time;
|
||||
pkt.cars[i].cuts = players[i].cuts;
|
||||
pkt.cars[i].total_cuts = players[i].total_cuts;
|
||||
pkt.cars[i].total_cuts_alltime = players[i].total_cuts_alltime; // TAG: Kinda useless
|
||||
pkt.cars[i].total_laps_completed = players[i].total_laps_completed;
|
||||
pkt.cars[i].contacts = players[i].contacts;
|
||||
pkt.cars[i].total_contacts = players[i].total_contacts;
|
||||
|
||||
pkt.cars[i].current_flag = players[i].current_flag;
|
||||
pkt.cars[i].normalizedSplinePos = players[i].normalizedSplinePos;
|
||||
|
||||
pkt.cars[i].isConnected = players[i].isConnected;
|
||||
pkt.cars[i].isLoading = players[i].isLoading;
|
||||
|
||||
strncpy(pkt.cars[i].car_model, players[i].car_model, sizeof(pkt.cars[i].car_model));
|
||||
strncpy(pkt.cars[i].car_skin, players[i].car_skin, sizeof(pkt.cars[i].car_skin));
|
||||
strncpy(pkt.cars[i].driver_name, players[i].driver_name, sizeof(pkt.cars[i].driver_name));
|
||||
strncpy(pkt.cars[i].driver_team, players[i].driver_team, sizeof(pkt.cars[i].driver_team));
|
||||
strncpy(pkt.cars[i].driver_GUID, players[i].driver_GUID, sizeof(pkt.cars[i].driver_GUID));
|
||||
}
|
||||
|
||||
// TAG: Also kinda useless to send track protocol
|
||||
pkt.track_info.protocol_version = track_info.protocol_version;
|
||||
|
||||
pkt.track_info.session_index = track_info.session_index;
|
||||
pkt.track_info.current_session_index = track_info.current_session_index;
|
||||
pkt.track_info.session_count = track_info.session_count;
|
||||
pkt.track_info.session_type = track_info.session_type;
|
||||
|
||||
strncpy(pkt.track_info.server_name, track_info.server_name, sizeof(pkt.track_info.server_name));
|
||||
strncpy(pkt.track_info.track, track_info.track, sizeof(pkt.track_info.track));
|
||||
strncpy(pkt.track_info.track_config, track_info.track_config, sizeof(pkt.track_info.track_config));
|
||||
strncpy(pkt.track_info.session_name, track_info.session_name, sizeof(pkt.track_info.session_name));
|
||||
|
||||
pkt.track_info.typ = track_info.typ;
|
||||
pkt.track_info.time = track_info.time;
|
||||
pkt.track_info.laps = track_info.laps;
|
||||
pkt.track_info.wait_time = track_info.wait_time;
|
||||
pkt.track_info.ambient_temp = track_info.ambient_temp;
|
||||
pkt.track_info.road_temp = track_info.road_temp;
|
||||
|
||||
strncpy(pkt.track_info.weather_graphics, track_info.weather_graphics, sizeof(pkt.track_info.weather_graphics));
|
||||
|
||||
pkt.track_info.elapsed_ms = track_info.elapsed_ms;
|
||||
|
||||
return pkt;
|
||||
}
|
||||
|
||||
SessionManager::SessionManager(u_int8_t sid) : server_id(sid), connected_players(0) {
|
||||
memset(&track_info, 0, sizeof(trackAtributes));
|
||||
memset(players, 0, sizeof(players));
|
||||
}
|
||||
90
source/socket.c
Normal file
90
source/socket.c
Normal file
@ -0,0 +1,90 @@
|
||||
#include "socket.h"
|
||||
|
||||
// =========================
|
||||
// UDP SOCKET FUCNTIONS
|
||||
// =========================
|
||||
|
||||
// Create a UDP socket
|
||||
// @return: Socket file descriptor, or -1 on error
|
||||
int create_udp_socket(void) {
|
||||
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0) {
|
||||
perror("[!] socket() failed");
|
||||
return -1;
|
||||
}
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
// Create a UDP socket and connect it to a specific IP and port
|
||||
// Used for sending data (no bind needed)
|
||||
// @param ip: Destination IP address as a string
|
||||
// @return: Socket file descriptor, or -1 on error
|
||||
int connect_udp_socket(const char *ip, uint16_t port) {
|
||||
int sockfd = create_udp_socket();
|
||||
if (sockfd < 0)
|
||||
return -1;
|
||||
|
||||
struct sockaddr_in servaddr;
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_port = htons(port);
|
||||
servaddr.sin_addr.s_addr = inet_addr(ip);
|
||||
|
||||
if (connect(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
|
||||
perror("[!] connect() failed");
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("[+] Connected UDP socket to %s:%d\n", ip, port);
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
// Create and bind a UDP socket to a local IP and port
|
||||
// Used for listening for incoming data
|
||||
// @param ip: Local IP address to bind to
|
||||
// @param port: Local port to bind to
|
||||
// @return: Socket file descriptor, or -1 on error
|
||||
int create_bound_udp_socket(const char *ip, uint16_t port) {
|
||||
int sockfd = create_udp_socket();
|
||||
if (sockfd < 0)
|
||||
return -1;
|
||||
|
||||
struct sockaddr_in addr;
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
addr.sin_addr.s_addr = inet_addr(ip);
|
||||
|
||||
if (bind(sockfd, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||
perror("[!] bind() failed");
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("[+] Bound UDP socket on %s:%d\n", ip, port);
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
// Send a UDP message to the specified IP and port
|
||||
// @param sockfd: Socket file descriptor
|
||||
// @param message: Message to send
|
||||
// @param dest_ip: Destination IP address as a string
|
||||
// @param dest_port: Destination port
|
||||
// @return: Number of bytes sent, or -1 on error
|
||||
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, uint16_t dest_port) {
|
||||
struct sockaddr_in destaddr;
|
||||
memset(&destaddr, 0, sizeof(destaddr));
|
||||
destaddr.sin_family = AF_INET;
|
||||
destaddr.sin_port = htons(dest_port);
|
||||
destaddr.sin_addr.s_addr = inet_addr(dest_ip);
|
||||
|
||||
ssize_t n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr *)&destaddr, sizeof(destaddr));
|
||||
if (n < 0) {
|
||||
perror("[!] sendto() failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("[+] Sent %zd bytes to %s:%d\n", n, dest_ip, dest_port);
|
||||
return n;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user