diff --git a/includes/client.hpp b/includes/client.hpp index a82ab1a..eb1dd7d 100644 --- a/includes/client.hpp +++ b/includes/client.hpp @@ -8,8 +8,13 @@ class Client : public Sock { 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) {} void create_socket() override; @@ -17,5 +22,8 @@ public: void sendAll(std::string to_send); std::string recvAll(); + + /* Return the type of socket */ + int get_type() override; }; #endif diff --git a/includes/server.hpp b/includes/server.hpp index b5a45a7..0c70665 100644 --- a/includes/server.hpp +++ b/includes/server.hpp @@ -7,6 +7,10 @@ It inherits from the 'Sock' class. */ class Server : public Sock { +private: + /* Address of the peer/client */ + std::string peer_addr; + public: /* Constructors */ Server() {} @@ -29,5 +33,11 @@ public: /* Receive data from peer socket */ std::string recvAll(); + + /* Return the address of the peer */ + std::string get_peer_addr(); + + /* Return the type of socket */ + int get_type() override; }; #endif diff --git a/includes/sock.hpp b/includes/sock.hpp index d25e656..9cd6e94 100644 --- a/includes/sock.hpp +++ b/includes/sock.hpp @@ -4,8 +4,13 @@ #include #include +/* 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 { -/* 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: int ip_ver; char protocol; @@ -13,8 +18,10 @@ protected: int sock_fd; std::string address; 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: @@ -24,14 +31,18 @@ public: /* Regular constructor - defined in sock.cpp */ 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); - /* Method to receive data sent to socket - defined in sock.cpp */ + /* Method to receive data sent to the 'other_socket' socket */ std::string recvAll(); - /* Returns socket identifier - defined in sock.cpp */ + /* Returns socket identifier */ 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