generated from AfonsoCMSousa/CPP-Template
fix: fixed more net.cpp problems
This commit is contained in:
parent
0bdcbb9a9c
commit
9da9cd3c13
BIN
PlayerTracker
BIN
PlayerTracker
Binary file not shown.
@ -6,6 +6,7 @@
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "server_structs.h"
|
||||
@ -17,6 +18,7 @@ class Socket {
|
||||
// Socket file descriptor
|
||||
int sock;
|
||||
struct sockaddr_in server_addr;
|
||||
struct sockaddr_un server_addr_unix;
|
||||
|
||||
// Socket Data
|
||||
api_packet packet_data;
|
||||
@ -25,9 +27,12 @@ class Socket {
|
||||
Socket();
|
||||
~Socket();
|
||||
|
||||
void connect(const char *ip, uint16_t port);
|
||||
void send();
|
||||
void send(const api_packet &data);
|
||||
void connect_server(const char *ip, uint16_t port);
|
||||
void connect_unix(const char *ip, uint16_t port);
|
||||
void send_server();
|
||||
void send_unix();
|
||||
void send_server(const api_packet &data);
|
||||
void send_unix(const api_packet &data);
|
||||
void set_packet(const api_packet &data);
|
||||
api_packet create_packet(uint8_t tracker_id);
|
||||
api_packet get_packet();
|
||||
|
||||
@ -17,15 +17,15 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
try {
|
||||
Socket sock;
|
||||
sock.connect(app.app_server_out_ip, app.app_port_out);
|
||||
// Connect to API socket path
|
||||
sock.connect_unix(app.app_api_socket_path, app.app_port_out);
|
||||
|
||||
// Send a packet for test to API
|
||||
packet = sock.create_packet(app.app_id); // tracker_id = 1
|
||||
sock.send_unix(packet);
|
||||
|
||||
packet = sock.create_packet((uint8_t)app.app_id);
|
||||
cout << "Sending packet with tracker ID: " << (int)packet.tracker_id << endl;
|
||||
sock.set_packet(packet);
|
||||
sock.send();
|
||||
} catch (const runtime_error &e) {
|
||||
cerr << "Error: " << e.what() << endl;
|
||||
cout << "Usage: ./PlayerTracker <app_id> <port_in> <port_out>" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@ -2,54 +2,83 @@
|
||||
#include <sys/types.h>
|
||||
|
||||
Socket::Socket() {
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock < 0) {
|
||||
throw runtime_error("Failed to create socket");
|
||||
}
|
||||
memset(&server_addr, 0, sizeof(server_addr));
|
||||
server_addr.sin_family = AF_INET;
|
||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sock < 0) {
|
||||
throw runtime_error("Failed to create socket");
|
||||
}
|
||||
memset(&server_addr, 0, sizeof(server_addr));
|
||||
server_addr.sin_family = AF_INET;
|
||||
}
|
||||
|
||||
Socket::~Socket() {
|
||||
close(sock);
|
||||
close(sock);
|
||||
}
|
||||
|
||||
void Socket::connect(const char *ip, uint16_t port) {
|
||||
server_addr.sin_port = htons(port);
|
||||
if (inet_pton(AF_INET, ip, &server_addr.sin_addr) <= 0) {
|
||||
throw runtime_error("Invalid IP address");
|
||||
void Socket::connect_server(const char *ip, uint16_t 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::connect_unix(const char *ip, uint16_t port) {
|
||||
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
|
||||
if (sock < 0) {
|
||||
throw std::runtime_error("Failed to create UNIX socket");
|
||||
}
|
||||
|
||||
memset(&server_addr, 0, sizeof(server_addr));
|
||||
this->server_addr_unix.sun_family = AF_UNIX;
|
||||
strncpy(this->server_addr_unix.sun_path, ip, sizeof(this->server_addr_unix.sun_path) - 1);
|
||||
|
||||
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
throw std::runtime_error("Failed to connect to UNIX socket");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::send_server() {
|
||||
ssize_t sent_bytes = sendto(sock, &packet_data, sizeof(packet_data), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||
if (sent_bytes < 0) {
|
||||
throw runtime_error("Failed to send data");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::send_unix() {
|
||||
ssize_t sent_bytes = sendto(sock, &packet_data, sizeof(packet_data), 0, (struct sockaddr *)&server_addr_unix, sizeof(server_addr_unix));
|
||||
if (sent_bytes < 0) {
|
||||
throw runtime_error("Failed to send data to UNIX socket");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::send() {
|
||||
ssize_t sent_bytes = sendto(sock, &packet_data, sizeof(packet_data), 0,
|
||||
(struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||
if (sent_bytes < 0) {
|
||||
throw runtime_error("Failed to send data");
|
||||
}
|
||||
void Socket::send_server(const api_packet &data) {
|
||||
ssize_t sent_bytes = sendto(sock, &data, sizeof(data), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||
if (sent_bytes < 0) {
|
||||
throw runtime_error("Failed to send data");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::send(const api_packet &data) {
|
||||
ssize_t sent_bytes = sendto(sock, &data, sizeof(data), 0,
|
||||
(struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||
void Socket::send_unix(const api_packet &data) {
|
||||
ssize_t sent_bytes = sendto(sock, &data, sizeof(data), 0, (struct sockaddr *)&server_addr_unix, sizeof(server_addr_unix));
|
||||
if (sent_bytes < 0) {
|
||||
throw runtime_error("Failed to send data");
|
||||
throw runtime_error("Failed to send data to UNIX socket");
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::set_packet(const api_packet &data) {
|
||||
packet_data = data;
|
||||
packet_data = data;
|
||||
}
|
||||
|
||||
api_packet Socket::create_packet(uint8_t tracker_id) {
|
||||
api_packet pkt;
|
||||
memset(&pkt, 0, sizeof(pkt));
|
||||
api_packet pkt;
|
||||
memset(&pkt, 0, sizeof(pkt));
|
||||
|
||||
pkt.message_type = 65; // Handshake message type
|
||||
pkt.tracker_id = tracker_id;
|
||||
return pkt;
|
||||
pkt.message_type = 65; // Handshake message type
|
||||
pkt.tracker_id = tracker_id;
|
||||
return pkt;
|
||||
}
|
||||
|
||||
api_packet Socket::get_packet() {
|
||||
return packet_data;
|
||||
return packet_data;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user