Abstracted sockets some more to ensure the same behavior over the entire program and on multiple platforms.
MCSocket.h should soon be deprecated and deleted Do a full rebuild (mine bugged out when I didn't) git-svn-id: http://mc-server.googlecode.com/svn/trunk@8 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
parent
ad119ce556
commit
cf94994f8e
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <WinSock.h>
|
#include <WinSock.h>
|
||||||
// #define socklen_t int
|
// #define socklen_t int
|
||||||
|
@ -1,11 +1,3 @@
|
|||||||
#ifndef _WIN32
|
|
||||||
#include <cstring>
|
|
||||||
#include <semaphore.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "MCSocket.h"
|
|
||||||
|
|
||||||
#include "cClientHandle.h"
|
#include "cClientHandle.h"
|
||||||
#include "cServer.h"
|
#include "cServer.h"
|
||||||
#include "cWorld.h"
|
#include "cWorld.h"
|
||||||
@ -1023,7 +1015,7 @@ void cClientHandle::ReceiveThread( void *lpParam )
|
|||||||
|
|
||||||
while( self->m_bKeepThreadGoing )
|
while( self->m_bKeepThreadGoing )
|
||||||
{
|
{
|
||||||
iStat = recv(socket, &temp, 1, 0);
|
iStat = socket.Receive( &temp, 1, 0 );
|
||||||
if( cSocket::IsSocketError(iStat) || iStat == 0 )
|
if( cSocket::IsSocketError(iStat) || iStat == 0 )
|
||||||
{
|
{
|
||||||
LOG("CLIENT DISCONNECTED (%i bytes):%s", iStat, GetWSAError().c_str() );
|
LOG("CLIENT DISCONNECTED (%i bytes):%s", iStat, GetWSAError().c_str() );
|
||||||
@ -1041,11 +1033,7 @@ void cClientHandle::ReceiveThread( void *lpParam )
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
#ifndef _WIN32
|
LOGERROR("Something went wrong during PacketID 0x%02x (%s)", temp, cSocket::GetLastErrorString() );
|
||||||
LOGERROR("Something went wrong during PacketID 0x%02x (%i)", temp, errno );
|
|
||||||
#else
|
|
||||||
LOGERROR("Something went wrong during PacketID 0x%02x (%s)", temp, GetWSAError().c_str() );
|
|
||||||
#endif
|
|
||||||
LOG("CLIENT %s DISCONNECTED", self->GetUsername() );
|
LOG("CLIENT %s DISCONNECTED", self->GetUsername() );
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -1056,11 +1044,7 @@ void cClientHandle::ReceiveThread( void *lpParam )
|
|||||||
|
|
||||||
|
|
||||||
char c_Str[128];
|
char c_Str[128];
|
||||||
#ifdef _WIN32
|
sprintf_s( c_Str, 128, "[C->S] Unknown PacketID: 0x%2x", (unsigned char)temp );
|
||||||
sprintf_s( c_Str, "[C->S] Unknown PacketID: 0x%2x", (unsigned char)temp );
|
|
||||||
#else
|
|
||||||
sprintf( c_Str, "[C->S] Unknown PacketID: 0x%2x", (unsigned char)temp );
|
|
||||||
#endif
|
|
||||||
cPacket_Disconnect DC(c_Str);
|
cPacket_Disconnect DC(c_Str);
|
||||||
DC.Send( socket );
|
DC.Send( socket );
|
||||||
|
|
||||||
|
@ -29,10 +29,7 @@
|
|||||||
#include "packets/cPacket_Chat.h"
|
#include "packets/cPacket_Chat.h"
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
#include <cstring>
|
#define sprintf_s(dst, size, format, ...) sprintf(dst, format, __VA_ARGS__ )
|
||||||
#include <errno.h>
|
|
||||||
#include <semaphore.h>
|
|
||||||
#include "MCSocket.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -150,64 +147,40 @@ bool cServer::InitServer( int a_Port )
|
|||||||
|
|
||||||
LOG("Starting up server.");
|
LOG("Starting up server.");
|
||||||
|
|
||||||
#ifdef _WIN32
|
if( cSocket::WSAStartup() != 0 ) // Only does anything on Windows, but whatever
|
||||||
WSADATA wsaData;
|
|
||||||
memset( &wsaData, 0, sizeof( wsaData ) );
|
|
||||||
int wsaret=WSAStartup(/*0x101*/ MAKEWORD(2, 2),&wsaData);
|
|
||||||
|
|
||||||
if(wsaret!=0)
|
|
||||||
{
|
{
|
||||||
LOG("wsaret != 0");
|
LOGERROR("WSAStartup() != 0");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
sockaddr_in local;
|
m_pState->SListenClient = cSocket::CreateSocket();
|
||||||
|
|
||||||
local.sin_family=AF_INET;
|
|
||||||
local.sin_addr.s_addr=INADDR_ANY;
|
|
||||||
local.sin_port=htons((u_short)a_Port); // 25565
|
|
||||||
|
|
||||||
m_pState->SListenClient = socket(AF_INET,SOCK_STREAM,0);
|
|
||||||
|
|
||||||
if( !m_pState->SListenClient.IsValid() )
|
if( !m_pState->SListenClient.IsValid() )
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
LOGERROR("m_SListenClient==INVALID_SOCKET (%s)", cSocket::GetLastErrorString() );
|
||||||
LOGERROR("m_SListenClient==INVALID_SOCKET (%s)", GetWSAError().c_str() );
|
return false;
|
||||||
#else
|
|
||||||
LOGERROR("m_SListenClient==INVALID_SOCKET");
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( m_pState->SListenClient.SetReuseAddress() == -1 )
|
||||||
#ifdef _WIN32
|
{
|
||||||
char yes = 1;
|
|
||||||
#else
|
|
||||||
int yes = 1;
|
|
||||||
#endif
|
|
||||||
if (setsockopt( m_pState->SListenClient, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
|
|
||||||
LOGERROR("setsockopt == -1");
|
LOGERROR("setsockopt == -1");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if( bind( m_pState->SListenClient, (sockaddr*)&local, sizeof(local)) != 0 )
|
cSocket::SockAddr_In local;
|
||||||
|
local.Family = cSocket::ADDRESS_FAMILY_INTERNET;
|
||||||
|
local.Address = cSocket::INTERNET_ADDRESS_ANY;
|
||||||
|
local.Port = (unsigned short)a_Port; // 25565
|
||||||
|
|
||||||
|
if( m_pState->SListenClient.Bind( local ) != 0 )
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
LOGERROR("bind fail (%s)", cSocket::GetLastErrorString() );
|
||||||
LOGERROR("bind fail (%s)", GetWSAError().c_str() );
|
|
||||||
#else
|
|
||||||
LOGERROR("bind fail (%i)", errno);
|
|
||||||
#endif
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( m_pState->SListenClient.Listen( 10 ) != 0)
|
||||||
if( listen( m_pState->SListenClient , 10 ) != 0)
|
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
LOGERROR("listen fail (%s)", cSocket::GetLastErrorString() );
|
||||||
LOGERROR("listen fail (%s)", GetWSAError().c_str() );
|
|
||||||
#else
|
|
||||||
LOGERROR("listen fail (%i)", errno);
|
|
||||||
#endif
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -243,7 +216,7 @@ cServer::cServer()
|
|||||||
|
|
||||||
cServer::~cServer()
|
cServer::~cServer()
|
||||||
{
|
{
|
||||||
if( m_pState->SListenClient ) closesocket( m_pState->SListenClient );
|
if( m_pState->SListenClient ) m_pState->SListenClient.CloseSocket();
|
||||||
m_pState->SListenClient = 0;
|
m_pState->SListenClient = 0;
|
||||||
|
|
||||||
m_pState->bStopListenThread = true;
|
m_pState->bStopListenThread = true;
|
||||||
@ -277,24 +250,17 @@ void cServer::SendAllEntitiesTo(cClientHandle* a_Target)
|
|||||||
|
|
||||||
void cServer::StartListenClient()
|
void cServer::StartListenClient()
|
||||||
{
|
{
|
||||||
sockaddr_in from;
|
cSocket SClient = m_pState->SListenClient.Accept();
|
||||||
socklen_t fromlen=sizeof(from);
|
|
||||||
|
|
||||||
cSocket SClient = accept(
|
if( SClient.IsValid() )
|
||||||
m_pState->SListenClient,
|
|
||||||
(sockaddr*)&from,
|
|
||||||
&fromlen);
|
|
||||||
|
|
||||||
if( from.sin_addr.s_addr && SClient.IsValid() )
|
|
||||||
{
|
{
|
||||||
char * ClientIP = 0;
|
char * ClientIP = SClient.GetIPString();
|
||||||
if((ClientIP = inet_ntoa(from.sin_addr)) == 0 )
|
if( ClientIP == 0 )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
LOG("%s connected!", ClientIP);
|
LOG("%s connected!", ClientIP);
|
||||||
|
|
||||||
cClientHandle *NewHandle = 0;
|
cClientHandle *NewHandle = new cClientHandle( SClient );
|
||||||
NewHandle = new cClientHandle( SClient );
|
|
||||||
cWorld* World = cRoot::Get()->GetWorld();
|
cWorld* World = cRoot::Get()->GetWorld();
|
||||||
World->LockClientHandle();
|
World->LockClientHandle();
|
||||||
World->AddClient( NewHandle );
|
World->AddClient( NewHandle );
|
||||||
|
@ -1,12 +1,20 @@
|
|||||||
#include "cSocket.h"
|
#include "cSocket.h"
|
||||||
|
#include "cMCLogger.h"
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
//#include <sys/socket.h>
|
||||||
|
//#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h> //inet_ntoa()
|
||||||
|
#else
|
||||||
|
#define socklen_t int
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
cSocket::cSocket( xSocket a_Socket )
|
cSocket::cSocket( xSocket a_Socket )
|
||||||
: m_Socket( a_Socket )
|
: m_Socket( a_Socket )
|
||||||
|
, m_IPString( 0 )
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +46,106 @@ void cSocket::CloseSocket()
|
|||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
closesocket(m_Socket);
|
closesocket(m_Socket);
|
||||||
#else
|
#else
|
||||||
shutdown(m_Socket, SHUT_RDWR);//SD_BOTH);
|
if( shutdown(m_Socket, SHUT_RDWR) != 0 )//SD_BOTH);
|
||||||
close(m_Socket);
|
LOGWARN("Error on shutting down socket (%s)", m_IPString );
|
||||||
|
if( close(m_Socket) != 0 )
|
||||||
|
LOGWARN("Error closing socket (%s)", m_IPString );
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
int cSocket::SetReuseAddress()
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
char yes = 1;
|
||||||
|
#else
|
||||||
|
int yes = 1;
|
||||||
|
#endif
|
||||||
|
return setsockopt( m_Socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int) );
|
||||||
|
}
|
||||||
|
|
||||||
|
int cSocket::WSAStartup()
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
WSADATA wsaData;
|
||||||
|
memset( &wsaData, 0, sizeof( wsaData ) );
|
||||||
|
return ::WSAStartup( MAKEWORD(2, 2),&wsaData);
|
||||||
|
#else
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
cSocket cSocket::CreateSocket()
|
||||||
|
{
|
||||||
|
return socket(AF_INET,SOCK_STREAM,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
int cSocket::Listen( int a_Backlog )
|
||||||
|
{
|
||||||
|
return listen( m_Socket, a_Backlog );
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cSocket::Receive( char* a_Buffer, unsigned int a_Length, unsigned int a_Flags )
|
||||||
|
{
|
||||||
|
return recv(m_Socket, a_Buffer, a_Length, a_Flags);
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#include <WinSock.h>
|
#include <WinSock.h>
|
||||||
#define socklen_t int
|
|
||||||
#ifdef SendMessage
|
#ifdef SendMessage
|
||||||
#undef SendMessage
|
#undef SendMessage
|
||||||
#endif
|
#endif
|
||||||
@ -30,6 +30,12 @@ public:
|
|||||||
xSocket GetSocket() const;
|
xSocket GetSocket() const;
|
||||||
void SetSocket( xSocket a_Socket );
|
void SetSocket( xSocket a_Socket );
|
||||||
|
|
||||||
|
int SetReuseAddress();
|
||||||
|
static int WSAStartup();
|
||||||
|
|
||||||
|
static const char* GetLastErrorString();
|
||||||
|
static cSocket CreateSocket();
|
||||||
|
|
||||||
inline static bool IsSocketError( int a_ReturnedValue )
|
inline static bool IsSocketError( int a_ReturnedValue )
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -39,7 +45,24 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 );
|
||||||
|
|
||||||
|
char* GetIPString() { return m_IPString; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
xSocket m_Socket;
|
xSocket m_Socket;
|
||||||
|
char* m_IPString;
|
||||||
};
|
};
|
@ -5,10 +5,10 @@
|
|||||||
#include "MCSocket.h"
|
#include "MCSocket.h"
|
||||||
|
|
||||||
#include "cMCLogger.h"
|
#include "cMCLogger.h"
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <errno.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -66,11 +66,7 @@ bool cTCPLink::Connect( const char* a_Address, unsigned int a_Port )
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_Socket=socket(AF_INET,SOCK_STREAM,0);
|
m_Socket=socket(AF_INET,SOCK_STREAM,0);
|
||||||
#ifdef _WIN32
|
if( !m_Socket.IsValid() )
|
||||||
if( m_Socket==INVALID_SOCKET )
|
|
||||||
#else
|
|
||||||
if( m_Socket < 0 )
|
|
||||||
#endif
|
|
||||||
{
|
{
|
||||||
LOGERROR("cTCPLink: Invalid socket");
|
LOGERROR("cTCPLink: Invalid socket");
|
||||||
m_Socket = 0;
|
m_Socket = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user