generated from AfonsoCMSousa/CPP-Template
Refactor: Rewriting new functions and classes in C++ with better code readability in mind
This commit is contained in:
parent
b2f283fff2
commit
43827f8f12
@ -0,0 +1,55 @@
|
||||
#include "net.hpp"
|
||||
#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;
|
||||
}
|
||||
|
||||
Socket::~Socket() {
|
||||
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::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(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::set_packet(const api_packet &data) {
|
||||
packet_data = data;
|
||||
}
|
||||
|
||||
api_packet Socket::create_packet(uint8_t tracker_id) {
|
||||
api_packet pkt;
|
||||
memset(&pkt, 0, sizeof(pkt));
|
||||
|
||||
pkt.message_type = 65; // Handshake message type
|
||||
pkt.tracker_id = tracker_id;
|
||||
return pkt;
|
||||
}
|
||||
|
||||
api_packet Socket::get_packet() {
|
||||
return packet_data;
|
||||
}
|
||||
@ -1,11 +1,11 @@
|
||||
#ifndef NET_HPP
|
||||
#define NET_HPP
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "server_structs.h"
|
||||
@ -17,10 +17,10 @@ class Socket {
|
||||
// Socket file descriptor
|
||||
int sock;
|
||||
struct sockaddr_in server_addr;
|
||||
|
||||
// Socket Data
|
||||
api_packet packet_data;
|
||||
|
||||
|
||||
public:
|
||||
Socket();
|
||||
~Socket();
|
||||
@ -28,6 +28,9 @@ class Socket {
|
||||
void connect(const char *ip, uint16_t port);
|
||||
void send();
|
||||
void send(const api_packet &data);
|
||||
void set_packet(const api_packet &data);
|
||||
api_packet create_packet(uint8_t tracker_id);
|
||||
api_packet get_packet();
|
||||
};
|
||||
|
||||
#endif // NET_HPP
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@ -6,12 +7,25 @@
|
||||
|
||||
#include "file.hpp" // for parce_args
|
||||
#include "net.hpp" // for socket operations
|
||||
#include "server_structs.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
app_info app = parce_args(argc, argv);
|
||||
api_packet packet;
|
||||
|
||||
try {
|
||||
Socket sock;
|
||||
sock.connect(app.app_server_out_ip, app.app_port_out);
|
||||
|
||||
packet = sock.create_packet((uint8_t)app.app_id);
|
||||
sock.set_packet(packet);
|
||||
sock.send();
|
||||
} catch (const runtime_error &e) {
|
||||
cerr << "Error: " << e.what() << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user