fix: finnaly working!

This commit is contained in:
AfonsoCMSousa 2026-01-07 23:10:16 +00:00
parent 7a648f8b21
commit 8bc4312c9a
4 changed files with 41 additions and 17 deletions

Binary file not shown.

View File

@ -17,6 +17,7 @@ class Socket {
private:
// Socket file descriptor
int sock_server;
int sock_server_rev;
int sock_unix;
struct sockaddr_in server_addr;
struct sockaddr_un server_addr_unix;
@ -32,12 +33,12 @@ class Socket {
Socket();
~Socket();
void connect_server(const char *ip, uint16_t port, uint16_t port_input);
void connect_server(const char *ip, uint16_t port);
void bind_server(const char *ip, uint16_t port);
void send_server();
void send_server(const api_packet &data);
void send_server(const void *data, size_t len);
void receive_server();
char *get_buffer() { return buffer; }
ssize_t receive_server(void *buffer, size_t len);
void connect_unix(const char *ip, uint16_t port);
void send_unix();

View File

@ -18,11 +18,13 @@ int main(int argc, char *argv[]) {
Socket sock;
try {
// 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, app.app_port_in);
sock.connect_server(app.app_server_out_ip.c_str(), app.app_port_out);
sock.bind_server("0.0.0.0", app.app_port_in);
// Right after connecting, send update rate request
char request[516] = {0};
@ -41,14 +43,21 @@ int main(int argc, char *argv[]) {
return 1;
}
sock.receive_server();
char *buffer = sock.get_buffer();
cout << "Received " << sizeof(buffer) << " bytes from server." << endl;
cout << "Data: ";
for (size_t i = 0; i < sizeof(buffer); i++) {
printf("%02X ", static_cast<unsigned char>(buffer[i]));
// 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
char buffer[2048];
sock.receive_server(buffer, sizeof(buffer));
cout << "[+] Received data from server!" << endl;
cout << "[+] Data (first 64 bytes): " << endl;
// printf buffer in hex
for (size_t i = 0; i < 64; i++) {
printf("%02X ", (unsigned char)buffer[i]);
}
cout << endl;
printf("\n");
return 0;
}

View File

@ -16,15 +16,27 @@ Socket::~Socket() {
close(sock_unix);
}
void Socket::connect_server(const char *ip, uint16_t port, uint16_t port_input) {
this->server_port_input = port_input;
void Socket::connect_server(const char *ip, uint16_t port) {
this->server_port_input = port;
server_addr.sin_port = htons(port);
if (inet_pton(AF_INET, ip, &server_addr.sin_addr) <= 0) {
throw runtime_error("Invalid IP address");
}
}
// Connect to existing UNIX domain socket (e.g., /tmp/socket_path)
void Socket::bind_server(const char *ip, uint16_t port) {
// The UDP socket is already created in the constructor
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(ip);
if (bind(sock_server_rev, (const struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("[!] bind() failed");
throw runtime_error("Failed to bind UDP socket");
}
printf("[+] Bound UDP socket on %s:%d\n", ip, port);
}
void Socket::connect_unix(const char *ip, uint16_t port) {
sock_unix = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_unix < 0) {
@ -46,11 +58,13 @@ void Socket::send_server() {
}
}
void Socket::receive_server() {
ssize_t recv_bytes = recvfrom(sock_server, buffer, sizeof(buffer), 0, NULL, NULL);
ssize_t Socket::receive_server(void *buffer, size_t len) {
ssize_t recv_bytes = recv(sock_server_rev, buffer, len, 0);
if (recv_bytes < 0) {
throw runtime_error("Failed to receive data");
perror("[!] recv() failed");
return -1;
}
return recv_bytes;
}
void Socket::send_unix() {