generated from AfonsoCMSousa/CPP-Template
fix: finnaly working!
This commit is contained in:
parent
ba606c8619
commit
ff07d6dcdf
BIN
PlayerTracker
BIN
PlayerTracker
Binary file not shown.
@ -16,7 +16,8 @@ using namespace std;
|
|||||||
class Socket {
|
class Socket {
|
||||||
private:
|
private:
|
||||||
// Socket file descriptor
|
// Socket file descriptor
|
||||||
int sock;
|
int sock_server;
|
||||||
|
int sock_unix;
|
||||||
struct sockaddr_in server_addr;
|
struct sockaddr_in server_addr;
|
||||||
struct sockaddr_un server_addr_unix;
|
struct sockaddr_un server_addr_unix;
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,9 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
Socket::Socket() {
|
Socket::Socket() {
|
||||||
sock = socket(AF_INET, SOCK_DGRAM, 0);
|
sock_server = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (sock < 0) {
|
sock_unix = -1;
|
||||||
|
if (sock_server < 0) {
|
||||||
throw runtime_error("Failed to create socket");
|
throw runtime_error("Failed to create socket");
|
||||||
}
|
}
|
||||||
memset(&server_addr, 0, sizeof(server_addr));
|
memset(&server_addr, 0, sizeof(server_addr));
|
||||||
@ -11,7 +12,8 @@ Socket::Socket() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Socket::~Socket() {
|
Socket::~Socket() {
|
||||||
close(sock);
|
close(sock_server);
|
||||||
|
close(sock_unix);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::connect_server(const char *ip, uint16_t port) {
|
void Socket::connect_server(const char *ip, uint16_t port) {
|
||||||
@ -23,51 +25,51 @@ void Socket::connect_server(const char *ip, uint16_t port) {
|
|||||||
|
|
||||||
// Connect to existing UNIX domain socket (e.g., /tmp/socket_path)
|
// Connect to existing UNIX domain socket (e.g., /tmp/socket_path)
|
||||||
void Socket::connect_unix(const char *ip, uint16_t port) {
|
void Socket::connect_unix(const char *ip, uint16_t port) {
|
||||||
sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
sock_unix = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
if (sock < 0) {
|
if (sock_unix < 0) {
|
||||||
throw std::runtime_error("Failed to create UNIX socket");
|
throw std::runtime_error("Failed to create UNIX socket");
|
||||||
}
|
}
|
||||||
|
|
||||||
this->server_addr_unix.sun_family = AF_UNIX;
|
this->server_addr_unix.sun_family = AF_UNIX;
|
||||||
strncpy(this->server_addr_unix.sun_path, ip, sizeof(this->server_addr_unix.sun_path) - 1);
|
strncpy(this->server_addr_unix.sun_path, ip, sizeof(this->server_addr_unix.sun_path) - 1);
|
||||||
|
|
||||||
printf("Connecting to UNIX socket at %s\n", ip);
|
printf("Connecting to UNIX socket at %s\n", ip);
|
||||||
|
|
||||||
if (connect(sock, (struct sockaddr *)&server_addr_unix, sizeof(server_addr_unix)) < 0) {
|
if (connect(sock_unix, (struct sockaddr *)&server_addr_unix, sizeof(server_addr_unix)) < 0) {
|
||||||
throw std::runtime_error("Failed to connect to UNIX socket");
|
throw std::runtime_error("Failed to connect to UNIX socket");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::send_server() {
|
void Socket::send_server() {
|
||||||
ssize_t sent_bytes = sendto(sock, &packet_data, sizeof(packet_data), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
ssize_t sent_bytes = sendto(sock_server, &packet_data, sizeof(packet_data), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||||
if (sent_bytes < 0) {
|
if (sent_bytes < 0) {
|
||||||
throw runtime_error("Failed to send data");
|
throw runtime_error("Failed to send data");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::send_unix() {
|
void Socket::send_unix() {
|
||||||
ssize_t sent_bytes = send(sock, &packet_data, sizeof(packet_data), 0);
|
ssize_t sent_bytes = send(sock_unix, &packet_data, sizeof(packet_data), 0);
|
||||||
if (sent_bytes < 0) {
|
if (sent_bytes < 0) {
|
||||||
throw runtime_error("Failed to send data to UNIX socket");
|
throw runtime_error("Failed to send data to UNIX socket");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::send_server(const api_packet &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));
|
ssize_t sent_bytes = sendto(sock_server, &data, sizeof(data), 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||||
if (sent_bytes < 0) {
|
if (sent_bytes < 0) {
|
||||||
throw runtime_error("Failed to send data");
|
throw runtime_error("Failed to send data");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::send_server(const void *data, size_t len) {
|
void Socket::send_server(const void *data, size_t len) {
|
||||||
ssize_t sent_bytes = sendto(sock, data, len, 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
ssize_t sent_bytes = sendto(sock_server, data, len, 0, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||||
if (sent_bytes < 0) {
|
if (sent_bytes < 0) {
|
||||||
throw runtime_error("Failed to send data");
|
throw runtime_error("Failed to send data");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::send_unix(const api_packet &data) {
|
void Socket::send_unix(const api_packet &data) {
|
||||||
ssize_t sent_bytes = send(sock, &data, sizeof(data), 0);
|
ssize_t sent_bytes = send(sock_unix, &data, sizeof(data), 0);
|
||||||
if (sent_bytes < 0) {
|
if (sent_bytes < 0) {
|
||||||
throw runtime_error("Failed to send data to UNIX socket");
|
throw runtime_error("Failed to send data to UNIX socket");
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user