Feature: API can now handle write and read to the DB

This commit is contained in:
2025-10-21 19:00:55 +01:00
parent 524405d80d
commit 2d3f52a9a5
8 changed files with 587 additions and 8 deletions
+244
View File
@@ -0,0 +1,244 @@
#ifndef SERVER_STRUCTS_H
#define SERVER_STRUCTS_H
#include <sys/cdefs.h>
#include <sys/types.h>
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.
// This will be used to adjust a specific behaviour for each platform. (eIPadDevice for now (1))
int32_t identifier;
// [not used in the current Remote Telemtry version by AC]
// In future version this field will identify the AC Remote Telemetry version that the device expects to speak with.
int32_t version;
// This is the type of operation required by the client.
// The following operations are now available:
// ----------------------------------------------------------------
// HANDSHAKE = 0 :
// This operation identifier must be set when the client wants to start the comunication.
//
// SUBSCRIBE_UPDATE = 1 :
// This operation identifier must be set when the client wants to be updated from the specific ACServer.
//
// SUBSCRIBE_SPOT = 2 :
// This operation identifier must be set when the client wants to be updated from the specific ACServer just for SPOT Events (e.g.: the end of a lap).
//
// DISMISS = 3 :
// This operation identifier must be set when the client wants to leave the comunication with ACServer.
int32_t operationId;
} __attribute__((packed)) handshake;
typedef struct handshackerResponse {
// is the name of the car that the player is driving on the AC Server
char carName[50];
// is the name of the driver running on the AC Server
char driverName[50];
// for now is just 4242, this code will identify different status,
// as “NOT AVAILABLE” for connection
int32_t identifier;
// for now is set to 1, this will identify the version running on the AC Server
int32_t version;
// is the name of the track on the AC Server
char trackName[50];
// is the track configuration on the AC Server
char trackConfig[50];
} __attribute__((packed)) handshackerResponse;
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
char *car_model;
char *car_skin;
char *driver_name;
char *driver_team;
char *driver_GUID;
// 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 struct carAtributesAPI {
// Related to ACSP_CAR_INFO
u_char isConnected; // 1 = connected, 0 = disconnected
u_char isLoading; // 1 = loading, 0 = not isLoading
char car_model[64];
char car_skin[64];
char driver_name[64];
char driver_team[64];
char 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)) carAtributesAPI;
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;
char *server_name;
char *track;
char *track_config;
char *session_name;
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;
char *weather_graphics;
u_int32_t elapsed_ms;
} __attribute__((packed)) trackAtributes;
typedef struct trackAtributesAPI {
u_int8_t protocol_version;
u_int8_t session_index;
u_int8_t current_session_index;
u_int8_t session_count;
SessionType session_type;
char server_name[128];
char track[64];
char track_config[64];
char 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;
char weather_graphics[64];
u_int32_t elapsed_ms;
} __attribute__((packed)) trackAtributesAPI;
typedef struct api_packet {
u_int8_t tracker_id;
u_int8_t last_updated_carid;
u_int8_t connected_cars;
carAtributesAPI cars[64];
trackAtributesAPI 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
+48
View File
@@ -0,0 +1,48 @@
#include "stack.h"
#include <cstdio>
Stack::Stack(size_t size) {
capacity = size;
data = new api_packet[capacity];
top = -1;
}
Stack::~Stack() {
delete[] data;
}
bool Stack::isEmpty() {
return top == -1;
}
bool Stack::isFull() {
return top == capacity - 1;
}
bool Stack::push(const api_packet &item) {
if (isFull()) {
return false; // Stack overflow
}
data[++top] = item;
return true;
}
bool Stack::pop(api_packet &item) {
if (isEmpty()) {
return false; // Stack underflow
}
item = data[top--];
return true;
}
bool Stack::peek(api_packet &item) {
if (isEmpty()) {
return false; // Stack is empty
}
item = data[top];
return true;
}
size_t Stack::size() {
return top + 1;
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef STACK_H
#define STACK_H // STACK_H
#include <stdbool.h>
#include <sys/types.h>
#include "server_structs.h"
class Stack {
private:
api_packet *data;
size_t capacity;
int top;
public:
Stack(size_t size);
~Stack();
bool isEmpty();
bool isFull();
bool push(const api_packet &item);
bool pop(api_packet &item);
bool peek(api_packet &item);
size_t size();
};
#endif // STACK_H