Exemple #1
0
void GameSocket::OnRawData( const char *pData,size_t len,struct sockaddr *sa_from,socklen_t sa_len )
{
	struct sockaddr_in inc_addr;
	memcpy(&inc_addr,sa_from,sa_len);
	Ipv4Address theAddr(inc_addr);

	if (theAddr.IsValid() == false)
		return;

	string IPStr = theAddr.Convert(true);
	GClientList::iterator it = m_clients.find(IPStr);
	if (it != m_clients.end())
	{
		GameClient *Client = it->second;
		if (Client->IsValid() == false)
		{
			DEBUG_LOG( format("Removing dead client [%1%]") % IPStr );
			m_clients.erase(it);
			delete Client;
		}
		else
		{
			Client->HandlePacket(pData, len);
		}
	}
	else
	{
		m_clients[IPStr] = new GameClient(inc_addr, this);
		DEBUG_LOG(format ("Client connected [%1%], now have [%2%] clients")
			% IPStr % Clients_Connected());

		m_clients[IPStr]->HandlePacket(pData, len);
		m_clients[IPStr]->InitializeWorld () ;
	}
}
Exemple #2
0
void GameSocket::PruneDeadClients()
{
	m_currTime = getTime();

	// Do client cleanup
	for (GClientList::iterator it=m_clients.begin();it!=m_clients.end();)
	{
		GameClient *Client = it->second;
		if (!Client->IsValid() || (m_currTime - Client->LastActive()) >= 20)
		{
			if (!Client->IsValid())
				DEBUG_LOG( format("Removing invalidated client [%1%]") % Client->Address() );
			else
				DEBUG_LOG( format("Removing client due to time-out [%1%]") % Client->Address() );

			m_clients.erase(it++);
			delete Client;
		}
		else
		{
			++it;
		}
	}

	if ((m_currTime - m_lastCleanupTime) >= 5)
	{
		// Update player count
		if (m_lastPlayerCount != this->Clients_Connected())
		{
			sDatabase.Execute(format("UPDATE `worlds` SET `numPlayers`='%1%' WHERE `name`='%2%' LIMIT 1")
				% this->Clients_Connected()
				% sGame.GetName() );

			m_lastPlayerCount = this->Clients_Connected();
		}

		m_lastCleanupTime = m_currTime;
	}
}