cSocket API fix (possible invalid pointer)
cMCLogger slight change (CS ptr -> member) git-svn-id: http://mc-server.googlecode.com/svn/trunk@222 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
parent
48d30d6ab4
commit
865216b15a
@ -22,7 +22,6 @@ cMCLogger* cMCLogger::GetInstance()
|
|||||||
|
|
||||||
cMCLogger::cMCLogger()
|
cMCLogger::cMCLogger()
|
||||||
{
|
{
|
||||||
m_CriticalSection = new cCriticalSection();
|
|
||||||
AString FileName;
|
AString FileName;
|
||||||
Printf(FileName, "LOG_%d.txt", (int)time(0) );
|
Printf(FileName, "LOG_%d.txt", (int)time(0) );
|
||||||
m_Log = new cLog(FileName);
|
m_Log = new cLog(FileName);
|
||||||
@ -37,19 +36,25 @@ cMCLogger::cMCLogger()
|
|||||||
|
|
||||||
cMCLogger::cMCLogger( char* a_File )
|
cMCLogger::cMCLogger( char* a_File )
|
||||||
{
|
{
|
||||||
m_CriticalSection = new cCriticalSection();
|
|
||||||
m_Log = new cLog( a_File );
|
m_Log = new cLog( a_File );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cMCLogger::~cMCLogger()
|
cMCLogger::~cMCLogger()
|
||||||
{
|
{
|
||||||
m_Log->Log("--- Stopped Log ---");
|
m_Log->Log("--- Stopped Log ---");
|
||||||
delete m_Log;
|
delete m_Log;
|
||||||
delete m_CriticalSection;
|
if (this == s_MCLogger)
|
||||||
if( this == s_MCLogger )
|
s_MCLogger = NULL;
|
||||||
s_MCLogger = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ )
|
void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ )
|
||||||
{
|
{
|
||||||
switch( a_LogType )
|
switch( a_LogType )
|
||||||
@ -72,17 +77,25 @@ void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cMCLogger::Log(const char* a_Format, va_list a_ArgList)
|
void cMCLogger::Log(const char* a_Format, va_list a_ArgList)
|
||||||
{
|
{
|
||||||
m_CriticalSection->Lock();
|
cCSLock Lock(m_CriticalSection);
|
||||||
SetColor( 0x7 ); // 0x7 is default grey color
|
SetColor( 0x7 ); // 0x7 is default grey color
|
||||||
m_Log->Log( a_Format, a_ArgList );
|
m_Log->Log( a_Format, a_ArgList );
|
||||||
m_CriticalSection->Unlock();
|
SetColor(0x07); // revert color back
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cMCLogger::Info(const char* a_Format, va_list a_ArgList)
|
void cMCLogger::Info(const char* a_Format, va_list a_ArgList)
|
||||||
{
|
{
|
||||||
m_CriticalSection->Lock();
|
cCSLock Lock(m_CriticalSection);
|
||||||
// for( int i = 0; i < 16; i++)
|
// for( int i = 0; i < 16; i++)
|
||||||
// {
|
// {
|
||||||
// for( int j = 0; j < 16; j++ )
|
// for( int j = 0; j < 16; j++ )
|
||||||
@ -95,25 +108,37 @@ void cMCLogger::Info(const char* a_Format, va_list a_ArgList)
|
|||||||
|
|
||||||
SetColor( 0xe ); // 0xe is yellow
|
SetColor( 0xe ); // 0xe is yellow
|
||||||
m_Log->Log( a_Format, a_ArgList );
|
m_Log->Log( a_Format, a_ArgList );
|
||||||
m_CriticalSection->Unlock();
|
SetColor(0x07); // revert color back
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cMCLogger::Warn(const char* a_Format, va_list a_ArgList)
|
void cMCLogger::Warn(const char* a_Format, va_list a_ArgList)
|
||||||
{
|
{
|
||||||
m_CriticalSection->Lock();
|
cCSLock Lock(m_CriticalSection);
|
||||||
SetColor( 0xc ); // 0xc is red
|
SetColor( 0xc ); // 0xc is red
|
||||||
m_Log->Log( a_Format, a_ArgList );
|
m_Log->Log( a_Format, a_ArgList );
|
||||||
m_CriticalSection->Unlock();
|
SetColor(0x07); // revert color back
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cMCLogger::Error(const char* a_Format, va_list a_ArgList)
|
void cMCLogger::Error(const char* a_Format, va_list a_ArgList)
|
||||||
{
|
{
|
||||||
m_CriticalSection->Lock();
|
cCSLock Lock(m_CriticalSection);
|
||||||
SetColor( 0xc0 ); // 0xc0 is red bg and black text
|
SetColor( 0xc0 ); // 0xc0 is red bg and black text
|
||||||
m_Log->Log( a_Format, a_ArgList );
|
m_Log->Log( a_Format, a_ArgList );
|
||||||
m_CriticalSection->Unlock();
|
SetColor(0x07); // revert color back
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cMCLogger::SetColor( unsigned char a_Color )
|
void cMCLogger::SetColor( unsigned char a_Color )
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -125,6 +150,9 @@ void cMCLogger::SetColor( unsigned char a_Color )
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
// Global functions
|
// Global functions
|
||||||
void LOG(const char* a_Format, ...)
|
void LOG(const char* a_Format, ...)
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class cLog;
|
class cLog;
|
||||||
@ -27,7 +28,7 @@ public: //tolua_export
|
|||||||
private:
|
private:
|
||||||
void SetColor( unsigned char a_Color );
|
void SetColor( unsigned char a_Color );
|
||||||
|
|
||||||
cCriticalSection* m_CriticalSection;
|
cCriticalSection m_CriticalSection;
|
||||||
cLog* m_Log;
|
cLog* m_Log;
|
||||||
static cMCLogger* s_MCLogger;
|
static cMCLogger* s_MCLogger;
|
||||||
}; //tolua_export
|
}; //tolua_export
|
||||||
@ -36,3 +37,9 @@ extern void LOG(const char* a_Format, ...);
|
|||||||
extern void LOGINFO(const char* a_Format, ...);
|
extern void LOGINFO(const char* a_Format, ...);
|
||||||
extern void LOGWARN(const char* a_Format, ...);
|
extern void LOGWARN(const char* a_Format, ...);
|
||||||
extern void LOGERROR(const char* a_Format, ...);
|
extern void LOGERROR(const char* a_Format, ...);
|
||||||
|
|
||||||
|
#define LOGWARNING LOGWARN
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -920,6 +920,10 @@ const cPlayer::GroupList & cPlayer::GetGroups()
|
|||||||
return m_pState->Groups;
|
return m_pState->Groups;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cPlayer::StringList cPlayer::GetResolvedPermissions()
|
cPlayer::StringList cPlayer::GetResolvedPermissions()
|
||||||
{
|
{
|
||||||
StringList Permissions;
|
StringList Permissions;
|
||||||
@ -933,17 +937,31 @@ cPlayer::StringList cPlayer::GetResolvedPermissions()
|
|||||||
return Permissions;
|
return Permissions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const char* cPlayer::GetLoadedWorldName()
|
const char* cPlayer::GetLoadedWorldName()
|
||||||
{
|
{
|
||||||
return m_pState->LoadedWorldName.c_str();
|
return m_pState->LoadedWorldName.c_str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cPlayer::UseEquippedItem()
|
void cPlayer::UseEquippedItem()
|
||||||
{
|
{
|
||||||
if(GetGameMode() != 1) //No damage in creative
|
if(GetGameMode() != 1) //No damage in creative
|
||||||
|
{
|
||||||
if (GetInventory().GetEquippedItem().DamageItem())
|
if (GetInventory().GetEquippedItem().DamageItem())
|
||||||
{
|
{
|
||||||
LOG("Player %s Broke ID: %i", GetClientHandle()->GetUsername().c_str(), GetInventory().GetEquippedItem().m_ItemID);
|
LOG("Player %s Broke ID: %i", GetClientHandle()->GetUsername().c_str(), GetInventory().GetEquippedItem().m_ItemID);
|
||||||
GetInventory().RemoveItem( GetInventory().GetEquippedItem());
|
GetInventory().RemoveItem( GetInventory().GetEquippedItem());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -295,17 +295,23 @@ void cServer::StartListenClient()
|
|||||||
{
|
{
|
||||||
cSocket SClient = m_pState->SListenClient.Accept();
|
cSocket SClient = m_pState->SListenClient.Accept();
|
||||||
|
|
||||||
if( SClient.IsValid() )
|
if (!SClient.IsValid())
|
||||||
{
|
{
|
||||||
char * ClientIP = SClient.GetIPString();
|
|
||||||
if( ClientIP == 0 )
|
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
LOG("%s connected!", ClientIP);
|
const AString & ClientIP = SClient.GetIPString();
|
||||||
|
if (ClientIP.empty())
|
||||||
|
{
|
||||||
|
LOGWARN("cServer: A client connected, but didn't present its IP, disconnecting.");
|
||||||
|
SClient.CloseSocket();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG("%s connected!", ClientIP.c_str());
|
||||||
|
|
||||||
cClientHandle *NewHandle = new cClientHandle( SClient );
|
cClientHandle *NewHandle = new cClientHandle( SClient );
|
||||||
m_pState->Clients.push_back( NewHandle ); // TODO - lock list
|
m_pState->Clients.push_back( NewHandle ); // TODO - lock list
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
cSocket::cSocket( xSocket a_Socket )
|
cSocket::cSocket( xSocket a_Socket )
|
||||||
: m_Socket( a_Socket )
|
: m_Socket( a_Socket )
|
||||||
, m_IPString( 0 )
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,9 +58,9 @@ void cSocket::CloseSocket()
|
|||||||
closesocket(m_Socket);
|
closesocket(m_Socket);
|
||||||
#else
|
#else
|
||||||
if( shutdown(m_Socket, SHUT_RDWR) != 0 )//SD_BOTH);
|
if( shutdown(m_Socket, SHUT_RDWR) != 0 )//SD_BOTH);
|
||||||
LOGWARN("Error on shutting down socket (%s)", m_IPString );
|
LOGWARN("Error on shutting down socket (%s)", m_IPString.c_str() );
|
||||||
if( close(m_Socket) != 0 )
|
if( close(m_Socket) != 0 )
|
||||||
LOGWARN("Error closing socket (%s)", m_IPString );
|
LOGWARN("Error closing socket (%s)", m_IPString.c_str() );
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,9 +55,9 @@ public:
|
|||||||
cSocket Accept();
|
cSocket Accept();
|
||||||
int Receive( char* a_Buffer, unsigned int a_Length, unsigned int a_Flags );
|
int Receive( char* a_Buffer, unsigned int a_Length, unsigned int a_Flags );
|
||||||
|
|
||||||
char* GetIPString() { return m_IPString; }
|
const AString & GetIPString(void) const { return m_IPString; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
xSocket m_Socket;
|
xSocket m_Socket;
|
||||||
char* m_IPString;
|
AString m_IPString;
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user