/*  $Id: outputsource_socket.cpp,v 1.7 2004/04/24 20:50:50 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/Socket/outputsource_socket.h"
#include "API/Network/Socket/socket.h"
#include "socket_generic.h"

/////////////////////////////////////////////////////////////////////////////
// CL_OutputSource_Socket Construction:

CL_OutputSource_Socket::CL_OutputSource_Socket(const CL_Socket &sock)
: socket(sock.impl), ref_count_socket(true)
{
	socket->add_ref();
}

CL_OutputSource_Socket::~CL_OutputSource_Socket()
{
	if (ref_count_socket) socket->release_ref();
}

/////////////////////////////////////////////////////////////////////////////
// CL_OutputSource_Socket Attributes:

int CL_OutputSource_Socket::tell() const
{
	return 0;
}

int CL_OutputSource_Socket::size() const
{
	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// CL_OutputSource_Socket Operations:

int CL_OutputSource_Socket::write(const void *data, int size)
{
	CL_Socket s(socket);
	char *d = (char*) data;
	int total_sent = 0;
	while (size-total_sent > 0)
	{
		int sent = s.send(d+total_sent, size-total_sent);
		if (sent <= 0)
			break;

		total_sent += sent;
	}
	return total_sent;
}
	
void CL_OutputSource_Socket::open()
{
}

void CL_OutputSource_Socket::close()
{
}

CL_OutputSource *CL_OutputSource_Socket::clone()
{
	return new CL_OutputSource_Socket(CL_Socket(socket));
}
