/*  $Id: netstream_generic.cpp,v 1.7 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 "netstream_generic.h"
#include "netsession_generic.h"
#include "netcomputer_generic.h"
#include "API/Network/NetSession/netcomputer.h"

/////////////////////////////////////////////////////////////////////////////
// CL_NetStream_Generic construction:

CL_NetStream_Generic::CL_NetStream_Generic(
	const std::string &netstream,
	CL_NetComputer &dest,
	CL_NetSession_Generic *session)
: session(session), computer(dest.impl), channel_id(0), closed(false), ref_count(0)
{
	CL_MutexSection mutex_section(&computer->mutex);
	computer->add_ref();

	channel_id = computer->send_stream_connect(netstream, this);
}

CL_NetStream_Generic::CL_NetStream_Generic(
	int channel_id,
	CL_NetComputer_Generic *dest,
	CL_NetSession_Generic *session)
: session(session), computer(dest), channel_id(channel_id), closed(false), ref_count(0)
{
	CL_MutexSection mutex_section(&computer->mutex);
	computer->add_ref();
}

CL_NetStream_Generic::~CL_NetStream_Generic()
{
	if (!closed) computer->send_stream_close(channel_id);

	CL_MutexSection mutex_section(&computer->mutex);
	computer->release_ref();
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetStream_Generic attributes:

/////////////////////////////////////////////////////////////////////////////
// CL_NetStream_Generic operations:

void CL_NetStream_Generic::add_ref()
{
	CL_MutexSection mutex_section(&mutex);
	ref_count++;
}

int CL_NetStream_Generic::release_ref()
{
	CL_MutexSection mutex_section(&mutex);
	ref_count--;
	if (ref_count == 0)
	{
		mutex_section.leave();
		delete this;
		return 0;
	}
	return ref_count;
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetStream_Generic implementation:
