diff --git a/source/main.cpp b/source/main.cpp index f67f4e9..e5b232c 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -5,14 +5,24 @@ #include #include #include +#include +#include -#include "app.hpp" // for app_info struct -#include "file.hpp" // for parce_args -#include "net.hpp" // for socket operations -#include "server_structs.h" +#include "app.hpp" // for app_info struct +#include "file.hpp" // for parce_args +#include "net.hpp" // for socket operations +#include "server_structs.h" // for api_packet and ACSP_MessageType +#include "session.hpp" // for SessionManager -using namespace std; +using namespace std; +volatile bool STOP_PROGRAM = false; + +void signal_handler(int signum) { + if (signum == SIGINT || signum == SIGTERM) { + STOP_PROGRAM = true; + } +} int main(int argc, char *argv[]) { app_info app = parce_args(argc, argv); @@ -20,44 +30,80 @@ int main(int argc, char *argv[]) { Socket sock; - char buffer[2048]; + SessionManager session_manager(app.app_id); // Server ID 1 for now + + char buffer[2048]; try { - // Connect socket to API + // Connect socket to API sock.connect_unix(app.app_api_socket_path.c_str(), app.app_port_out); - // Connect socket to Server - sock.connect_server(app.app_server_out_ip.c_str(), app.app_port_out); - sock.bind_server("127.0.0.1", app.app_port_in); + // Connect socket to Server + sock.connect_server(app.app_server_out_ip.c_str(), app.app_port_out); + sock.bind_server("127.0.0.1", app.app_port_in); - // Await server for initial data - sock.receive_server(buffer, sizeof(buffer)); - cout << "[+] Received info from server!" << endl; - - if (buffer[0] == ACSP_VERSION) { - cout << "[+] Server API Version: " << (int)buffer[1] << endl; - } + // Await server for initial data + sock.receive_server(buffer, sizeof(buffer)); + cout << "[+] Received info from server!" << endl; - // Right after confirmation, send update rate request - char request[516] = {0}; - request[0] = ACSP_REALTIMEPOS_INTERVAL; - request[1] = 120; + if (buffer[0] == ACSP_VERSION) { + cout << "[+] Server API Version: " << (int)buffer[1] << endl; + } + + // Right after confirmation, send update rate request + char request[516] = {0}; + request[0] = ACSP_REALTIMEPOS_INTERVAL; + request[1] = 120; + + sock.send_server(request, sizeof(request)); + cout << "[!] Info: " << endl; + cout << " > API Socket Path: " << app.app_api_socket_path << endl; + cout << " > Server Out IP: " << app.app_server_out_ip << endl; + cout << " > App Port In: " << app.app_port_in << endl; + cout << " > App Port Out: " << app.app_port_out << endl; - sock.send_server(request, sizeof(request)); - cout << "[!] Info: " << endl; - cout << " > API Socket Path: " << app.app_api_socket_path << endl; - cout << " > Server Out IP: " << app.app_server_out_ip << endl; - cout << " > App Port In: " << app.app_port_in << endl; - cout << " > App Port Out: " << app.app_port_out << endl; - } catch (const runtime_error &e) { cerr << "Error: " << e.what() << endl; return 1; } - // TODO: Implement Cache - // TAG: Because sometimes the parser doesnt have the name of the server because it started after the server init - // we can cache it and reuse it to avoid NULL names in the DB + // TODO: Implement Cache + // TAG: Because sometimes the parser doesnt have the name of the server because it started after the server init + // we can cache it and reuse it to avoid NULL names in the DB - return 0; + while (STOP_PROGRAM == false) { + // Receive data from server + ssize_t received = sock.receive_server(buffer, sizeof(buffer)); + if (received > 0) { + switch (buffer[0]) { + case ACSP_VERSION: { + cout << "[?] Received Version Packet?\nResending Update Rate Request..." << endl; + + char request[516] = {0}; + request[0] = ACSP_REALTIMEPOS_INTERVAL; + request[1] = 120; + sock.send_server(request, sizeof(request)); + + break; + } + + case ACSP_CAR_UPDATE: { + cout << "[+] Received Car Update Packet with " << (int)packet.connected_players << " connected players." << endl; + break; + } + + case ACSP_NEW_SESSION: { + cout << "[+] New Session Started: " << packet.track_info.session_name << endl; + break; + } + + default: { + cout << "[!] Unknown packet type received: " << (int)buffer[0] << endl; + break; + } + } + } + } + + return 0; }