generated from AfonsoCMSousa/CPP-Template
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| eb7624da8b | |||
| eafd600b19 | |||
| 8c6577bb2e | |||
| 32b496b596 | |||
| 48091f826e | |||
| 3fa53d741c | |||
| 3e96227be7 | |||
| 64a81b7760 | |||
| 73ffb9d8d2 | |||
| 95f5652c8d | |||
| c5c9e22ed6 | |||
| 9449749b9b |
BIN
AC_Telemetry_FullStackInfo.pdf
Normal file
BIN
AC_Telemetry_FullStackInfo.pdf
Normal file
Binary file not shown.
BIN
PlayerTracker
BIN
PlayerTracker
Binary file not shown.
14
build.sh
14
build.sh
@ -18,6 +18,20 @@ 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
|
||||
done
|
||||
|
||||
echo ">>> Build Complete <<<"
|
||||
echo ">>> Executable: $1 <<<"
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
#include "parcer.h"
|
||||
#include "server_structs.h"
|
||||
#include <sys/types.h>
|
||||
|
||||
int ensure(size_t recv_len, size_t offset, size_t need) {
|
||||
@ -39,28 +40,28 @@ u_int32_t read_uint32(const u_int8_t *buf, size_t recv_len, size_t *offset, int
|
||||
}
|
||||
|
||||
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);
|
||||
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;
|
||||
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) {
|
||||
@ -138,4 +139,3 @@ void read_string(const u_int8_t *buf, size_t recv_len, size_t *offset, char *out
|
||||
*ok = 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -13,6 +13,8 @@ extern "C" {
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "server_structs.h"
|
||||
|
||||
// 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);
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
#ifndef SERVER_STRUCTS_H
|
||||
#define SERVER_STRUCTS_H
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
struct handshake {
|
||||
#define MAX_PLAYERS 64
|
||||
const int MAX_TELEMETRY_CLIENTS = 128;
|
||||
|
||||
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))
|
||||
@ -31,9 +32,9 @@ struct handshake {
|
||||
// DISMISS = 3 :
|
||||
// This operation identifier must be set when the client wants to leave the comunication with ACServer.
|
||||
int32_t operationId;
|
||||
} __attribute__((packed));
|
||||
} __attribute__((packed)) handshake;
|
||||
|
||||
struct handshackerResponse {
|
||||
typedef struct handshackerResponse {
|
||||
// is the name of the car that the player is driving on the AC Server
|
||||
char carName[50];
|
||||
|
||||
@ -52,26 +53,26 @@ struct handshackerResponse {
|
||||
|
||||
// is the track configuration on the AC Server
|
||||
char trackConfig[50];
|
||||
} __attribute__((packed));
|
||||
} __attribute__((packed)) handshackerResponse;
|
||||
|
||||
struct postion {
|
||||
typedef struct postion {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
} __attribute__((packed));
|
||||
} __attribute__((packed)) postion;
|
||||
|
||||
enum flag {
|
||||
NO_FLAG = 0,
|
||||
YELLOW_FLAG = 1,
|
||||
BLUE_FLAG = 2,
|
||||
BLACK_FLAG = 3,
|
||||
CHECKERED_FLAG = 4,
|
||||
} __attribute__((packed));
|
||||
typedef enum flag {
|
||||
NO_FLAG = 0,
|
||||
YELLOW_FLAG = 1,
|
||||
BLUE_FLAG = 2,
|
||||
BLACK_FLAG = 3,
|
||||
CHECKERED_FLAG = 4,
|
||||
} __attribute__((packed)) flag;
|
||||
|
||||
struct carAtributes {
|
||||
typedef struct carAtributes {
|
||||
// Related to ACSP_CAR_INFO
|
||||
u_char isConnected; // 1 = connected, 0 = disconnected
|
||||
u_char isLoading; // 1 = loading, 0 = not isLoading
|
||||
u_char isLoading; // 1 = loading, 0 = not isLoading
|
||||
|
||||
char *car_model;
|
||||
char *car_skin;
|
||||
@ -85,27 +86,58 @@ struct carAtributes {
|
||||
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;
|
||||
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
|
||||
flag current_flag; // TODO: implement flag status updates
|
||||
// TAG:3
|
||||
|
||||
float_t normalizedSplinePos;
|
||||
} __attribute__((packed));
|
||||
float normalizedSplinePos;
|
||||
} __attribute__((packed)) carAtributes;
|
||||
|
||||
enum SessionType {
|
||||
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;
|
||||
|
||||
struct trackAtributes {
|
||||
typedef struct trackAtributes {
|
||||
u_int8_t protocol_version;
|
||||
|
||||
u_int8_t session_index;
|
||||
@ -127,6 +159,64 @@ struct trackAtributes {
|
||||
|
||||
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_char message_type; // ACSP_MessageType
|
||||
u_int8_t tracker_id;
|
||||
u_int8_t connected_players;
|
||||
|
||||
u_int8_t cars_colided[2]; // IDs of cars involved in a collision event IF [1] == 255, [0] is the only car involved (environment collision)
|
||||
|
||||
carAtributesAPI cars[64];
|
||||
trackAtributesAPI track_info;
|
||||
|
||||
} __attribute__((packed)) api_packet;
|
||||
|
||||
// Telemetry packet structure (lightweight for streaming)
|
||||
struct telemetry_packet {
|
||||
u_int8_t server_id;
|
||||
u_int8_t car_count;
|
||||
|
||||
struct car_telemetry {
|
||||
u_int8_t carID;
|
||||
char driver_name[64];
|
||||
char driver_guid[64];
|
||||
char car_model[64];
|
||||
postion position;
|
||||
float normalizedSplinePos;
|
||||
float speed_kmh;
|
||||
u_int8_t gear;
|
||||
u_int16_t rpm;
|
||||
u_int32_t last_lap_time;
|
||||
u_int32_t best_lap_time;
|
||||
u_int16_t current_lap;
|
||||
u_int8_t position_rank; // RENAME: position -> position_rank to avoid conflict
|
||||
} __attribute__((packed)) cars[64];
|
||||
} __attribute__((packed));
|
||||
|
||||
enum ACSP_MessageType {
|
||||
|
||||
313
source/main.cpp
313
source/main.cpp
@ -1,26 +1,162 @@
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <exception>
|
||||
#include <iterator>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "parcer.h"
|
||||
#include "server_structs.h"
|
||||
#include "socket.h"
|
||||
|
||||
#define DEBUG_CAR_INFO 0
|
||||
#define DEBUG_CAR_INFO 1
|
||||
|
||||
const char *API_SOCKET_PATH = "/tmp/ACplayer_socket";
|
||||
|
||||
const int SERVER_OUT_PORT = 12000;
|
||||
const int SERVER_IN_PORT = 11000;
|
||||
const char *SERVER_OUT_IP = "127.0.0.1";
|
||||
|
||||
const int MAX_PLAYERS = 64;
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
@ -57,18 +193,32 @@ void init_carupdate(carAtributes *car) {
|
||||
|
||||
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->total_contacts = 0; // TODO: SQL querry to get total contacts of all time; TAG:2
|
||||
|
||||
car->normalizedSplinePos = 0.0f;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
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]);
|
||||
|
||||
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);
|
||||
|
||||
int send_sock_fd = connect_udp_socket("127.0.0.1", 11000); // SEND requests to Server
|
||||
int recv_sock_fd = create_bound_udp_socket("0.0.0.0", 12000); // LISTEN to server updates
|
||||
current_packet.tracker_id = (u_int8_t)-1; // Mark as uninitialized
|
||||
|
||||
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
|
||||
|
||||
if (send_sock_fd < 0 || recv_sock_fd < 0) {
|
||||
fprintf(stderr, "[!] Failed to create sockets (%d)(%d)\n", send_sock_fd, recv_sock_fd);
|
||||
@ -79,15 +229,26 @@ int main(void) {
|
||||
int ok = 1;
|
||||
char buffer[1024];
|
||||
|
||||
// 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);
|
||||
@ -97,7 +258,12 @@ int main(void) {
|
||||
trackInfo.session_name = (char *)malloc(64);
|
||||
|
||||
char message[124] = {0}; // just a place to put info messages from the server itself
|
||||
u_char update_rate_message[2] = {0};
|
||||
|
||||
// EXPERIMENTAL: Send a message to the server to increase update rate - USING ACSP_REALTIMEPOS_INTERVAL
|
||||
update_rate_message[0] = (u_char)ACSP_REALTIMEPOS_INTERVAL;
|
||||
update_rate_message[1] = 255; // ms
|
||||
u_char updateSent = 0;
|
||||
|
||||
printf("\n");
|
||||
while (1) {
|
||||
@ -106,15 +272,22 @@ int main(void) {
|
||||
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);
|
||||
size_t recv_bytes = (size_t)recvfrom(recv_sock_fd, buffer, sizeof(buffer), 0, NULL, NULL);
|
||||
|
||||
// Send update rate message to server ONCE
|
||||
if (updateSent != 1) {
|
||||
sendto(send_sock_fd, update_rate_message, sizeof(update_rate_message), 0, NULL, 0);
|
||||
updateSent = 1;
|
||||
}
|
||||
|
||||
// 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 ((int)buffer[0]) {
|
||||
switch ((u_char)buffer[0]) {
|
||||
// ============================
|
||||
// SERVER → CLIENT MESSAGES
|
||||
// ============================
|
||||
@ -178,6 +351,7 @@ int main(void) {
|
||||
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:
|
||||
@ -204,7 +378,8 @@ int main(void) {
|
||||
}
|
||||
|
||||
update.isConnected = 1; // Mark as connected
|
||||
update.isLoading = 1; // Mark as loading
|
||||
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);
|
||||
@ -214,9 +389,23 @@ int main(void) {
|
||||
|
||||
// Store player player into the respective Index
|
||||
if (update.carID < MAX_PLAYERS) {
|
||||
memcpy(&players[update.carID], &update, sizeof(carAtributes));
|
||||
}
|
||||
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:
|
||||
@ -243,6 +432,10 @@ int main(void) {
|
||||
}
|
||||
|
||||
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);
|
||||
@ -252,9 +445,10 @@ int main(void) {
|
||||
|
||||
// Store player player into the respective Index
|
||||
if (update.carID < MAX_PLAYERS) {
|
||||
memcpy(&players[update.carID], &update, sizeof(carAtributes));
|
||||
players[update.carID].isConnected = 0; // Mark disconnected
|
||||
players[update.carID].isLoading = 0;
|
||||
}
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
case ACSP_CAR_UPDATE: // DONE:
|
||||
@ -263,7 +457,7 @@ int main(void) {
|
||||
#endif
|
||||
offset = 1;
|
||||
|
||||
memcpy(&update.carID, buffer, sizeof(uint8_t));
|
||||
memcpy(&update.carID, buffer + offset, sizeof(uint8_t));
|
||||
offset = 1 + sizeof(uint8_t);
|
||||
memcpy(&update.position, buffer + offset, sizeof(postion));
|
||||
offset += sizeof(postion);
|
||||
@ -281,15 +475,20 @@ int main(void) {
|
||||
printf("Gear: %d RPM: %d\n", update.carGear, update.carRPM);
|
||||
#endif
|
||||
|
||||
// Store player player into the respective index
|
||||
// Store player player into the respective Index
|
||||
if (update.carID < MAX_PLAYERS) {
|
||||
memcpy(&players[update.carID].position, &update.position, sizeof(postion));
|
||||
memcpy(&players[update.carID].velocity, &update.velocity, sizeof(postion));
|
||||
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:
|
||||
@ -324,9 +523,25 @@ int main(void) {
|
||||
|
||||
// Store player player into the respective Index
|
||||
if (update.carID < MAX_PLAYERS) {
|
||||
memcpy(&players[update.carID], &update, sizeof(carAtributes));
|
||||
|
||||
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)
|
||||
@ -346,6 +561,7 @@ int main(void) {
|
||||
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:
|
||||
@ -355,6 +571,7 @@ int main(void) {
|
||||
|
||||
printf("\tProtocol Version: %d\n\n", trackInfo.protocol_version);
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
case ACSP_CHAT: // DONE: Receive chat messages
|
||||
@ -369,6 +586,7 @@ int main(void) {
|
||||
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
|
||||
@ -380,6 +598,7 @@ int main(void) {
|
||||
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
|
||||
@ -399,17 +618,24 @@ int main(void) {
|
||||
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 server
|
||||
// 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);
|
||||
|
||||
current_packet.message_type = ACSP_LAP_COMPLETED;
|
||||
|
||||
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);
|
||||
|
||||
current_packet.cars_colided[0] = update.carID;
|
||||
players[update.carID].total_laps_completed++;
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
break;
|
||||
|
||||
// ============================
|
||||
@ -440,6 +666,11 @@ int main(void) {
|
||||
// TODO: Update total contacts for both players in the database
|
||||
// TAG:2 Update total contacts for both players
|
||||
|
||||
current_packet.message_type = ACSP_CE_COLLISION_WITH_CAR;
|
||||
current_packet.cars_colided[0] = update.carID;
|
||||
current_packet.cars_colided[1] = event_car_id;
|
||||
|
||||
update_api_packet(trackInfo, players);
|
||||
} break;
|
||||
|
||||
case ACSP_CE_COLLISION_WITH_ENV: {
|
||||
@ -451,22 +682,45 @@ int main(void) {
|
||||
|
||||
// TODO: Update total contacts for the player in the database
|
||||
// TAG:2 Update total contacts for the player
|
||||
|
||||
current_packet.message_type = ACSP_CE_COLLISION_WITH_ENV;
|
||||
current_packet.cars_colided[0] = update.carID;
|
||||
|
||||
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};
|
||||
world_pos.x = 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);
|
||||
*/
|
||||
|
||||
postion rel_pos = {0};
|
||||
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);
|
||||
@ -479,7 +733,7 @@ int main(void) {
|
||||
//
|
||||
// ============================
|
||||
// CLIENT → SERVER COMMANDS
|
||||
// NOTE: These are not meant to be here, these are comands to SEND to the server
|
||||
// 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");
|
||||
@ -517,19 +771,19 @@ int main(void) {
|
||||
printf("[+] Command: ACSP_RESTART_SESSION\n");
|
||||
break;
|
||||
|
||||
case ACSP_ADMIN_COMMAND: { // DONE:
|
||||
case ACSP_ADMIN_COMMAND: { // DONE:
|
||||
printf("[+] Command: ACSP_ADMIN_COMMAND\n");
|
||||
char *buffer = (char *)malloc(256);
|
||||
char *__buffer = (char *)malloc(256);
|
||||
char command[256] = "/settime 19:00"; // TEST:
|
||||
|
||||
buffer[0] = (char)ACSP_ADMIN_COMMAND;
|
||||
__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);
|
||||
free(__buffer);
|
||||
} break;
|
||||
|
||||
// ============================
|
||||
@ -540,6 +794,7 @@ int main(void) {
|
||||
break;
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user