/*  $Id: netobject_server_generic.cpp,v 1.6 2003/08/14 23:01:33 mbn 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 "netobject_server_generic.h"
#include "netobject_controller_generic.h"
#include "API/Network/NetSession/netpacket.h"
#include "API/Network/NetSession/netgroup.h"

/////////////////////////////////////////////////////////////////////////////
// CL_NetObject_Server_Generic construction:

CL_NetObject_Server_Generic::CL_NetObject_Server_Generic(
	CL_NetObject_Controller_Generic *controller)
: obj_id(controller->obj_id_counter++), controller(controller), ref_count(0)
{
	controller->server_objects[obj_id] = this;
}

CL_NetObject_Server_Generic::~CL_NetObject_Server_Generic()
{
	controller->server_objects.erase(controller->server_objects.find(obj_id));
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetObject_Server_Generic attributes:


/////////////////////////////////////////////////////////////////////////////
// CL_NetObject_Server_Generic operations:

void CL_NetObject_Server_Generic::receive(int msg_type, CL_NetComputer &from, CL_NetPacket &packet)
{
	std::map< int, CL_Signal_v2<CL_NetComputer &, CL_NetPacket &> >::iterator it;
	it = sig_received_message.find(msg_type);
	if (it != sig_received_message.end())
	{
		it->second(from, packet);
	}
	else
	{
		// No message handler found.
		// Inform application somehow?
	}
}

void CL_NetObject_Server_Generic::send(CL_NetGroup &group, int msg_type, const CL_NetPacket &message)
{
	CL_NetPacket netpacket;
	netpacket.output.write_bool8(false);
	netpacket.output.write_int32(obj_id);
	netpacket.output.write_int32(msg_type);
	netpacket.output.write(message.get_data(), message.get_size());
	group.send(controller->channel, netpacket);
}

void CL_NetObject_Server_Generic::send(CL_NetComputer &computer, int msg_type, const CL_NetPacket &message)
{
	CL_NetPacket netpacket;
	netpacket.output.write_bool8(false);
	netpacket.output.write_int32(obj_id);
	netpacket.output.write_int32(msg_type);
	netpacket.output.write(message.get_data(), message.get_size());
	computer.send(controller->channel, netpacket);
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetObject_Server_Generic implementation:
