/*
 *
 *
 *    udpsocket.h
 *
 *    C++ header file for class 'udpsocket.'
 *
 *    =========================================================================
 *
 *    Copyright (C) 2001-2003 by Matt Roberts, KK5JY.
 *
 *    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
 *
 *    =========================================================================
 *
 *
 */


#ifndef __UDPSOCKET_H
#define __UDPSOCKET_H

	#include <my++/exception.h>
	#include <my++/inaddr.h>
	#include <my++/l4address.h>
	#include <my++/socket.h>
	#include <netinet/in.h>
	#include <sys/socket.h>
	#include <sys/types.h>
	#include <unistd.h>
	#include <stdio.h>
	#include <errno.h>


	namespace my {
		class udpsocket : public datagram_socket {
			public:

				/*
				 *
				 *    exception
				 *
				 */

				class exception : public exception_template<udpsocket> {
					public:
						exception(udpsocket *sp, int en) : exception_template<udpsocket>(sp, en) { /* noop */ };
				};

				/*
				 *
				 *    address
				 *
				 */

				typedef my::l4address address;


			private:  // our datum
				mutable int sd;

			private:  // internal utility classes
				void check_sd (void);
				//void assign (int sd2);

			public: // these functions implement the interfaces inherited from stream_socket
				int getfd (void) throw() { return sd; };

				void bind (const socket::address &a);
				void bind (const std::string &a);
				void ishutdown (void);
				void oshutdown (void);
				void ioshutdown (void);

				size_t get_from (socket::address &a, unsigned char *b, size_t len);
				size_t get_from (socket::address *a, unsigned char *b, size_t len);
				size_t get_from (socket::address &a, char *b, size_t len);
				size_t get_from (socket::address *a, char *b, size_t len);
				size_t get_from (socket::address &a, void *b, size_t len);
				size_t get_from (socket::address *a, void *b, size_t len);

				size_t put_to (const socket::address &a, const unsigned char *b, size_t len);
				size_t put_to (const socket::address *a, const unsigned char *b, size_t len);
				size_t put_to (const socket::address &a, const char *b, size_t len);
				size_t put_to (const socket::address *a, const char *b, size_t len);
				size_t put_to (const socket::address &a, const void *b, size_t len);
				size_t put_to (const socket::address *a, const void *b, size_t len);

				void name (socket::address &a);

				// operator = used in copy constructor
				//udpsocket &operator = (const int new_sd);

				// ctors/dtors
				udpsocket (void);
				udpsocket (const address &a);
				udpsocket (const std::string &a, int p);
				udpsocket (const inet_address &a, int p);
				~udpsocket (void) throw () { if (sd >= 0) { ::close(sd); sd = (-1); } };

			private:
				udpsocket (const udpsocket &s);
		};
	}


#endif /* __UDPSOCKET_H */
