<commit_before>/*
 * Ascent MMORPG Server
 * Copyright (C) 2005-2007 Ascent Team <http://www.ascentemu.com/>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

//
// WorldSession.cpp
//

#include "StdAfx.h"

#define WORLDSOCKET_TIMEOUT		 80
#define PLAYER_LOGOUT_DELAY (20*1000)		/* 20 seconds should be more than enough to gank ya. */

OpcodeHandler WorldPacketHandlers[NUM_MSG_TYPES];

WorldSession::WorldSession(uint32 id, string Name, WorldSocket *sock) : _player(0), _socket(sock), _accountId(id), _accountName(Name),
_logoutTime(0), permissions(NULL), permissioncount(0), _loggingOut(false), instanceId(0)
{
	memset(movement_packet, 0, sizeof(movement_packet));
	m_currMsTime = getMSTime();
	bDeleted = false;
	m_bIsWLevelSet = false;
	floodLines = 0;
	floodTime = UNIXTIME;
	_updatecount = 0;
	m_moveDelayTime=0;
	m_clientTimeDelay =0;
	m_loggingInPlayer=NULL;
	language=0;
	m_muted = 0;
	_side = -1;

	for(uint32 x=0;x<8;x++)
		sAccountData[x].data=NULL;	
}

WorldSession::~WorldSession()
{
	deleteMutex.Acquire();

	if(HasFlag(ACCOUNT_FLAG_XTEND_INFO))
		sWorld.RemoveExtendedSession(this);

	if(HasGMPermissions())
		sWorld.gmList.erase(this);

	if(_player)
	{
		sLog.outError("warning: logged out player in worldsession destructor");
		LogoutPlayer(true);
	}

	if(permissions)
		delete [] permissions;

	WorldPacket *packet;

	while((packet = _recvQueue.Pop()))
		delete packet;

	for(uint32 x=0;x<8;x++)
	{
		if(sAccountData[x].data)
			delete [] sAccountData[x].data;
	}

#ifndef CLUSTERING
	if(_socket)
		_socket->SetSession(0);
#endif

	if(m_loggingInPlayer)
		m_loggingInPlayer->SetSession(NULL);

	deleteMutex.Release();
}

