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

📄 browse_client_generic.cpp

📁 这是一款2d游戏引擎
💻 CPP
字号:
/*  $Id: browse_client_generic.cpp,v 1.11 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
**
*/

#include "browse_client_generic.h"
#include "API/Core/System/error.h"
#include "API/Core/System/event_trigger.h"
#include "API/Core/System/log.h"
#include "API/Core/IOData/outputsource_memory.h"
#include "API/Core/System/clanstring.h"

#ifdef WIN32
#  ifdef _MSC_VER
#    pragma warning (disable:4786)
#    pragma warning (disable:4355)
#  endif
#  include <winsock2.h> // needed for ntohl and ntohs
#else
#  include <netinet/in.h>
#endif


/////////////////////////////////////////////////////////////////////////////
// CL_BrowseClient_Generic construction:

CL_BrowseClient_Generic::CL_BrowseClient_Generic(const std::string &app_id)
: app_id(app_id), sock(CL_Socket::tcp), sock_udp(CL_Socket::udp), thread(this), stop_thread(false)
{
	// connect recive signal to slot
	slot_recv = sock_udp.sig_read_triggered().connect(this, &CL_BrowseClient_Generic::on_udp_recv);
}

CL_BrowseClient_Generic::~CL_BrowseClient_Generic()
{
	// shut down worker thread.
	stop_thread = true;
	thread.wait();
}

/////////////////////////////////////////////////////////////////////////////
// CL_BrowseClient_Generic attributes:

/////////////////////////////////////////////////////////////////////////////
// CL_BrowseClient_Generic operations:

void CL_BrowseClient_Generic::connect(const CL_IPAddress &new_browse_master)
{
	browse_master = new_browse_master;
	thread.start();
}

void CL_BrowseClient_Generic::broadcast(const std::string &port)
{
	// get broadcast addresses
	std::list<CL_IPAddress> broadcast_list;
	broadcast_list = sock_udp.get_broadcast_addresses(port);

	// set the data to send
	CL_NetPacket packet;

	packet.output.write_string(app_id.c_str());
	packet.output.write_int32(1); // we are a client

	// bind to the port to listen
	sock_udp.bind(CL_IPAddress(port));

	std::list<CL_IPAddress>::iterator it;

	sock_udp.set_broadcasting(true);

	for(it = broadcast_list.begin(); it != broadcast_list.end(); it++)
	{
		CL_Log::log("debug", "Broadcasting with address %1", it->get_address());
		sock_udp.send(packet.get_data(), packet.get_size(), *it);
	}

	sock_udp.set_broadcasting(false);
}

/////////////////////////////////////////////////////////////////////////////
// CL_BrowseClient_Generic implementation:

void CL_BrowseClient_Generic::keep_alive()
{
	CL_MutexSection mutex_lock(&mutex);

	std::list< std::pair<CL_IPAddress, CL_NetPacket> >::iterator it;
	std::list<CL_IPAddress>::iterator it2;
	
	for (it = added_list.begin(); it != added_list.end(); it++)
	{
		sig_server_added(it->first, it->second);
	}

	for (it = updated_list.begin(); it != updated_list.end(); it++)
	{
		sig_server_updated(it->first, it->second);
	}

	for (it2 = removed_list.begin(); it2 != removed_list.end(); it2++)
	{
		sig_server_removed(*it2);
	}

	added_list.clear();
	updated_list.clear();
	removed_list.clear();

	// Pass on exception in worker thread to user thread.
	if (exception_thrown) throw CL_Error(exception);
}

void CL_BrowseClient_Generic::run()
{
	try
	{
		sock.connect(browse_master);

		sock.output.write_string(app_id.c_str());
		sock.output.write_int32(1);      // we are a client

		while (true)
		{
			if (sock.get_read_trigger()->wait(100) == false)
			{
				if (stop_thread) break;
				continue;
			}

			int type = sock.input.read_int32();
			if (type == 0) // added or modified
			{
				std::string address = sock.input.read_string();
				std::string port = sock.input.read_string();
				std::string desc = sock.input.read_string();
				
				CL_IPAddress addr(address, port);
				std::pair<CL_IPAddress, CL_NetPacket> params(addr, desc);

				CL_MutexSection mutex_lock(&mutex);
				// added_list.push_back(params);
				updated_list.push_back(params);
			}
			else // removed
			{
				std::string address = sock.input.read_string();
				std::string port = sock.input.read_string();

				CL_IPAddress addr(address, port);

				CL_MutexSection mutex_lock(&mutex);
				removed_list.push_back(addr);
			}
		}
	}
	catch (CL_Error err)
	{
		CL_MutexSection mutex_lock(&mutex);
		exception = err.message;
		exception_thrown = true;
	}
}

void CL_BrowseClient_Generic::on_udp_recv()
{
	// NetPacket for receiving data packets
	char buffer[16*1024];
	CL_IPAddress ip_add;
	CL_IPAddress server_address;

	// only recieving data less than or equal to size of 16*1024
	int size = sock_udp.recv(buffer, sizeof(buffer), ip_add);

	CL_NetPacket recv_packet(buffer, size);

	try
	{
		if (app_id == recv_packet.input.read_string()) // match!!!
		{
			if (0 == recv_packet.input.read_int32()) // It's a server!
			{
				// get the server address and port number
				server_address.set_address(ip_add.get_address(), recv_packet.input.read_string());

				CL_NetPacket data;
				data.output.write(recv_packet.get_data(), recv_packet.get_size());

				// send the data out
				sig_server_added(server_address, data);
			}
		}
	}
	catch(CL_Error err)
	{
		// DO NOTHING
	}
}

⌨️ 快捷键说明

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