fix: added executable to the gitignore

"
This commit is contained in:
Afonso Clerigo Mendes de Sousa 2026-03-12 20:22:22 +00:00
parent c40075ca94
commit 4f37834e20
9 changed files with 690 additions and 1 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.a *.a
*.log *.log
*.so *.so
@ -8,3 +9,5 @@
/.DS_store /.DS_store
/.env /.env
/imgui.ini /imgui.ini
PlayerTracker

Binary file not shown.

View File

@ -1 +1 @@
/home/afonsocmsousa/Developer/ac_sevice/ACPlayer_tracker/build/compile_commands.json /Users/AfonsoCMSosua/Developer/C++/ACPlayer_tracker/build/compile_commands.json

193
include/handle.hpp Normal file
View 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

141
include/parcer.c.old Normal file
View File

@ -0,0 +1,141 @@
#include "parcer.h"
#include "server_structs.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_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(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;
}

62
include/parcer.h.old Normal file
View 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

158
include/server_structs.hpp Normal file
View 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

90
include/socket.c.old Normal file
View 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;
}

42
include/socket.h.old Normal file
View 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