/*
 * 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_packetsocket_hh
#define _uds_packetsocket_hh


// includes

#include <uds/sockbase.hh>
#include <string>


namespace uds
{

/** base class for sockets with packet oriented protocols
	\author Julian v. Bock */
class PacketSocket : public Socket_base
{
public:
	/** default constructor */
	PacketSocket();
	
	/** \param fd socket file descriptor */
	explicit
	PacketSocket( int fd );
	
	/** destructor */
	virtual
	~PacketSocket() = 0;
	
	/** closes a connection */
	virtual void
	Close();

private:
	/** Close() helper function */
	void
	priv_close();
};


/** UDP socket class. Flags for the Send() and Recv() methods are directly passed
	to sendto(), secvfrom(), etc.
	\author Julian v. Bock */
class UDPSocket : public PacketSocket
{
public:
	/** \param c 'create socket' flag */
	explicit
	UDPSocket( bool c = true );

	/** \param fd socket file descriptor */
	explicit
	UDPSocket( int fd );

	/** destructor */
	virtual
	~UDPSocket();

	/** creates a new UDP socket */
	virtual void
	Create();

	/** binds a name to a UDP socket. The socket is created if necessary.
		\param p port */
	virtual void
	Bind( unsigned short p );

	/** sets the default target. This is like calling connect() on a UDP
		socket
		\param h host
		\param p port */
	virtual void
	SetDTarget( const char* h, unsigned short p );

	/** unsets the default target */
	virtual void
	UnsetDTarget();

	/** writes data to the socket. The message is sent to the default target
		that has been set using SetDTarget. 
		\param d data
		\param f flags */
	virtual void
	Send( const string& d, int f = 0 );

	/** writes data to the socket
		\param d data
		\param h destinaton host
		\param p port
		\param f flags */
	virtual void
	Send( const string& d, const char* h, unsigned short p, int f = 0 );

	/** receives data from the socket
		\param d string to be filled with packet data
		\param f flags */
	virtual void
	Recv( string& d, int f = 0 );

	/** receives data from the socket
		\param f flags 
		\return packet data */
	virtual string
	Recv( int f = 0 ) { string ret; Recv( ret, f ); return ret; }

	/** receives data from the socket. The source address is also retrieved.
		\param d string to be filled with packet data
		\param h source host
		\param f flags */
	virtual void
	Recv( string& d, string& h, int f = 0 );
};

/** creates a UDP socket. The socket is not bound to an address */
int
CrUDPSocket();

}

#endif // _uds_packetsocket_hh
