generated from AfonsoCMSousa/CPP-Template
Stabe: V1.0 - First Stable Release AND API connection (See https://git.fernandovideira.com/AfonsoCMSousa/ACPlayer_database_api)
This commit is contained in:
+145
-5
@@ -2,12 +2,14 @@
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#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"
|
||||
@@ -20,7 +22,132 @@ const int SERVER_OUT_PORT = 12000;
|
||||
const int SERVER_IN_PORT = 11000;
|
||||
const char *SERVER_OUT_IP = "127.0.0.1";
|
||||
|
||||
const char *API_SOCKET_PATH = "/tmp/ACplayer_socket";
|
||||
|
||||
const int MAX_PLAYERS = 64;
|
||||
const int SERVER_ID = 130;
|
||||
|
||||
api_packet current_packet;
|
||||
|
||||
// INFO: This assumes Players pointer to be the same size of MAX_PLAYERS
|
||||
void update_api_packet(trackAtributes trackInfo, carAtributes *players, u_int8_t last_updated_carid) {
|
||||
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;
|
||||
}
|
||||
|
||||
if (last_updated_carid == NULL) {
|
||||
current_packet.last_updated_carid = 0xFF; // No specific car updated
|
||||
} else {
|
||||
current_packet.last_updated_carid = last_updated_carid;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
while (1) {
|
||||
if (current_packet.tracker_id == (u_int8_t)-1) {
|
||||
usleep(10000);
|
||||
continue;
|
||||
}
|
||||
ssize_t sent_bytes = send(sock_fd, ¤t_packet, sizeof(api_packet), 0);
|
||||
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);
|
||||
}
|
||||
|
||||
usleep(10000); // 10ms
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void init_carupdate(carAtributes *car) {
|
||||
car->isConnected = 0;
|
||||
@@ -67,6 +194,8 @@ int main(void) {
|
||||
printf("[+] Server listening on port %d\n", SERVER_IN_PORT);
|
||||
printf("[+] Server sending to %s:%d\n\n", SERVER_OUT_IP, SERVER_OUT_PORT);
|
||||
|
||||
current_packet.tracker_id = (u_int8_t)-1; // Mark as uninitialized
|
||||
|
||||
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
|
||||
|
||||
@@ -79,6 +208,15 @@ 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];
|
||||
|
||||
@@ -98,7 +236,6 @@ int main(void) {
|
||||
|
||||
char message[124] = {0}; // just a place to put info messages from the server itself
|
||||
|
||||
|
||||
printf("\n");
|
||||
while (1) {
|
||||
offset = 0;
|
||||
@@ -479,7 +616,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,12 +654,12 @@ 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];
|
||||
}
|
||||
@@ -540,6 +677,9 @@ int main(void) {
|
||||
break;
|
||||
}
|
||||
|
||||
// OPTIMIZE: This should be done inside each case where data is updated otherwise we lose performance
|
||||
update_api_packet(trackInfo, players, update.carID);
|
||||
|
||||
usleep(10000); // Sleep for 10ms
|
||||
}
|
||||
close(send_sock_fd);
|
||||
|
||||
Reference in New Issue
Block a user