int WorldSession::Update(uint32 InstanceID)
{
	m_currMsTime = getMSTime();

#ifndef CLUSTERING
	if(!((++_updatecount) % 2) && _socket)
		_socket->UpdateQueuedPackets();
#endif

	WorldPacket *packet;
	OpcodeHandler * Handler;

	if(InstanceID != instanceId)
	{
		// We're being updated by the wrong thread.
		// "Remove us!" - 2
		return 2;
	}

	// Socket disconnection.
	if(!_socket)
	{
		// Check if the player is in the process of being moved. We can't delete him
		// if we are.
		if(_player && _player->m_beingPushed)
		{
			// Abort..
			return 0;
		}

		if(!_logoutTime)
			_logoutTime = m_currMsTime + PLAYER_LOGOUT_DELAY;

/*
				if(_player && _player->DuelingWith)
					_player->EndDuel(DUEL_WINNER_RETREAT);
		
				bDeleted = true;
				LogoutPlayer(true);
				// 1 - Delete session completely.
				return 1;*/
		
	}

	while ((packet = _recvQueue.Pop()))
	{
		ASSERT(packet);

		if(packet->GetOpcode() >= NUM_MSG_TYPES)
			sLog.outError("[Session] Received out of range packet with opcode 0x%.4X", packet->GetOpcode());
		else
		{
			Handler = &WorldPacketHandlers[packet->GetOpcode()];
			if(Handler->status == STATUS_LOGGEDIN && !_player && Handler->handler != 0)
			{
				sLog.outError("[Session] Received unexpected/wrong state packet with opcode %s (0x%.4X)",
					LookupName(packet->GetOpcode(), g_worldOpcodeNames), packet->GetOpcode());
			}
			else
			{
				// Valid Packet :>
				if(Handler->handler == 0)
				{
					sLog.outError("[Session] Received unhandled packet with opcode %s (0x%.4X)",
						LookupName(packet->GetOpcode(), g_worldOpcodeNames), packet->GetOpcode());
				}
				else
				{
					(this->*Handler->handler)(*packet);
				}
			}
		}

		delete packet;

		if(InstanceID != instanceId)
		{
			// If we hit this -> means a packet has changed our map.
			return 2;
		}
	}

	if(InstanceID != instanceId)
	{
		// If we hit this -> means a packet has changed our map.
		return 2;
	}

	if( _logoutTime && (m_currMsTime >= _logoutTime) && instanceId == InstanceID)
	{
		// Check if the player is in the process of being moved. We can't delete him
		// if we are.
		if(_player && _player->m_beingPushed)
		{
			// Abort..
			return 0;
		}

		if( !_socket == NULL )
		{
			bDeleted = true;<commit_msg>* fixed the WorldSession::Update crash<commit_after>/*
 * Ascent MMORPG Server
 * Copyright (C) 2005-2007 Ascent Team <http://www.ascentemu.com/>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

//
// WorldSession.cpp
//

#include "StdAfx.h"

#define WORLDSOCKET_TIMEOUT		 80
#define PLAYER_LOGOUT_DELAY (20*1000)		/* 20 seconds should be more than enough to gank ya. */

OpcodeHandler WorldPacketHandlers[NUM_MSG_TYPES];

WorldSession::WorldSession(uint32 id, string Name, WorldSocket *sock) : _player(0), _socket(sock), _accountId(id), _accountName(Name),
_logoutTime(0), permissions(NULL), permissioncount(0), _loggingOut(false), instanceId(0)
{
	memset(movement_packet, 0, sizeof(movement_packet));
	m_currMsTime = getMSTime();
	bDeleted = false;
	m_bIsWLevelSet = false;
	floodLines = 0;
	floodTime = UNIXTIME;
	_updatecount = 0;
	m_moveDelayTime=0;
	m_clientTimeDelay =0;
	m_loggingInPlayer=NULL;
	language=0;
	m_muted = 0;
	_side = -1;

	for(uint32 x=0;x<8;x++)
		sAccountData[x].data=NULL;	
}

WorldSession::~WorldSession()
{
	deleteMutex.Acquire();

	if(HasFlag(ACCOUNT_FLAG_XTEND_INFO))
		sWorld.RemoveExtendedSession(this);

	if(HasGMPermissions())
		sWorld.gmList.erase(this);

	if(_player)
	{
		sLog.outError("warning: logged out player in worldsession destructor");
		LogoutPlayer(true);
	}

	if(permissions)
		delete [] permissions;

	WorldPacket *packet;

	while((packet = _recvQueue.Pop()))
		delete packet;

	for(uint32 x=0;x<8;x++)
	{
		if(sAccountData[x].data)
			delete [] sAccountData[x].data;
	}

#ifndef CLUSTERING
	if(_socket)
		_socket->SetSession(0);
#endif

	if(m_loggingInPlayer)
		m_loggingInPlayer->SetSession(NULL);

	deleteMutex.Release();
}

int WorldSession::Update(uint32 InstanceID)
{
	m_currMsTime = getMSTime();

#ifndef CLUSTERING
	if(!((++_updatecount) % 2) && _socket)
		_socket->UpdateQueuedPackets();
#endif

	WorldPacket *packet;
	OpcodeHandler * Handler;

	if(InstanceID != instanceId)
	{
		// We're being updated by the wrong thread.
		// "Remove us!" - 2
		return 2;
	}

	// Socket disconnection.
	if(!_socket)
	{
		// Check if the player is in the process of being moved. We can't delete him
		// if we are.
		if(_player && _player->m_beingPushed)
		{
			// Abort..
			return 0;
		}

		if(!_logoutTime)
			_logoutTime = m_currMsTime + PLAYER_LOGOUT_DELAY;

/*
				if(_player && _player->DuelingWith)
					_player->EndDuel(DUEL_WINNER_RETREAT);
		
				bDeleted = true;
				LogoutPlayer(true);
				// 1 - Delete session completely.
				return 1;*/
		
	}

	while ((packet = _recvQueue.Pop()))
	{
		ASSERT(packet);

		if(packet->GetOpcode() >= NUM_MSG_TYPES)
			sLog.outError("[Session] Received out of range packet with opcode 0x%.4X", packet->GetOpcode());
		else
		{
			Handler = &WorldPacketHandlers[packet->GetOpcode()];
			if(Handler->status == STATUS_LOGGEDIN && !_player && Handler->handler != 0)
			{
				sLog.outError("[Session] Received unexpected/wrong state packet with opcode %s (0x%.4X)",
					LookupName(packet->GetOpcode(), g_worldOpcodeNames), packet->GetOpcode());
			}
			else
			{
				// Valid Packet :>
				if(Handler->handler == 0)
				{
					sLog.outError("[Session] Received unhandled packet with opcode %s (0x%.4X)",
						LookupName(packet->GetOpcode(), g_worldOpcodeNames), packet->GetOpcode());
				}
				else
				{
					(this->*Handler->handler)(*packet);
				}
			}
		}

		delete packet;

		if(InstanceID != instanceId)
		{
			// If we hit this -> means a packet has changed our map.
			return 2;
		}

		if( bDeleted )
		{
			return 1;
		}
	}

	if(InstanceID != instanceId)
	{
		// If we hit this -> means a packet has changed our map.
		return 2;
	}

	if( _logoutTime && (m_currMsTime >= _logoutTime) && instanceId == InstanceID)
	{
		// Check if the player is in the process of being moved. We can't delete him
		// if we are.
		if(_player && _player->m_beingPushed)
		{
			// Abort..
			return 0;
		}

		if( _socket == NULL )
		{
			bDeleted = true;
{'fraction_non_alphanumeric': 0.1292003992458689, 'fraction_numerical': 0.013640900521237662, 'mean_word_length': 3.7264150943396226, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6406158', 'n_tokens_mistral': 3518, 'n_tokens_neox': 3142, 'n_words': 809}