#ifndef __SOCKET_STREAM_H_INCLUDED__
#define __SOCKET_STREAM_H_INCLUDED__

#include "socket.h"
#include "streambufImpl.h"
#include "abortable.h"

// The basic_tcpstreambuf class is the streambuf that will form the basis
// of the tcpstream class
template <typename Trait>
class basic_tcpstreambuf : public streambuf_impl<Trait, dual_buffer< typename Trait::char_type> > {
  public:
	// CONSTRUCTORS

	// Construct an uninitialized streambuf
	basic_tcpstreambuf();

	// Construct a streambuf and initialize it with an open tcp_socket
	basic_tcpstreambuf(tcp_socket& s);

	// Abort an I/O operation
	void abort();

	// Abort completed
	void clear_abort();
};

// tcpstreambuf is for char I/O
// wtcpstreambuf is for w_char I/O
typedef basic_tcpstreambuf< std::char_traits<char> > tcpstreambuf;
typedef basic_tcpstreambuf< std::char_traits<wchar_t> > wtcpstreambuf;

// The basic_tcpstream class can be used to perform formatted I/O
template <typename BS>
class basic_tcpstream : public BS, public ios_abortable {
  public:
	// CONSTRUCTORS

	// Construct an uninitialized streambuf
	basic_tcpstream();

	// Construct a streambuf and initialize it with an open tcp_socket
	basic_tcpstream(tcp_socket& s);

	// Abort an I/O operation
	virtual void abort() { tcpbuf.abort(); }

	// Abort completed
	virtual void clear_abort() { tcpbuf.clear_abort(); }

  private:
	// The underlying basic_tcpstreambuf that the stream uses
	basic_tcpstreambuf<typename BS::traits_type> tcpbuf;
};

// Input stream, output stream, and input/output stream.
// It is unlikely that itcpstream and otcpstream will ever be used.
typedef basic_tcpstream< std::istream >  itcpstream;
typedef basic_tcpstream< std::ostream >  otcpstream;
typedef basic_tcpstream< std::iostream > tcpstream;

#endif  // __SOCKET_STREAM_H_INCLUDED__
