From 195d6c4b4bb74ed3e62cf6fd2c62403c538d0431 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Wed, 14 Feb 2024 18:30:09 -0500 Subject: [PATCH] Separated 'Sock' file into header and implementation file, and added include guards to header file --- includes/sock.hpp | 72 ++++++++--------------------------------------- sock.cpp | 64 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 61 deletions(-) create mode 100644 sock.cpp diff --git a/includes/sock.hpp b/includes/sock.hpp index 6c77492..37ecfa5 100644 --- a/includes/sock.hpp +++ b/includes/sock.hpp @@ -1,10 +1,8 @@ #ifndef _SOCK_CLASS #define _SOCK_CLASS -#include "easysock.hpp" #include -#include -#include "exception_consts.hpp" +#include class Sock { /* Define a parent class of client and server, that can be used in main.cpp, instead of defining a client and server. This will allow me to create a struct for Mode, that can store either a client or a server, depending on what mode the game is in. */ @@ -16,72 +14,24 @@ protected: std::string address; struct sockaddr* dest; - void create_socket() { - dest = (struct sockaddr *)malloc(sizeof(struct sockaddr)); - } + void create_socket(); public: + /* Default constructor */ Sock() {} - Sock(int ip_ver, char protocol, const char* address, int port) { - /* Error checking */ - if (ip_ver != 4 && ip_ver != 6) { - throw std::invalid_argument("Invalid IP address type"); - } - if (port < 1024 || port > 65535) { - throw std::invalid_argument("Invalid port"); - } - if (protocol != 'T' && protocol != 'U') { - throw std::invalid_argument("Invalid protocol"); - } + /* Regular constructor - defined in sock.cpp */ + Sock(int ip_ver, char protocol, const char* address, int port); - this->ip_ver = ip_ver; - this->protocol = protocol; - this->port = port; - this->address = std::string(address); + /* Method to send data in 'to_send' to the destination - defined in sock.cpp */ + void sendAll(std::string to_send); - /* Check to see if the given IP address matches the given ip_ver */ - if ((check_ip_ver(address) != 6 && ip_ver == 6) || (check_ip_ver(address) != 4 && ip_ver == 4)) { - throw std::invalid_argument("Invalid IP address for given type."); - } - - try { - create_socket(); - } catch (int e) { - throw; - } - } - - - void sendAll(std::string to_send) { - int str_length = to_send.length(); - int num_bytes_sent = 0; /* Number of bytes sent in one call to send */ - int total_bytes_sent = 0; /* Total number of bytes sent */ - - while (total_bytes_sent < str_length) { - num_bytes_sent = send(this->getSockFD(), to_send.substr(total_bytes_sent).data(), str_length - total_bytes_sent, 0); - total_bytes_sent += num_bytes_sent; - } - - return; - } - - - std::string recvAll() { - std::string string = std::string(); - char* buffer = (char *)malloc(100); - while (recv(this->getSockFD(), buffer, 100, 0) != 0) { - string.append(std::string(buffer)); - } - - return string; - } - - int getSockFD() { - return sock_fd; - } + /* Method to receive data sent to socket - defined in sock.cpp */ + std::string recvAll(); + /* Returns socket identifier - defined in sock.cpp */ + int getSockFD(); }; #endif diff --git a/sock.cpp b/sock.cpp new file mode 100644 index 0000000..7b0cf8f --- /dev/null +++ b/sock.cpp @@ -0,0 +1,64 @@ +#include +#include "includes/sock.hpp" +#include "includes/exception_consts.hpp" +#include "includes/easysock.hpp" + +void Sock::create_socket() { + dest = (struct sockaddr *)malloc(sizeof(struct sockaddr)); +} + +Sock::Sock(int ip_ver, char protocol, const char* address, int port) { + /* Error checking */ + if (ip_ver != 4 && ip_ver != 6) { + throw std::invalid_argument("Invalid IP address type"); + } + if (port < 1024 || port > 65535) { + throw std::invalid_argument("Invalid port"); + } + if (protocol != 'T' && protocol != 'U') { + throw std::invalid_argument("Invalid protocol"); + } + + this->ip_ver = ip_ver; + this->protocol = protocol; + this->port = port; + this->address = std::string(address); + + /* Check to see if the given IP address matches the given ip_ver */ + if ((check_ip_ver(address) != 6 && ip_ver == 6) || (check_ip_ver(address) != 4 && ip_ver == 4)) { + throw std::invalid_argument("Invalid IP address for given type."); + } + + try { + create_socket(); + } catch (int e) { + throw; + } +} + +void Sock::sendAll(std::string to_send) { + int str_length = to_send.length(); + int num_bytes_sent = 0; /* Number of bytes sent in one call to send */ + int total_bytes_sent = 0; /* Total number of bytes sent */ + + while (total_bytes_sent < str_length) { + num_bytes_sent = send(this->getSockFD(), to_send.substr(total_bytes_sent).data(), str_length - total_bytes_sent, 0); + total_bytes_sent += num_bytes_sent; + } + + return; +} + +std::string Sock::recvAll() { + std::string string = std::string(); + char* buffer = (char *)malloc(100); + while (recv(this->getSockFD(), buffer, 100, 0) != 0) { + string.append(std::string(buffer)); + } + + return string; +} + +int Sock::getSockFD() { + return sock_fd; +}