📄 browse_server_generic.cpp
字号:
/* $Id: browse_server_generic.cpp,v 1.9 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 "browse_server_generic.h"
#include "API/Core/IOData/outputsource_memory.h"
#include "API/Core/System/clanstring.h"
#include "API/Core/System/error.h"
/////////////////////////////////////////////////////////////////////////////
// CL_BrowseServer_Generic construction:
CL_BrowseServer_Generic::CL_BrowseServer_Generic(
const std::string &app_id,
const CL_IPAddress &server_address)
: app_id(app_id), server_address(server_address), sock(CL_Socket::tcp), sock_udp(CL_Socket::udp), connected(false)
{
}
CL_BrowseServer_Generic::~CL_BrowseServer_Generic()
{
}
/////////////////////////////////////////////////////////////////////////////
// CL_BrowseServer_Generic attributes:
/////////////////////////////////////////////////////////////////////////////
// CL_BrowseServer_Generic operations:
void CL_BrowseServer_Generic::set_description(const CL_NetPacket &new_description)
{
description = new_description;
if (connected) sock.output.write_string(
std::string((char *) description.get_data(), description.get_size()));
}
void CL_BrowseServer_Generic::connect(const CL_IPAddress &browse_master)
{
sock.connect(browse_master);
sock.output.write_string(app_id);
sock.output.write_int32(0); // we are a server
sock.output.write_string(server_address.get_port());
sock.output.write_string(std::string((char *) description.get_data(), description.get_size()));
connected = true;
}
void CL_BrowseServer_Generic::listen(const std::string &port)
{
// NOTE: This is IPV4 compatible only. For IPV6, multicasting should be used
// bind to the given port
sock_udp.bind(CL_IPAddress(port));
// connect recive signal to slot
slot_recv = sock_udp.sig_read_triggered().connect(this, &CL_BrowseServer_Generic::on_udp_recv);
}
/////////////////////////////////////////////////////////////////////////////
// CL_BrowseServer_Generic implementation:
void CL_BrowseServer_Generic::on_udp_recv()
{
// NetPacket for receiving data packets
char buffer[16*1024];
CL_IPAddress ip_add;
// 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 (1 == recv_packet.input.read_int32()) // It's a client!
{
CL_NetPacket packet;
packet.output.write_string(app_id);
packet.output.write_int32(0); // we are a server
packet.output.write_string(server_address.get_port());
packet.output.write_string(std::string((char *) description.get_data(), description.get_size()));
sock_udp.send(packet.get_data(), packet.get_size(), ip_add);
}
}
}
catch(CL_Error err)
{
// DO NOTHING
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -