1
0

cSocket: Added more functions that will be needed for the new cSocketThreads

git-svn-id: http://mc-server.googlecode.com/svn/trunk@237 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2012-02-07 07:44:00 +00:00
parent 12fd317a7b
commit 5750fbf65f
2 changed files with 56 additions and 8 deletions

View File

@ -17,6 +17,12 @@
unsigned long cSocket::INTERNET_ADDRESS_LOCALHOST = htonl((127 << 24) | 1);
cSocket::cSocket(xSocket a_Socket)
: m_Socket(a_Socket)
{
@ -194,13 +200,9 @@ 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);
local.sin_family = a_Address.Family;
local.sin_addr.s_addr = a_Address.Address;
local.sin_port = htons((u_short)a_Address.Port);
return bind(m_Socket, (sockaddr*)&local, sizeof(local));
}
@ -238,6 +240,21 @@ cSocket cSocket::Accept()
int cSocket::Connect(SockAddr_In & a_Address)
{
sockaddr_in local;
local.sin_family = a_Address.Family;
local.sin_addr.s_addr = a_Address.Address;
local.sin_port = htons((u_short)a_Address.Port);
return connect(m_Socket, (sockaddr *)&local, sizeof(local));
}
int cSocket::Receive(char* a_Buffer, unsigned int a_Length, unsigned int a_Flags)
{
return recv(m_Socket, a_Buffer, a_Length, a_Flags);
@ -246,3 +263,29 @@ int cSocket::Receive(char* a_Buffer, unsigned int a_Length, unsigned int a_Flags
int cSocket::Send(const char * a_Buffer, unsigned int a_Length)
{
return send(m_Socket, a_Buffer, a_Length, 0);
}
unsigned short cSocket::GetPort(void) const
{
assert(IsValid());
sockaddr_in Addr;
socklen_t AddrSize = sizeof(Addr);
if (getsockname(m_Socket, (sockaddr *)&Addr, &AddrSize) != 0)
{
return 0;
}
return Addr.sin_port;
}

View File

@ -6,6 +6,7 @@
class cSocket
{
public:
#ifdef _WIN32
typedef SOCKET xSocket;
#else
@ -13,7 +14,6 @@ class cSocket
static const int INVALID_SOCKET = -1;
#endif
public:
cSocket(void) : m_Socket(INVALID_SOCKET) {}
cSocket(xSocket a_Socket);
~cSocket();
@ -55,11 +55,16 @@ public:
static const short ADDRESS_FAMILY_INTERNET = 2;
static const unsigned long INTERNET_ADDRESS_ANY = 0;
static unsigned long INTERNET_ADDRESS_LOCALHOST; // 127.0.0.1 represented in network byteorder
int Bind( SockAddr_In& a_Address );
int Listen( int a_Backlog );
cSocket Accept();
int Connect(SockAddr_In & a_Address); // Returns 0 on success, !0 on failure
int Receive( char* a_Buffer, unsigned int a_Length, unsigned int a_Flags );
int Send (const char * a_Buffer, unsigned int a_Length);
unsigned short GetPort(void) const; // Returns 0 on failure
const AString & GetIPString(void) const { return m_IPString; }