00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00025 #ifndef SOCKETSTREAM_BASIC_IOSOCKETSTREAM_H
00026 #define SOCKETSTREAM_BASIC_IOSOCKETSTREAM_H 1
00027
00028 #include <socketstream/config.h>
00029
00030 #include <iostream>
00031 #include <socketstream/basic_socketbuf.h>
00032
00033 namespace ss {
00034
00038 template <typename _CharT, typename _Traits>
00039 class basic_socketstream
00040 : public std::basic_iostream<_CharT, _Traits>,
00041 public sockets_base
00042 {
00043 public:
00044
00046
00053 typedef _CharT char_type;
00054 typedef _Traits traits_type;
00055
00056 typedef typename traits_type::int_type int_type;
00057 typedef typename traits_type::pos_type pos_type;
00058 typedef typename traits_type::off_type off_type;
00060
00061 typedef basic_socketbuf<char_type, traits_type> __streambuf_type;
00062 typedef std::basic_iostream<char_type, traits_type> __stream_type;
00063
00064 template <typename C, typename T>
00065 friend class basic_listener;
00066
00070 basic_socketstream()
00071 : __stream_type(&_M_buffer), _M_buffer()
00072 { }
00073
00080 basic_socketstream(family __f, style __s)
00081 : __stream_type(&_M_buffer), _M_buffer(__f, __s)
00082 { }
00083
00089 virtual ~basic_socketstream()
00090 { }
00091
00097 bool
00098 is_open ()
00099 { return _M_buffer.is_open(); }
00100
00106 template <typename AddressT>
00107 void
00108 connect (AddressT& __addr)
00109 {
00110 if (!_M_buffer.connect(__addr))
00111 this->setstate(std::ios_base::failbit);
00112 }
00113
00123 void
00124 shutdown (shutmode __mode = sockets_base::rw)
00125 {
00126 if (!_M_buffer.shutdown(__mode))
00127 this->setstate(std::ios_base::failbit);
00128 }
00129
00133 __streambuf_type*
00134 rdbuf () const
00135 { return const_cast<__streambuf_type*>(&_M_buffer); }
00136
00137 __stream_type&
00138 operator>> (flag& __flag)
00139 {
00140 _M_buffer.setf(__flag);
00141 return *this;
00142 }
00143
00144 __stream_type&
00145 operator<< (flag& __flag)
00146 {
00147 _M_buffer.setf(__flag);
00148 return *this;
00149 }
00150
00151 protected:
00152
00158 basic_socketstream(basic_socket::descriptor_t descriptor)
00159 : __stream_type(&_M_buffer), _M_buffer(descriptor)
00160 { }
00161
00162 __streambuf_type _M_buffer;
00163
00164 };
00165
00166 typedef basic_socketstream<char, std::char_traits<char> > socketstream;
00167
00168 }
00169
00170 #endif // SOCKETSTREAM_BASIC_IOSOCKETSTREAM_H