2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-16 15:08:17 -05:00
|
|
|
// Windows and MacOSX don't have the MSG_NOSIGNAL flag
|
|
|
|
#if ( \
|
|
|
|
defined(_WIN32) || \
|
|
|
|
(defined(__APPLE__) && defined(__MACH__)) \
|
|
|
|
)
|
|
|
|
#define MSG_NOSIGNAL (0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-01-25 08:51:03 -05:00
|
|
|
#include "Errors.h"
|
2014-01-16 15:08:17 -05:00
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
class cSocket
|
|
|
|
{
|
|
|
|
public:
|
2013-03-05 04:53:29 -05:00
|
|
|
enum eFamily
|
|
|
|
{
|
|
|
|
IPv4 = AF_INET,
|
|
|
|
IPv6 = AF_INET6,
|
2014-01-27 15:27:13 -05:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
ErrWouldBlock = WSAEWOULDBLOCK,
|
|
|
|
#else
|
|
|
|
ErrWouldBlock = EWOULDBLOCK,
|
|
|
|
#endif
|
2013-03-05 04:53:29 -05:00
|
|
|
} ;
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
typedef SOCKET xSocket;
|
|
|
|
#else
|
|
|
|
typedef int xSocket;
|
|
|
|
static const int INVALID_SOCKET = -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cSocket(void) : m_Socket(INVALID_SOCKET) {}
|
|
|
|
cSocket(xSocket a_Socket);
|
|
|
|
|
2012-11-22 09:10:21 -05:00
|
|
|
bool IsValid(void) const { return IsValidSocket(m_Socket); }
|
2013-03-05 04:53:29 -05:00
|
|
|
void CloseSocket(void);
|
2014-01-19 13:31:43 -05:00
|
|
|
|
|
|
|
/** Notifies the socket that we don't expect any more reads nor writes on it.
|
|
|
|
Most TCPIP implementations use this to send the FIN flag in a packet */
|
|
|
|
void ShutdownReadWrite(void);
|
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
operator xSocket(void) const;
|
|
|
|
xSocket GetSocket(void) const;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
bool operator == (const cSocket & a_Other) {return m_Socket == a_Other.m_Socket; }
|
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
void SetSocket(xSocket a_Socket);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
/// Sets the address-reuse socket flag; returns true on success
|
|
|
|
bool SetReuseAddress(void);
|
|
|
|
|
2013-11-24 08:28:51 -05:00
|
|
|
/// Initializes the network stack. Returns 0 on success, or another number as an error code.
|
2013-03-05 04:53:29 -05:00
|
|
|
static int WSAStartup(void);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
static int GetLastError();
|
|
|
|
static AString GetLastErrorString(void)
|
|
|
|
{
|
2014-01-25 08:51:03 -05:00
|
|
|
return GetOSErrorString(GetLastError());
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
/// Creates a new socket of the specified address family
|
|
|
|
static cSocket CreateSocket(eFamily a_Family);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
inline static bool IsSocketError(int a_ReturnedValue)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-11-22 09:10:21 -05:00
|
|
|
#ifdef _WIN32
|
|
|
|
return (a_ReturnedValue == SOCKET_ERROR || a_ReturnedValue == 0);
|
|
|
|
#else
|
|
|
|
return (a_ReturnedValue <= 0);
|
|
|
|
#endif
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-11-22 09:10:21 -05:00
|
|
|
|
|
|
|
static bool IsValidSocket(xSocket a_Socket);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-03-04 13:40:48 -05:00
|
|
|
static const unsigned short ANY_PORT = 0; // When given to Bind() functions, they will find a free port
|
2013-03-04 16:13:08 -05:00
|
|
|
static const int DEFAULT_BACKLOG = 10;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
/// Binds to the specified port on "any" interface (0.0.0.0). Returns true if successful.
|
|
|
|
bool BindToAnyIPv4(unsigned short a_Port);
|
|
|
|
|
|
|
|
/// Binds to the specified port on "any" interface (::/128). Returns true if successful.
|
|
|
|
bool BindToAnyIPv6(unsigned short a_Port);
|
2013-03-04 13:40:48 -05:00
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
/// Binds to the specified port on localhost interface (127.0.0.1) through IPv4. Returns true if successful.
|
|
|
|
bool BindToLocalhostIPv4(unsigned short a_Port);
|
2013-03-04 13:40:48 -05:00
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
/// Sets the socket to listen for incoming connections. Returns true if successful.
|
|
|
|
bool Listen(int a_Backlog = DEFAULT_BACKLOG);
|
2013-03-04 13:40:48 -05:00
|
|
|
|
2013-03-05 08:35:19 -05:00
|
|
|
/// Accepts an IPv4 incoming connection. Blocks if none available.
|
|
|
|
cSocket AcceptIPv4(void);
|
|
|
|
|
|
|
|
/// Accepts an IPv6 incoming connection. Blocks if none available.
|
|
|
|
cSocket AcceptIPv6(void);
|
2013-03-04 16:13:08 -05:00
|
|
|
|
2013-03-05 04:53:29 -05:00
|
|
|
/// Connects to a localhost socket on the specified port using IPv4; returns true if successful.
|
|
|
|
bool ConnectToLocalhostIPv4(unsigned short a_Port);
|
|
|
|
|
|
|
|
/// Connects to the specified host or string IP address and port, using IPv4. Returns true if successful.
|
|
|
|
bool ConnectIPv4(const AString & a_HostNameOrAddr, unsigned short a_Port);
|
2013-03-04 16:13:08 -05:00
|
|
|
|
2014-05-08 14:16:35 -04:00
|
|
|
int Receive(char * a_Buffer, size_t a_Length, unsigned int a_Flags);
|
|
|
|
int Send (const char * a_Buffer, size_t a_Length);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
unsigned short GetPort(void) const; // Returns 0 on failure
|
|
|
|
|
|
|
|
const AString & GetIPString(void) const { return m_IPString; }
|
2014-01-27 15:27:13 -05:00
|
|
|
|
|
|
|
/** Sets the socket into non-blocking mode */
|
|
|
|
void SetNonBlocking(void);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
xSocket m_Socket;
|
|
|
|
AString m_IPString;
|
2014-01-25 08:51:03 -05:00
|
|
|
};
|