generated from AfonsoCMSousa/CPP-Template
Compare commits
22 Commits
WindowsBui
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| eb7624da8b | |||
| eafd600b19 | |||
| 8c6577bb2e | |||
| 32b496b596 | |||
| 48091f826e | |||
| 3fa53d741c | |||
| 3e96227be7 | |||
| 64a81b7760 | |||
| 73ffb9d8d2 | |||
| 95f5652c8d | |||
| c5c9e22ed6 | |||
| 9449749b9b | |||
| e94afdfd9b | |||
| 650977cfb9 | |||
| 67d51489f0 | |||
| 8bf502a237 | |||
| f5bf01b1f8 | |||
| d38dbac8f4 | |||
| 2df0838be8 | |||
| 1fe0452ad6 | |||
| 529469e2e3 | |||
| 42df9f79fe |
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.
Binary file not shown.
14
build.sh
14
build.sh
@ -18,6 +18,20 @@ if [ $? -ne 0 ]; then
|
|||||||
echo "Error: Build failed"
|
echo "Error: Build failed"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cp ./bin/$1 ../
|
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 ">>> Build Complete <<<"
|
||||||
echo ">>> Executable: $1 <<<"
|
echo ">>> Executable: $1 <<<"
|
||||||
|
|
||||||
|
|||||||
141
include/parcer.c
Normal file
141
include/parcer.c
Normal 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
Normal file
62
include/parcer.h
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.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);
|
||||||
|
|
||||||
|
// 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
|
||||||
@ -1,8 +1,13 @@
|
|||||||
#ifndef SERVER_STRUCTS_H
|
#ifndef SERVER_STRUCTS_H
|
||||||
#define SERVER_STRUCTS_H
|
#define SERVER_STRUCTS_H
|
||||||
|
|
||||||
#include <cstdint>
|
#include <sys/cdefs.h>
|
||||||
struct handshake {
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#define MAX_PLAYERS 64
|
||||||
|
const int MAX_TELEMETRY_CLIENTS = 128;
|
||||||
|
|
||||||
|
typedef struct handshake {
|
||||||
// [not used in the current Remote Telemtry version by AC]
|
// [not used in the current Remote Telemtry version by AC]
|
||||||
// In future versions it will identify the platform type of the client.
|
// 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))
|
// This will be used to adjust a specific behaviour for each platform. (eIPadDevice for now (1))
|
||||||
@ -27,9 +32,9 @@ struct handshake {
|
|||||||
// DISMISS = 3 :
|
// DISMISS = 3 :
|
||||||
// This operation identifier must be set when the client wants to leave the comunication with ACServer.
|
// This operation identifier must be set when the client wants to leave the comunication with ACServer.
|
||||||
int32_t operationId;
|
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
|
// is the name of the car that the player is driving on the AC Server
|
||||||
char carName[50];
|
char carName[50];
|
||||||
|
|
||||||
@ -48,6 +53,218 @@ struct handshackerResponse{
|
|||||||
|
|
||||||
// is the track configuration on the AC Server
|
// is the track configuration on the AC Server
|
||||||
char trackConfig[50];
|
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_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));
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
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
|
#endif // SERVER_STRUCTS_H
|
||||||
|
|||||||
108
include/socket.c
108
include/socket.c
@ -1,84 +1,90 @@
|
|||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int connect_udp_socket(const char* ip, uint16_t port) {
|
// =========================
|
||||||
#ifdef _WIN32
|
// UDP SOCKET FUCNTIONS
|
||||||
static int wsa_initialized = 0;
|
// =========================
|
||||||
if (!wsa_initialized) {
|
|
||||||
WSADATA wsaData;
|
|
||||||
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
|
|
||||||
fprintf(stderr, "WSAStartup failed\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
wsa_initialized = 1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
// 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);
|
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (sockfd < 0) {
|
if (sockfd < 0) {
|
||||||
perror("socket creation failed");
|
perror("[!] socket() failed");
|
||||||
return -1;
|
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("connection to the server failed");
|
|
||||||
CLOSESOCKET(sockfd);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sockfd;
|
return sockfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bind_udp_socket(int sockfd, const char* ip, uint16_t port) {
|
// 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;
|
struct sockaddr_in servaddr;
|
||||||
memset(&servaddr, 0, sizeof(servaddr));
|
memset(&servaddr, 0, sizeof(servaddr));
|
||||||
|
|
||||||
servaddr.sin_family = AF_INET;
|
servaddr.sin_family = AF_INET;
|
||||||
servaddr.sin_addr.s_addr = inet_addr(ip);
|
|
||||||
servaddr.sin_port = htons(port);
|
servaddr.sin_port = htons(port);
|
||||||
|
servaddr.sin_addr.s_addr = inet_addr(ip);
|
||||||
|
|
||||||
if (bind(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
|
if (connect(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
|
||||||
perror("bind failed");
|
perror("[!] connect() failed");
|
||||||
CLOSESOCKET(sockfd);
|
close(sockfd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
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) {
|
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, uint16_t dest_port) {
|
||||||
struct sockaddr_in destaddr;
|
struct sockaddr_in destaddr;
|
||||||
memset(&destaddr, 0, sizeof(destaddr));
|
memset(&destaddr, 0, sizeof(destaddr));
|
||||||
|
|
||||||
destaddr.sin_family = AF_INET;
|
destaddr.sin_family = AF_INET;
|
||||||
destaddr.sin_port = htons(dest_port);
|
destaddr.sin_port = htons(dest_port);
|
||||||
destaddr.sin_addr.s_addr = inet_addr(dest_ip);
|
destaddr.sin_addr.s_addr = inet_addr(dest_ip);
|
||||||
|
|
||||||
#ifdef _WIN32
|
ssize_t n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr *)&destaddr, sizeof(destaddr));
|
||||||
int n = sendto(sockfd, message, (int)strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr));
|
|
||||||
#else
|
|
||||||
ssize_t n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
perror("sendto failed");
|
perror("[!] sendto() failed");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("[+] Sent %zd bytes to %s:%d\n", n, dest_ip, dest_port);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
void cleanup_sockets() {
|
|
||||||
WSACleanup();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|||||||
@ -5,24 +5,20 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#pragma comment(lib, "ws2_32.lib")
|
#pragma comment(lib, "ws2_32.lib") // link Winsock
|
||||||
#define CLOSESOCKET closesocket
|
|
||||||
#else
|
#else
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
#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>
|
#include <unistd.h>
|
||||||
#define CLOSESOCKET close
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// =========================
|
// =========================
|
||||||
@ -31,12 +27,13 @@ extern "C" {
|
|||||||
// Server hosts a UDP socket at 127.0.0.1:12000
|
// Server hosts a UDP socket at 127.0.0.1:12000
|
||||||
// Client sends a message to the server at 11000
|
// 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 connect_udp_socket(const char *ip, uint16_t port);
|
||||||
|
int create_bound_udp_socket(const char *ip, uint16_t port);
|
||||||
|
|
||||||
int bind_udp_socket(int sockfd, 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);
|
||||||
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip,
|
|
||||||
uint16_t dest_port);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
862
source/main.cpp
862
source/main.cpp
@ -1,108 +1,824 @@
|
|||||||
#include <cstdint>
|
#include <cmath>
|
||||||
#include <ctype.h>
|
#include <cstddef>
|
||||||
#include <iostream>
|
#include <cstdlib>
|
||||||
|
#include <exception>
|
||||||
|
#include <iterator>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <winsock2.h>
|
|
||||||
#include <ws2tcpip.h>
|
|
||||||
#pragma comment(lib, "ws2_32.lib") // only needed for MSVC
|
|
||||||
#else
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <sys/un.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
|
#include "parcer.h"
|
||||||
#include "server_structs.h"
|
#include "server_structs.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
|
|
||||||
// Cross-platform close macro
|
#define DEBUG_CAR_INFO 1
|
||||||
#ifdef _WIN32
|
|
||||||
#define CLOSESOCKET closesocket
|
|
||||||
#else
|
|
||||||
#define CLOSESOCKET close
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const int SERVER_OUT_PORT = 9996;
|
const char *API_SOCKET_PATH = "/tmp/ACplayer_socket";
|
||||||
const int SERVER_IN_PORT = 11000;
|
|
||||||
const char* SERVER_OUT_IP = "127.0.0.1";
|
|
||||||
|
|
||||||
int main() {
|
const char *SERVER_OUT_IP = "127.0.0.1";
|
||||||
|
|
||||||
#ifdef _WIN32
|
u_int8_t SERVER_ID;
|
||||||
WSADATA wsaData;
|
api_packet current_packet;
|
||||||
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
|
u_int8_t apiSent = 0;
|
||||||
fprintf(stderr, "[-] WSAStartup failed\n");
|
|
||||||
return -1;
|
// 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;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
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[]) {
|
||||||
|
|
||||||
|
// 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("[+] Starting server...\n");
|
||||||
printf("[+] Server listening on port %d\n", SERVER_IN_PORT);
|
printf("[+] Server listening on port %d\n", SERVER_IN_PORT);
|
||||||
printf("[+] Server sending to %s:%d\n", SERVER_OUT_IP, SERVER_OUT_PORT);
|
printf("[+] Server sending to %s:%d\n\n", SERVER_OUT_IP, SERVER_OUT_PORT);
|
||||||
|
|
||||||
// Create UDP socket
|
current_packet.tracker_id = (u_int8_t)-1; // Mark as uninitialized
|
||||||
int sock_FD = connect_udp_socket(SERVER_OUT_IP, SERVER_OUT_PORT);
|
|
||||||
if (sock_FD < 0) {
|
int send_sock_fd = connect_udp_socket(SERVER_OUT_IP, SERVER_IN_PORT); // SEND requests to Server
|
||||||
fprintf(stderr, "[-] Failed to create UDP socket\n");
|
int recv_sock_fd = create_bound_udp_socket("0.0.0.0", SERVER_OUT_PORT); // LISTEN to server updates
|
||||||
#ifdef _WIN32
|
|
||||||
WSACleanup();
|
if (send_sock_fd < 0 || recv_sock_fd < 0) {
|
||||||
#endif
|
fprintf(stderr, "[!] Failed to create sockets (%d)(%d)\n", send_sock_fd, recv_sock_fd);
|
||||||
return -1;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
handshake hs;
|
unsigned long offset = 0;
|
||||||
hs.identifier = 1;
|
int ok = 1;
|
||||||
hs.operationId = 1;
|
char buffer[1024];
|
||||||
hs.version = 0;
|
|
||||||
|
|
||||||
printf("[+] Sending handshake message...\t");
|
// Start thread to send data to API socket
|
||||||
ssize_t bytes_sent = send_udp_message((int)sock_FD, (const char*)&hs, SERVER_OUT_IP, uint16_t(SERVER_OUT_PORT));
|
pthread_t api_thread;
|
||||||
if (bytes_sent >= 0) {
|
if (pthread_create(&api_thread, NULL, send_to_api_socket, NULL) != 0) {
|
||||||
printf("OK (%zd bytes)\n", bytes_sent);
|
fprintf(stderr, "[!] Failed to create API socket thread\n");
|
||||||
} else {
|
return EXIT_FAILURE;
|
||||||
fprintf(stderr, "ERROR.\n");
|
|
||||||
CLOSESOCKET(sock_FD);
|
|
||||||
#ifdef _WIN32
|
|
||||||
WSACleanup();
|
|
||||||
#endif
|
|
||||||
return -2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t buffer[512]; // bigger than struct
|
pthread_detach(api_thread);
|
||||||
ssize_t bytes_received = recv(sock_FD, (char*)buffer, sizeof(buffer), 0);
|
|
||||||
|
|
||||||
if (bytes_received >= sizeof(handshackerResponse)) {
|
trackAtributes trackInfo;
|
||||||
|
carAtributes players[MAX_PLAYERS];
|
||||||
|
|
||||||
// Convert strings from big endian to little endian if necessary
|
current_packet.connected_players = 0;
|
||||||
|
carAtributes update;
|
||||||
|
|
||||||
handshackerResponse resp;
|
// Basically a contrutor for carAtributes (because all strings in carAtributes are pointers)
|
||||||
memcpy(&resp, buffer, sizeof(handshackerResponse));
|
for (int i = 0; i < MAX_PLAYERS; i++) {
|
||||||
|
init_carupdate(&players[i]);
|
||||||
printf("[+] Received handshake response:\n");
|
|
||||||
printf(" Car: ");
|
|
||||||
for (int i = 0; i < 50; i++) {
|
|
||||||
printf("%c", isprint(resp.carName[i]) ? resp.carName[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
|
||||||
|
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");
|
printf("\n");
|
||||||
|
while (1) {
|
||||||
|
offset = 0;
|
||||||
|
u_int8_t str_len_8 = 0;
|
||||||
|
memset(buffer, 0, sizeof(buffer));
|
||||||
|
|
||||||
printf(" Driver: %s\n", resp.driverName);
|
// 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
|
||||||
printf(" Identifier: %d\n", resp.identifier);
|
size_t recv_bytes = (size_t)recvfrom(recv_sock_fd, buffer, sizeof(buffer), 0, NULL, NULL);
|
||||||
printf(" Version: %d\n", resp.version);
|
|
||||||
printf(" Track: %s\n", resp.trackName);
|
// Send update rate message to server ONCE
|
||||||
printf(" Config: %s\n", resp.trackConfig);
|
if (updateSent != 1) {
|
||||||
} else {
|
sendto(send_sock_fd, update_rate_message, sizeof(update_rate_message), 0, NULL, 0);
|
||||||
printf("[!] Packet too short for handshake response (%zd bytes)\n", bytes_received);
|
updateSent = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
CLOSESOCKET(sock_FD);
|
// Convert buffer to utf-8 string and print it
|
||||||
|
#if DEBUG_CAR_INFO
|
||||||
#ifdef _WIN32
|
printf("[+] Received %zd bytes from client\n", recv_bytes);
|
||||||
WSACleanup();
|
|
||||||
#endif
|
#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);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// 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
|
||||||
|
|
||||||
|
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: {
|
||||||
|
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
|
||||||
|
|
||||||
|
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, 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// Free allocated memory
|
||||||
|
free(update.car_model);
|
||||||
|
free(update.driver_GUID);
|
||||||
|
free(update.car_skin);
|
||||||
|
free(update.driver_name);
|
||||||
|
free(update.driver_team);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(trackInfo.server_name);
|
||||||
|
free(trackInfo.track);
|
||||||
|
free(trackInfo.track_config);
|
||||||
|
free(trackInfo.weather_graphics);
|
||||||
|
free(trackInfo.session_name);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user