Completely rewrote Server class, and split the class into header and implementation

This commit is contained in:
2024-02-19 21:28:07 -05:00
parent 98160f0071
commit 98abf50da5
2 changed files with 88 additions and 16 deletions

View File

@@ -1,30 +1,33 @@
#ifndef _SERVER
#define _SERVER
#include "includes/sock.hpp"
#include "includes/exception_consts.hpp"
/*
Server class - Inherits from 'Sock' class - Defines a TCP/UDP server.
*/
/* Server class - This class is used to create a TCP/UDP server.
It inherits from the 'Sock' class. */
class Server : public Sock {
private:
void create_socket() {
Sock::create_socket();
this->sock_fd = create_local(this->ip_ver, this->protocol, this->address.data(), this->port, dest);
if (sock_fd < 0) {
if (sock_fd * -1 == ECONNREFUSED) {
throw EXCEPT_CONNREFUSED;
}
}
}
public:
/* Constructors */
Server() {}
Server(int ip_ver, char protocol, const char* address, int port) : Sock(ip_ver, protocol, address, port) {}
/* Destructor */
~Server();
/* Method to create socket - overrides (i.e. extends) method from parent class */
void create_socket() override;
/* FOR TCP ONLY - Wait for a peer to connect to this socket, and store the result in 'other_socket' */
void wait_for_peer();
/* FOR TCP - Send data to the peer socket
FOR UDP - Send data to the client, from which data was received.
FOR UDP, this function MUST be called after recvAll() */
void sendAll(std::string to_send);
/* Receive data from peer socket */
std::string recvAll();
};
#endif