/*
 * 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_sockbase_hh
#define _uds_sockbase_hh


class sockaddr_un;
class sockaddr_in;

namespace uds
{

/** base class for socket streams
	\author Julian v. Bock */
class Socket_base
{
public:
	/** default constructor */
	Socket_base();
	
	/** \param fd socket file descriptor */
	explicit
	Socket_base( int fd );
	
	/** destructor */
	virtual
	~Socket_base() = 0;
	
	/** enables the socket to accept connections
		\param n queue size for pending connection requests */
	virtual void
	Listen( unsigned n = 1 );
	
	/** accepts a conection to a server socket
		\return new socket (file) descriptor */
	virtual int
	Accept();
	
	/** closes a connection */
	virtual void
	Close();
	
	/** sets a new socket
		\param s new socket */
	virtual void
	Attach( int s );
	
	/** checks whether the socket is valid
		\return true if the socket is valid */
	bool
	Valid() const { return sock >= 0; }

	/** sets the socket to blocking / nonblocking mode
		\param b 'block' flag */
	void
	SetBlock( bool b );

	/** \return socket file descriptor */
	int
	Sock() const { return sock; }
	
	/** \return socket file descriptor */
	int
	operator () () const { return sock; }
	
private:
	/// socket file descriptor
	int	sock;
};


// utility functions that initialize network address structures

void
init_local_addr( sockaddr_un& name, const char* filename );

void
init_inet_addr( sockaddr_in& name, unsigned short p );

void
init_inet_addr( sockaddr_in& name, const char* n, unsigned short p );

}

#endif // _uds_sockbase_hh
