/*
 * The UDS Collection
 * Copyright (C) 2000, 2001  Julian v. Bock
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *	(See file COPYING for more information)
 */

#ifndef _uds_streamsocket_hh
#define _uds_streamsocket_hh


// includes

#include <sys/socket.h>
#include <uds/sockbase.hh>
#include <iostream>


namespace uds
{

/** socket stream buffer
	\authoe Julian v. Bock */
class sockbuf : public filebuf
{
public:
	/** default constructor */
	sockbuf();

	/** destructor */
	virtual
	~sockbuf();
	
	/** low-level read operation
		\param buf buffer
		\param sz size */
	virtual streamsize
	sys_read( char* buf, streamsize sz );

	/** low-level write operation
		\param buf buffer
		\param sz size */
	virtual streamsize
	sys_write( const char* buf, streamsize sz );

	/** sets socket operation flags
		\param f new flags
		\return old flags */
	void
	sflags( int f ) { sf = f; }

	/** \return socket operation flags */
	int
	sflags() const { return sf; }

private:
	/// socket operation flags
	int sf;
};


/** stream class for internet and unix domain sockets
	\author Julian v. Bock */
class StreamSocket : public Socket_base, public iostream
{
public:
	/** default constructor */
	StreamSocket();
	
	/** \param fd socket file descriptor */
	explicit
	StreamSocket( int fd );
	
	/** destructor */
	virtual
	~StreamSocket() = 0;
	
	/** closes a connection */
	virtual void
	Close();
	
	/** sets a new socket
		\param s new socket */
	virtual void
	Attach( int s );

	/** sets socket flags
		\param f new flags */
	void
	SFlags( int f ) { sb.sflags( f ); }

	/** \return socket flags */
	int
	SFlags() const { return sb.sflags(); }

private:
	/// stream buffer
	sockbuf sb;
	
	/** Close() helper function */
	void
	priv_close();
};

template < class Tp >
class sockmanip;

template < class Tp >
inline StreamSocket&
operator >> ( StreamSocket& i, const sockmanip< Tp >& m );

template < class Tp >
inline StreamSocket&
operator << ( StreamSocket& o, const sockmanip< Tp >& m );

template < class Tp >
class sockmanip
{
public:
    sockmanip( StreamSocket& ( * f )( StreamSocket&, Tp ), Tp a )
		: _f( f ), _a( a ) {}

    friend 
	StreamSocket& operator >> <> ( StreamSocket& i, const sockmanip< Tp >& m );
	
    friend
	StreamSocket& operator << <> ( StreamSocket& o, const sockmanip< Tp >& m );
	
private:
    StreamSocket& ( * _f )( StreamSocket&, Tp );
    Tp _a;
};

template < class Tp >
inline StreamSocket&
operator >> ( StreamSocket& i, const sockmanip< Tp >& m )
{
	m._f( i, m._a );
	return i;
}

template < class Tp >
inline StreamSocket&
operator << ( StreamSocket& o, const sockmanip< Tp >& m )
{
	m._f( o, m._a );
	return o;
}

StreamSocket&
sockflags_fn( StreamSocket&, int );

/** stream manipulator for StreamSockets. Changes the flags that are passed
	to send() and recv calls. */
inline sockmanip< int >
sockflags( int f )
{
	return sockmanip< int >( &sockflags_fn, f );
}


/** TCP stream class
	\author Julian v. Bock */
class TCPSocket : public StreamSocket
{
public:
	/** default constructor */
	TCPSocket();
	
	/** \param fd socket file descriptor */
	explicit
	TCPSocket( int fd );
	
	/** destructor */
	virtual
	~TCPSocket();
	
	/** creates a socket and enables it to accept connections
		\param p port
		\param n queue size for pending connection requests */
	virtual void
	Listen( unsigned short p, unsigned n = 1 );

	/** creates a new socket and connects to a [remote] tcp socket
		\param hn host name 
		\param p port */
	virtual void
	Connect( const char* hn, unsigned short p );
};

/** Creates a local TCP socket
	\param p port number
	\return socket file descriptor */
int
CrTCPSocket( unsigned short p );


/** Unix domain socket stream class
	\author Julian v. Bock */
class UnixSocket : public StreamSocket
{
public:
	/** default constructor */
	UnixSocket();
	
	/** \param fd socket file descriptor */
	explicit
	UnixSocket( int fd );
	
	/** destructor */
	virtual
	~UnixSocket();
	
	/** creates a socket and enables it to accept connections
		\param f file name
		\param n queue size for pending connection requests */
	virtual void
	Listen( const char* f, unsigned n = 1 );

	/** creates a socket and connects to a unix domain socket
		\param f file name */
	virtual void
	Connect( const char* f );
};

/** Creates a named unix domain socket.
	\param name socket name
	\return socket file descriptor */
int
CrUnixSocket( const char *name );

}

#endif // _uds_streamsocket_hh
