Updated header files to reflect recent change in implementation files

master
Aadhavan Srinivasan 10 months ago
parent 4fce513211
commit f0e30f28e9

@ -8,8 +8,13 @@
class Client : public Sock { class Client : public Sock {
public: public:
Client() {} /* Default constructor - Does nothing */
Client() {};
/* Destructor - defined in client.cpp */
~Client();
/* Normal constructor that calls the parent constructor to set the given values */
Client(int ip_ver, char protocol, const char* address, int port) : Sock(ip_ver, protocol, address, port) {} Client(int ip_ver, char protocol, const char* address, int port) : Sock(ip_ver, protocol, address, port) {}
void create_socket() override; void create_socket() override;
@ -17,5 +22,8 @@ public:
void sendAll(std::string to_send); void sendAll(std::string to_send);
std::string recvAll(); std::string recvAll();
/* Return the type of socket */
int get_type() override;
}; };
#endif #endif

@ -7,6 +7,10 @@ It inherits from the 'Sock' class. */
class Server : public Sock { class Server : public Sock {
private:
/* Address of the peer/client */
std::string peer_addr;
public: public:
/* Constructors */ /* Constructors */
Server() {} Server() {}
@ -29,5 +33,11 @@ public:
/* Receive data from peer socket */ /* Receive data from peer socket */
std::string recvAll(); std::string recvAll();
/* Return the address of the peer */
std::string get_peer_addr();
/* Return the type of socket */
int get_type() override;
}; };
#endif #endif

@ -4,8 +4,13 @@
#include <string> #include <string>
#include <sys/socket.h> #include <sys/socket.h>
/* Global constants - can be used by children classes as return values, and by any clients to check what type the socket is of */
const int SOCK_CLIENT = 'C';
const int SOCK_SERVER = 'S';
/* Abstract class for a Socket. 'Client' and 'Server' derive from
this class, and extend the functions defined here. */
class Sock { 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. */
protected: protected:
int ip_ver; int ip_ver;
char protocol; char protocol;
@ -13,8 +18,10 @@ protected:
int sock_fd; int sock_fd;
std::string address; std::string address;
struct sockaddr* dest; struct sockaddr* dest;
socklen_t addrlen;
int other_socket; // The peer socket (the client if this socket is a server, and the server if this socket is a client) */
void create_socket(); virtual void create_socket();
public: public:
@ -24,14 +31,18 @@ public:
/* Regular constructor - defined in sock.cpp */ /* Regular constructor - defined in sock.cpp */
Sock(int ip_ver, char protocol, const char* address, int port); Sock(int ip_ver, char protocol, const char* address, int port);
/* Method to send data in 'to_send' to the destination - defined in sock.cpp */ /* Method to send data in 'to_send' through the 'other_socket' socket */
void sendAll(std::string to_send); void sendAll(std::string to_send);
/* Method to receive data sent to socket - defined in sock.cpp */ /* Method to receive data sent to the 'other_socket' socket */
std::string recvAll(); std::string recvAll();
/* Returns socket identifier - defined in sock.cpp */ /* Returns socket identifier */
int getSockFD(); int getSockFD();
/* This is a pure virtual function (AKA an abstract function). It's purpose
is to be redefined by the children classes (client and server). */
virtual int get_type() = 0;
}; };
#endif #endif

Loading…
Cancel
Save