1
0

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:
madmaxoft@gmail.com 2012-02-02 07:47:19 +00:00
parent 48d30d6ab4
commit 865216b15a
6 changed files with 104 additions and 46 deletions

View File

@ -22,7 +22,6 @@ cMCLogger* cMCLogger::GetInstance()
cMCLogger::cMCLogger()
{
m_CriticalSection = new cCriticalSection();
AString FileName;
Printf(FileName, "LOG_%d.txt", (int)time(0) );
m_Log = new cLog(FileName);
@ -37,52 +36,66 @@ cMCLogger::cMCLogger()
cMCLogger::cMCLogger( char* a_File )
{
m_CriticalSection = new cCriticalSection();
m_Log = new cLog( a_File );
}
cMCLogger::~cMCLogger()
{
m_Log->Log("--- Stopped Log ---");
delete m_Log;
delete m_CriticalSection;
if( this == s_MCLogger )
s_MCLogger = 0;
if (this == s_MCLogger)
s_MCLogger = NULL;
}
void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ )
{
switch( a_LogType )
{
case 0:
Log(a_Text, 0);
break;
case 1:
Info(a_Text, 0);
break;
case 2:
Warn(a_Text, 0);
break;
case 3:
Error(a_Text, 0);
break;
default:
Log(a_Text, 0);
break;
case 0:
Log(a_Text, 0);
break;
case 1:
Info(a_Text, 0);
break;
case 2:
Warn(a_Text, 0);
break;
case 3:
Error(a_Text, 0);
break;
default:
Log(a_Text, 0);
break;
}
}
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
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)
{
m_CriticalSection->Lock();
cCSLock Lock(m_CriticalSection);
// for( int i = 0; i < 16; i++)
// {
// 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
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)
{
m_CriticalSection->Lock();
cCSLock Lock(m_CriticalSection);
SetColor( 0xc ); // 0xc is red
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)
{
m_CriticalSection->Lock();
cCSLock Lock(m_CriticalSection);
SetColor( 0xc0 ); // 0xc0 is red bg and black text
m_Log->Log( a_Format, a_ArgList );
m_CriticalSection->Unlock();
SetColor(0x07); // revert color back
}
void cMCLogger::SetColor( unsigned char a_Color )
{
#ifdef _WIN32
@ -125,6 +150,9 @@ void cMCLogger::SetColor( unsigned char a_Color )
}
//////////////////////////////////////////////////////////////////////////
// Global functions
void LOG(const char* a_Format, ...)

View File

@ -1,7 +1,8 @@
#pragma once
#ifndef _WIN32
#include <stdarg.h>
#include <stdarg.h>
#endif
class cLog;
@ -27,7 +28,7 @@ public: //tolua_export
private:
void SetColor( unsigned char a_Color );
cCriticalSection* m_CriticalSection;
cCriticalSection m_CriticalSection;
cLog* m_Log;
static cMCLogger* s_MCLogger;
}; //tolua_export
@ -35,4 +36,10 @@ private:
extern void LOG(const char* a_Format, ...);
extern void LOGINFO(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

View File

@ -920,6 +920,10 @@ const cPlayer::GroupList & cPlayer::GetGroups()
return m_pState->Groups;
}
cPlayer::StringList cPlayer::GetResolvedPermissions()
{
StringList Permissions;
@ -933,17 +937,31 @@ cPlayer::StringList cPlayer::GetResolvedPermissions()
return Permissions;
}
const char* cPlayer::GetLoadedWorldName()
{
return m_pState->LoadedWorldName.c_str();
}
void cPlayer::UseEquippedItem()
{
if(GetGameMode() != 1) //No damage in creative
{
if (GetInventory().GetEquippedItem().DamageItem())
{
LOG("Player %s Broke ID: %i", GetClientHandle()->GetUsername().c_str(), GetInventory().GetEquippedItem().m_ItemID);
GetInventory().RemoveItem( GetInventory().GetEquippedItem());
}
}
}
}

View File

@ -295,17 +295,23 @@ void cServer::StartListenClient()
{
cSocket SClient = m_pState->SListenClient.Accept();
if( SClient.IsValid() )
if (!SClient.IsValid())
{
char * ClientIP = SClient.GetIPString();
if( ClientIP == 0 )
return;
LOG("%s connected!", ClientIP);
cClientHandle *NewHandle = new cClientHandle( SClient );
m_pState->Clients.push_back( NewHandle ); // TODO - lock list
return;
}
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 );
m_pState->Clients.push_back( NewHandle ); // TODO - lock list
}

View File

@ -19,7 +19,6 @@
cSocket::cSocket( xSocket a_Socket )
: m_Socket( a_Socket )
, m_IPString( 0 )
{
}
@ -59,9 +58,9 @@ void cSocket::CloseSocket()
closesocket(m_Socket);
#else
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 )
LOGWARN("Error closing socket (%s)", m_IPString );
LOGWARN("Error closing socket (%s)", m_IPString.c_str() );
#endif
}

View File

@ -55,9 +55,9 @@ public:
cSocket Accept();
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:
xSocket m_Socket;
char* m_IPString;
AString m_IPString;
};