/*
 *
 *
 *    tcpsocket.h
 *
 *    C++ header file for class 'tcpsocket.'
 *
 *    =========================================================================
 *
 *    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 __TCPSOCKET_H
#define __TCPSOCKET_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>

	#define DEFAULT_BACKLOG (5)


	namespace my {
		class tcpsocket : public stream_socket {
			public:

				/*
				 *
				 *    exception
				 *
				 */

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

				/*
				 *
				 *    address
				 *
				 */

				typedef my::l4address address;


			private:  // our one and only datum
				mutable int sd;

			private:  // internal utility functions
				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) { throw tcpsocket::exception(this, EINVAL); };
				void ishutdown (void);
				void oshutdown (void);
				void ioshutdown (void);

				void connect (const socket::address &a);
				void connect (const std::string &a);
				void listen (int backlog);
				void listen (void);
				void disconnect (void);

				void accept (socket &s, socket::address &a);
				void accept (socket &s, std::string &a);
				void accept (socket &s);

				void name (socket::address &a);
				void peer (socket::address &a);

				void open (const std::string &fn, my::device::open_flags) { connect (fn); };
				void open (const std::string &fn, int mod) { connect (fn); };
				void close (void) throw();

			public:
				ssize_t read_vp  (void *b,       size_t l);
				ssize_t write_vp (const void *b, size_t l);

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

			public:
				// ctors/dtors
				tcpsocket (void);
				tcpsocket (const inet_address &a, int p);
				tcpsocket (const address &a);
				tcpsocket (const std::string &a, int p);
				~tcpsocket (void) throw () { close(); };

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


#endif /* __TCPSOCKET_H */
