generated from AfonsoCMSousa/CPP-Template
feat and fix: log and server issues
This commit is contained in:
parent
ef0da72123
commit
ef73f16fea
BIN
PlayerTracker
BIN
PlayerTracker
Binary file not shown.
23
include/log.h
Normal file
23
include/log.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef LOG_H
|
||||
#define LOG_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <time.h>
|
||||
|
||||
typedef enum {
|
||||
LOG_INFO,
|
||||
LOG_DEBUG,
|
||||
LOG_WARN,
|
||||
LOG_ERROR
|
||||
} LogLevel;
|
||||
|
||||
void log_print(LogLevel level, const char* format, ...);
|
||||
|
||||
#define log_info(...) log_print(LOG_INFO, __VA_ARGS__)
|
||||
#define log_debug(...) log_print(LOG_DEBUG, __VA_ARGS__)
|
||||
#define log_warn(...) log_print(LOG_WARN, __VA_ARGS__)
|
||||
#define log_error(...) log_print(LOG_ERROR, __VA_ARGS__)
|
||||
|
||||
#endif // LOG_H
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "log.h"
|
||||
#include "server_structs.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -199,6 +199,7 @@ enum ACSP_MessageType {
|
||||
// ============================
|
||||
// PROTOCOL VERSION
|
||||
// ============================
|
||||
// DONE: Update this when protocol changes
|
||||
PROTOCOL_VERSION = 4,
|
||||
|
||||
// ============================
|
||||
|
||||
@ -14,7 +14,6 @@ private:
|
||||
|
||||
public:
|
||||
SessionManager(u_int8_t sid);
|
||||
~SessionManager();
|
||||
|
||||
void on_new_session(const trackAtributes &track);
|
||||
void on_player_connected(const carAtributes &car);
|
||||
|
||||
24
source/log.c
Normal file
24
source/log.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include "log.h"
|
||||
|
||||
void log_print(LogLevel level, const char* format, ...) {
|
||||
time_t now = time(NULL);
|
||||
struct tm *t = localtime(&now);
|
||||
|
||||
const char* level_tag;
|
||||
switch(level) {
|
||||
case LOG_INFO: level_tag = "INF"; break;
|
||||
case LOG_DEBUG: level_tag = "DBG"; break;
|
||||
case LOG_ERROR: level_tag = "ERR"; break;
|
||||
case LOG_WARN: level_tag = "WRN"; break;
|
||||
default: level_tag = "UNK"; break;
|
||||
}
|
||||
|
||||
// %02d ensures it prints "05" instead of just "5"
|
||||
printf("[%02d:%02d:%02d %s] ",
|
||||
t->tm_hour, t->tm_min, t->tm_sec, level_tag);
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vprintf(format, args); // vprintf takes a va_list instead of ...
|
||||
va_end(args);
|
||||
}
|
||||
@ -1,27 +1,30 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <signal.h>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <vector>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <vector>
|
||||
|
||||
#include "app.hpp" // for app_info struct
|
||||
#include "file.hpp" // for parce_args
|
||||
#include "log.h" // for logging
|
||||
#include "net.hpp" // for socket operations
|
||||
#include "server_structs.h" // for api_packet and ACSP_MessageType
|
||||
#include "session.hpp" // for SessionManager
|
||||
|
||||
const u_int8_t UPDATE_INTERVAL = 120; // in milliseconds
|
||||
|
||||
using namespace std;
|
||||
|
||||
volatile bool STOP_PROGRAM = false;
|
||||
|
||||
void signal_handler(int signum) {
|
||||
if (signum == SIGINT || signum == SIGTERM) {
|
||||
STOP_PROGRAM = true;
|
||||
}
|
||||
if (signum == SIGINT || signum == SIGTERM) {
|
||||
STOP_PROGRAM = true;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
@ -36,30 +39,32 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
try {
|
||||
// Connect socket to API
|
||||
sock.connect_unix(app.app_api_socket_path.c_str(), app.app_port_out);
|
||||
// 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);
|
||||
|
||||
// Await server for initial data
|
||||
sock.receive_server(buffer, sizeof(buffer));
|
||||
cout << "[+] Received info from server!" << endl;
|
||||
log_info("Connected to server, awaiting version confirmation...\n");
|
||||
|
||||
if (buffer[0] == ACSP_VERSION) {
|
||||
cout << "[+] Server API Version: " << (int)buffer[1] << endl;
|
||||
log_info("Server version confirmed. Sending update rate request @ %ums\n", UPDATE_INTERVAL);
|
||||
} else {
|
||||
throw runtime_error("Did not receive version confirmation from server.");
|
||||
}
|
||||
|
||||
// Right after confirmation, send update rate request
|
||||
char request[516] = {0};
|
||||
request[0] = ACSP_REALTIMEPOS_INTERVAL;
|
||||
request[1] = 120;
|
||||
request[1] = UPDATE_INTERVAL;
|
||||
|
||||
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;
|
||||
log_debug("Info:\n");
|
||||
log_debug("\tApp ID: %d\n", app.app_id);
|
||||
log_debug("\tAPI Socket Path: %s\n", app.app_api_socket_path.c_str());
|
||||
log_debug("\tServer Out IP: %s\n", app.app_server_out_ip.c_str());
|
||||
log_debug("\tApp Port In: %d\n", app.app_port_in);
|
||||
log_debug("\tApp Port Out: %d\n", app.app_port_out);
|
||||
|
||||
} catch (const runtime_error &e) {
|
||||
cerr << "Error: " << e.what() << endl;
|
||||
@ -75,29 +80,31 @@ int main(int argc, char *argv[]) {
|
||||
ssize_t received = sock.receive_server(buffer, sizeof(buffer));
|
||||
if (received > 0) {
|
||||
switch (buffer[0]) {
|
||||
// DONE:
|
||||
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));
|
||||
log_warn("Received Version Again? (Probably server restart) Resending Update Request @ %ums\n", UPDATE_INTERVAL);
|
||||
|
||||
break;
|
||||
}
|
||||
char request[516] = {0};
|
||||
request[0] = ACSP_REALTIMEPOS_INTERVAL;
|
||||
request[1] = UPDATE_INTERVAL;
|
||||
sock.send_server(request, sizeof(request));
|
||||
|
||||
case ACSP_CAR_UPDATE: {
|
||||
cout << "[+] Received Car Update Packet with " << (int)packet.connected_players << " connected players." << endl;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// TODO:
|
||||
case ACSP_CAR_UPDATE: {
|
||||
log_info("Received car update.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO:
|
||||
case ACSP_NEW_SESSION: {
|
||||
cout << "[+] New Session Started: " << packet.track_info.session_name << endl;
|
||||
log_info("New session started.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
cout << "[!] Unknown packet type received: " << (int)buffer[0] << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -106,4 +113,3 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
#include "net.hpp"
|
||||
#include "log.c"
|
||||
#include <sys/types.h>
|
||||
|
||||
Socket::Socket() {
|
||||
@ -32,11 +33,11 @@ void Socket::bind_server(const char *ip, uint16_t port) {
|
||||
bind_addr.sin_addr.s_addr = inet_addr(ip);
|
||||
|
||||
if (bind(sock_server, (const struct sockaddr *)&bind_addr, sizeof(bind_addr)) < 0) {
|
||||
perror("[!] bind() failed");
|
||||
log_error("Failed to bind UDP socket on %s:%d\n", ip, port);
|
||||
throw runtime_error("Failed to bind UDP socket");
|
||||
}
|
||||
|
||||
printf("[+] Bound UDP socket on %s:%d\n", ip, port);
|
||||
log_info("Successfully bound UDP socket on %s:%d\n", ip, port);
|
||||
}
|
||||
|
||||
void Socket::connect_unix(const char *ip, uint16_t port) {
|
||||
@ -63,7 +64,7 @@ void Socket::send_server() {
|
||||
ssize_t Socket::receive_server(void *buffer, size_t len) {
|
||||
ssize_t recv_bytes = recv(sock_server, buffer, len, 0);
|
||||
if (recv_bytes < 0) {
|
||||
perror("[!] recv() failed");
|
||||
log_error("Failed to receive data from server socket\n");
|
||||
return -1;
|
||||
}
|
||||
return recv_bytes;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user