You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.7 KiB
C++

#ifndef _SOCK_CLASS
#define _SOCK_CLASS
#include <string>
#if defined(__unix__) || defined(__unix) ||(defined(__APPLE__) && defined(__MACH__))
#include <sys/socket.h>
#endif
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
/* 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 {
protected:
int ip_ver;
char protocol;
int port;
int sock_fd;
std::string address;
struct sockaddr_storage* 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) */
virtual void create_socket();
public:
/* Default constructor */
Sock() {}
/* Virtual destructor */
virtual ~Sock();
/* Regular constructor - defined in sock.cpp */
Sock(char protocol, const char* address, int port);
/* Method to send data in 'to_send' through the 'other_socket' socket */
void sendAll(std::string to_send);
/* Same as method above, with buffer and buffer size */
void sendAll(char* buffer, int size);
/* Method to receive data sent to the 'other_socket' socket */
char* recvAll();
/* Non-blocking receive method - calls the method above after polling for data. Returns
an empty string if there is nothing to read. */
char* recvAllNB();
/* 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