2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
#include "cSocket.h"
|
|
|
|
|
2011-10-21 17:25:29 -04:00
|
|
|
#ifndef _WIN32
|
2012-01-29 14:28:19 -05:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
// #include <sys/socket.h>
|
|
|
|
// #include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h> //inet_ntoa()
|
2011-10-22 20:18:44 -04:00
|
|
|
#else
|
2012-01-29 14:28:19 -05:00
|
|
|
#define socklen_t int
|
2011-10-21 17:25:29 -04:00
|
|
|
#endif
|
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cSocket::cSocket( xSocket a_Socket )
|
|
|
|
: m_Socket( a_Socket )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cSocket::~cSocket()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cSocket::operator const cSocket::xSocket() const
|
|
|
|
{
|
|
|
|
return m_Socket;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
cSocket::xSocket cSocket::GetSocket() const
|
|
|
|
{
|
|
|
|
return m_Socket;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-03 14:41:19 -04:00
|
|
|
|
2011-10-21 17:25:29 -04:00
|
|
|
void cSocket::CloseSocket()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
closesocket(m_Socket);
|
|
|
|
#else
|
2011-10-22 20:18:44 -04:00
|
|
|
if( shutdown(m_Socket, SHUT_RDWR) != 0 )//SD_BOTH);
|
2012-02-02 02:47:19 -05:00
|
|
|
LOGWARN("Error on shutting down socket (%s)", m_IPString.c_str() );
|
2011-10-22 20:18:44 -04:00
|
|
|
if( close(m_Socket) != 0 )
|
2012-02-02 02:47:19 -05:00
|
|
|
LOGWARN("Error closing socket (%s)", m_IPString.c_str() );
|
2011-10-21 17:25:29 -04:00
|
|
|
#endif
|
2011-10-22 20:18:44 -04:00
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
const char* cSocket::GetLastErrorString()
|
|
|
|
{
|
|
|
|
#define CASE_AND_RETURN( x ) case x: return #x
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
switch( WSAGetLastError() )
|
|
|
|
{
|
|
|
|
CASE_AND_RETURN( WSANOTINITIALISED );
|
|
|
|
CASE_AND_RETURN( WSAENETDOWN );
|
|
|
|
CASE_AND_RETURN( WSAEFAULT );
|
|
|
|
CASE_AND_RETURN( WSAENOTCONN );
|
|
|
|
CASE_AND_RETURN( WSAEINTR );
|
|
|
|
CASE_AND_RETURN( WSAEINPROGRESS );
|
|
|
|
CASE_AND_RETURN( WSAENETRESET );
|
|
|
|
CASE_AND_RETURN( WSAENOTSOCK );
|
|
|
|
CASE_AND_RETURN( WSAEOPNOTSUPP );
|
|
|
|
CASE_AND_RETURN( WSAESHUTDOWN );
|
|
|
|
CASE_AND_RETURN( WSAEWOULDBLOCK );
|
|
|
|
CASE_AND_RETURN( WSAEMSGSIZE );
|
|
|
|
CASE_AND_RETURN( WSAEINVAL );
|
|
|
|
CASE_AND_RETURN( WSAECONNABORTED );
|
|
|
|
CASE_AND_RETURN( WSAETIMEDOUT );
|
|
|
|
CASE_AND_RETURN( WSAECONNRESET );
|
|
|
|
}
|
|
|
|
return "No Error";
|
|
|
|
#else
|
|
|
|
return "GetLastErrorString() only works on Windows";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
int cSocket::SetReuseAddress()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
char yes = 1;
|
|
|
|
#else
|
|
|
|
int yes = 1;
|
|
|
|
#endif
|
|
|
|
return setsockopt( m_Socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int) );
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
int cSocket::WSAStartup()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSADATA wsaData;
|
|
|
|
memset( &wsaData, 0, sizeof( wsaData ) );
|
|
|
|
return ::WSAStartup( MAKEWORD(2, 2),&wsaData);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
cSocket cSocket::CreateSocket()
|
|
|
|
{
|
|
|
|
return socket(AF_INET,SOCK_STREAM,0);
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
int cSocket::Bind( SockAddr_In& a_Address )
|
|
|
|
{
|
|
|
|
sockaddr_in local;
|
|
|
|
|
|
|
|
if( a_Address.Family == ADDRESS_FAMILY_INTERNET )
|
|
|
|
local.sin_family = AF_INET;
|
|
|
|
|
|
|
|
if( a_Address.Address == INTERNET_ADDRESS_ANY )
|
|
|
|
local.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
local.sin_port=htons( (u_short)a_Address.Port );
|
|
|
|
|
|
|
|
return bind( m_Socket, (sockaddr*)&local, sizeof(local));
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
int cSocket::Listen( int a_Backlog )
|
|
|
|
{
|
|
|
|
return listen( m_Socket, a_Backlog );
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
cSocket cSocket::Accept()
|
|
|
|
{
|
|
|
|
sockaddr_in from;
|
|
|
|
socklen_t fromlen=sizeof(from);
|
|
|
|
|
|
|
|
cSocket SClient = accept( m_Socket, (sockaddr*)&from, &fromlen);
|
|
|
|
|
|
|
|
if( from.sin_addr.s_addr && SClient.IsValid() ) // Get IP in string form
|
|
|
|
{
|
|
|
|
SClient.m_IPString = inet_ntoa(from.sin_addr);
|
|
|
|
//LOG("cSocket::Accept() %s", SClient.m_IPString );
|
|
|
|
}
|
|
|
|
|
|
|
|
return SClient;
|
|
|
|
}
|
|
|
|
|
2012-02-01 17:38:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-10-22 20:18:44 -04:00
|
|
|
int cSocket::Receive( char* a_Buffer, unsigned int a_Length, unsigned int a_Flags )
|
|
|
|
{
|
|
|
|
return recv(m_Socket, a_Buffer, a_Length, a_Flags);
|
2012-02-01 17:38:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|