From 11d0a1a5abe41cfd08cdab5ad0fea4646c8ea679 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 19 Feb 2024 21:29:41 -0500 Subject: [PATCH] Completely rewrote Client class, and split the class into header and implementation --- client.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ includes/client.hpp | 23 +++++++---------------- 2 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 client.cpp diff --git a/client.cpp b/client.cpp new file mode 100644 index 0000000..a7cf1bb --- /dev/null +++ b/client.cpp @@ -0,0 +1,43 @@ +#include +#include "includes/client.hpp" +#include "includes/exception_consts.hpp" +#include "includes/sock.hpp" +#include "includes/easysock.hpp" + +/* Destructor - closes any open sockets */ +Client::~Client() { + close(this->other_socket); + close(this->sock_fd); +} + +/* Creates a client socket. This method extends the parent method, and should be +called immediately after the constructor. It also tries to connect to the +address specified in the constructor, and will throw an exception if that fails. +The exception thrown is an integer, that corresponds to the errno returned by the failing +function. This enables a client to 'catch' the thrown exception, and print the corresponding +error message using strerror(). +*/ + +void Client::create_socket() { + Sock::create_socket(); + this->sock_fd = create_remote(this->ip_ver, this->protocol, this->address.data(), this->port, dest); + if (this->sock_fd < 0) { + throw strerror(this->sock_fd * -1); + } +} + +/* Sends given data to the peer socket. This method is overriden, because a TCP +server socket has a 'peer' socket, that it must send to and receive data from. A +client socket (TCP or UDP) does not have this requirement. Therefore, while both +sendAll methods perform the same actions, they do so using different sockets. */ + +void Client::sendAll(std::string to_send) { + Sock::sendAll(to_send, this->sock_fd); +} + +/* Receives data from peer socket, and returns it. See above for better +explanation of why this method is overriden. */ + +std::string Client::recvAll() { + return Sock::recvAll(this->sock_fd); +} diff --git a/includes/client.hpp b/includes/client.hpp index ca31291..a82ab1a 100644 --- a/includes/client.hpp +++ b/includes/client.hpp @@ -2,29 +2,20 @@ #define _CLIENT_H #include "includes/sock.hpp" -#include "includes/exception_consts.hpp" -/* -Client class - Inherits from 'Sock' class - Defines a TCP/UDP client. The only method that is overriden is the 'create_socket' method. -*/ -class Client : public Sock { - -private: - void create_socket() { - Sock::create_socket(); - this->sock_fd = create_remote(this->ip_ver, this->protocol, this->address.data(), this->port, dest); - if (sock_fd < 0) { - if (sock_fd * -1 == ECONNREFUSED) { - throw EXCEPT_CONNREFUSED; - } - } +/* Client class - Inherits from 'Sock' class - Defines a TCP/UDP client. */ - } +class Client : public Sock { public: Client() {} Client(int ip_ver, char protocol, const char* address, int port) : Sock(ip_ver, protocol, address, port) {} + void create_socket() override; + + void sendAll(std::string to_send); + + std::string recvAll(); }; #endif