/*  $Id: netpacket.cpp,v 1.9 2003/08/13 15:33:42 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 "API/Network/NetSession/netpacket.h"
#include "API/Network/NetSession/netcomputer.h"
#include "netpacket_generic.h"
#include "inputsource_netpacket_generic.h"
#include "outputsource_netpacket_generic.h"
#include "netcomputer_generic.h"

/////////////////////////////////////////////////////////////////////////////
// CL_NetPacket construction:

CL_NetPacket::CL_NetPacket()
: impl(new CL_NetPacket_Generic)
{
	input.impl->netpacket = impl;
	output.impl->netpacket = impl;
	impl->add_ref();
}

CL_NetPacket::CL_NetPacket(const void *data, int size)
: impl(new CL_NetPacket_Generic)
{
	input.impl->netpacket = impl;
	output.impl->netpacket = impl;
	impl->add_ref();

	output.write(data, size);
}

CL_NetPacket::CL_NetPacket(const std::string &data)
: impl(new CL_NetPacket_Generic)
{
	input.impl->netpacket = impl;
	output.impl->netpacket = impl;
	impl->add_ref();

	output.write(data.data(), data.length());
}

CL_NetPacket::CL_NetPacket(const CL_NetPacket &copy)
: impl(copy.impl)
{
	input.impl->netpacket = impl;
	output.impl->netpacket = impl;
	input.impl->position = copy.input.impl->position;
	output.impl->position = copy.output.impl->position;

	if (impl) impl->add_ref();
}

CL_NetPacket::~CL_NetPacket()
{
	if (impl) impl->release_ref();
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetPacket attributes:

unsigned char *CL_NetPacket::get_data()
{
	return impl->data;
}

const unsigned char *CL_NetPacket::get_data() const
{
	return impl->data;
}

int CL_NetPacket::get_size() const
{
	return impl->size;
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetPacket operations:

void CL_NetPacket::resize(int new_size)
{
	impl->resize(new_size);
}

CL_NetPacket &CL_NetPacket::operator =(const CL_NetPacket &copy)
{
	if (impl) impl->release_ref();
	impl = copy.impl;
	if (impl) impl->add_ref();

	input.impl->netpacket = impl;
	output.impl->netpacket = impl;
	input.impl->position = copy.input.impl->position;
	output.impl->position = copy.output.impl->position;

	return *this;
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetPacket implementation:
