/*  $Id: inputsource_netpacket.cpp,v 1.4 2003/07/20 22:34:40 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 "API/Core/System/cl_assert.h"
#include "API/Network/NetSession/inputsource_netpacket.h"
#include "API/Network/NetSession/netpacket.h"
#include "inputsource_netpacket_generic.h"
#include "netpacket_generic.h"

/////////////////////////////////////////////////////////////////////////////
// CL_InputSource_NetPacket construction:

CL_InputSource_NetPacket::CL_InputSource_NetPacket(const CL_NetPacket &netpacket)
: impl(new CL_InputSource_NetPacket_Generic(netpacket.impl, true))
{
}

CL_InputSource_NetPacket::~CL_InputSource_NetPacket()
{
	delete impl;
}

/////////////////////////////////////////////////////////////////////////////
// CL_InputSource_NetPacket attributes:

int CL_InputSource_NetPacket::tell() const
{
	return impl->position;
}

int CL_InputSource_NetPacket::size() const
{
	return impl->netpacket->size;
}

/////////////////////////////////////////////////////////////////////////////
// CL_InputSource_NetPacket operations:

#define cl_min(a, b) ((a < b) ? a : b)

int CL_InputSource_NetPacket::read(void *data, int size)
{
	int received = cl_min(size, impl->netpacket->size - impl->position);
	memcpy(data, impl->netpacket->data+impl->position, received);
	impl->position += received;
	return received;
}

void CL_InputSource_NetPacket::open()
{
}

void CL_InputSource_NetPacket::close()
{
}

CL_InputSource *CL_InputSource_NetPacket::clone() const
{
	return new CL_InputSource_NetPacket(*this);
}

void CL_InputSource_NetPacket::seek(int pos, SeekEnum seek_type)
{
	switch (seek_type)
	{
	case seek_set:
		impl->position = pos;
		break;

	case seek_cur:
		impl->position += pos;
		break;

	case seek_end:
		impl->position = size()+pos;
		break;
	}
}

void CL_InputSource_NetPacket::push_position()
{
	cl_assert(false); // not implemented.
}

void CL_InputSource_NetPacket::pop_position()
{
	cl_assert(false); // not implemented.
}

/////////////////////////////////////////////////////////////////////////////
// CL_InputSource_NetPacket implementation:

CL_InputSource_NetPacket::CL_InputSource_NetPacket()
: impl(new CL_InputSource_NetPacket_Generic(0, false))
{
}
