2011-10-03 14:41:19 -04:00
|
|
|
#pragma once
|
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
|
2011-10-21 17:25:29 -04:00
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
class cSocket
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
typedef SOCKET xSocket;
|
|
|
|
#else
|
|
|
|
typedef int xSocket;
|
2012-02-02 11:13:49 -05:00
|
|
|
static const int INVALID_SOCKET = -1;
|
2011-10-03 14:41:19 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
2012-02-01 17:38:03 -05:00
|
|
|
cSocket(void) : m_Socket(INVALID_SOCKET) {}
|
|
|
|
cSocket(xSocket a_Socket);
|
2011-10-03 14:41:19 -04:00
|
|
|
~cSocket();
|
|
|
|
|
2012-02-02 11:13:49 -05:00
|
|
|
bool IsValid(void) const;
|
2011-10-21 17:25:29 -04:00
|
|
|
void CloseSocket();
|
2011-10-03 14:41:19 -04:00
|
|
|
|
|
|
|
operator const xSocket() const;
|
|
|
|
xSocket GetSocket() const;
|
|
|
|
void SetSocket( xSocket a_Socket );
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
int SetReuseAddress();
|
|
|
|
static int WSAStartup();
|
|
|
|
|
2012-02-05 11:24:16 -05:00
|
|
|
static AString GetErrorString( int a_ErrNo );
|
|
|
|
static int GetLastError();
|
2011-10-22 20:18:44 -04:00
|
|
|
static cSocket CreateSocket();
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
inline static bool IsSocketError( int a_ReturnedValue )
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return (a_ReturnedValue == SOCKET_ERROR || a_ReturnedValue == 0);
|
|
|
|
#else
|
|
|
|
return (a_ReturnedValue <= 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
struct SockAddr_In
|
|
|
|
{
|
|
|
|
short Family;
|
|
|
|
unsigned short Port;
|
|
|
|
unsigned long Address;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const short ADDRESS_FAMILY_INTERNET = 2;
|
|
|
|
static const unsigned long INTERNET_ADDRESS_ANY = 0;
|
|
|
|
|
|
|
|
int Bind( SockAddr_In& a_Address );
|
|
|
|
int Listen( int a_Backlog );
|
|
|
|
cSocket Accept();
|
|
|
|
int Receive( char* a_Buffer, unsigned int a_Length, unsigned int a_Flags );
|
|
|
|
|
2012-02-02 02:47:19 -05:00
|
|
|
const AString & GetIPString(void) const { return m_IPString; }
|
2011-10-22 20:18:44 -04:00
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
private:
|
|
|
|
xSocket m_Socket;
|
2012-02-02 02:47:19 -05:00
|
|
|
AString m_IPString;
|
2011-10-03 14:41:19 -04:00
|
|
|
};
|