⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server_database.cpp

📁 这是一款2d游戏引擎
💻 CPP
字号:
/*  $Id: server_database.cpp,v 1.3 2003/11/11 10:13:25 grumbel Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library 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
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*/

#ifdef _MSC_VER
#pragma warning(disable : 4786)
#endif

#include "server_database.h"
#include "API/Core/System/system.h"

/////////////////////////////////////////////////////////////////////////////
// CL_ServerDatabase construction:

CL_ServerDatabase::CL_ServerDatabase() : ref_count(0)
{
}

CL_ServerDatabase::~CL_ServerDatabase()
{
}

/////////////////////////////////////////////////////////////////////////////
// CL_ServerDatabase attributes:


/////////////////////////////////////////////////////////////////////////////
// CL_ServerDatabase operations:

int CL_ServerDatabase::add_ref()
{
	return ++ref_count;
}

int CL_ServerDatabase::release_ref()
{
	--ref_count;
	if (ref_count == 0)
	{
		delete this;
		return 0;
	}
	return ref_count;
}

void CL_ServerDatabase::update_server(const CL_IPAddress &address, const std::string &desc)
{
	CL_MutexSection mutex_lock(&mutex);

	CL_ServerUpdate update;
	update.timestamp = CL_System::get_time();
	update.server = address;
	updates.push_front(update);

	std::map<CL_IPAddress, CL_ServerDescription *>::iterator it;

	it = servers.find(address);
	if (it != servers.end())
	{
		it->second->ref_count--;
		if (it->second->ref_count == 0) delete it->second;
	}

	CL_ServerDescription *server_description = new CL_ServerDescription;
	server_description->ref_count = 1;
	server_description->desc = desc;

	servers[address] = server_description;
}

void CL_ServerDatabase::remove_server(const CL_IPAddress &address)
{
	CL_MutexSection mutex_lock(&mutex);

	CL_ServerUpdate update;
	update.timestamp = CL_System::get_time();
	update.server = address;
	updates.push_front(update);

	std::map<CL_IPAddress, CL_ServerDescription *>::iterator it;

	it = servers.find(address);
	if (it != servers.end())
	{
		it->second->ref_count--;
		if (it->second->ref_count == 0) delete it->second;
		servers.erase(it);
	}
}

CL_ServerChanges CL_ServerDatabase::get_changes(unsigned int timestamp)
{
	CL_MutexSection mutex_lock(&mutex);

	CL_ServerChanges changes;
	changes.timestamp = CL_System::get_time();

	if (timestamp == 0) changes.servers = servers;
	else
	{
		std::deque<CL_ServerUpdate>::iterator it;

		for (it = updates.begin(); it != updates.end(); ++it)
		{
			if (it->timestamp < timestamp) break;

			std::map<CL_IPAddress, CL_ServerDescription *>::iterator server_it;

			server_it = servers.find(it->server);
			if (server_it != servers.end())
			{
				changes.servers[it->server] = server_it->second;
			}
			else
			{
				changes.removed_servers.push_back(it->server);
			}
		}
	}

	std::map<CL_IPAddress, CL_ServerDescription *>::iterator it;
	for (it = changes.servers.begin(); it != changes.servers.end(); ++it)
	{
		it->second->ref_count++;
	}

	return changes;
}

void CL_ServerDatabase::release_changes(CL_ServerChanges &changes)
{
	CL_MutexSection mutex_lock(&mutex);

	std::map<CL_IPAddress, CL_ServerDescription *>::iterator it;
	for (it = changes.servers.begin(); it != changes.servers.end(); ++it)
	{
		it->second->ref_count--;
		if (it->second->ref_count == 0) delete it->second;
	}

	changes.removed_servers.clear();
	changes.servers.clear();
}

/////////////////////////////////////////////////////////////////////////////
// CL_ServerDatabase implementation:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -