00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2008 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien_jorge@yahoo.fr 00024 */ 00031 #ifndef __CLAW_BASIC_SOCKETBUF_HPP__ 00032 #define __CLAW_BASIC_SOCKETBUF_HPP__ 00033 00034 #include <streambuf> 00035 #include <string> 00036 #include <claw/basic_socket.hpp> 00037 00038 namespace claw 00039 { 00040 namespace net 00041 { 00047 template< typename CharT, typename Traits > 00048 class basic_socketbuf : private basic_socket, 00049 public std::basic_streambuf<CharT, Traits> 00050 { 00051 public: 00052 typedef CharT char_type; 00053 typedef Traits traits_type; 00054 typedef typename traits_type::int_type int_type; 00055 typedef typename traits_type::pos_type pos_type; 00056 typedef typename traits_type::off_type off_type; 00057 00058 typedef basic_socketbuf<char_type, traits_type> self_type; 00059 00060 public: 00061 basic_socketbuf(); 00062 virtual ~basic_socketbuf(); 00063 00064 self_type* open( const std::string& addr, int port ); 00065 self_type* open( socket_traits::descriptor d ); 00066 self_type* close(); 00067 00068 bool is_open() const; 00069 00070 protected: 00071 virtual int sync(); 00072 virtual int_type underflow(); 00073 virtual int_type overflow( int_type c = traits_type::eof() ); 00074 00075 private: 00076 bool connect( const std::string& addr, int port ); 00077 00078 void create_buffers(); 00079 void destroy_buffers(); 00080 00081 bool buffered() const; 00082 00083 private: 00085 char_type* m_input_buffer; 00086 00088 size_t m_input_buffer_size; 00089 00091 char_type* m_output_buffer; 00092 00094 size_t m_output_buffer_size; 00095 00097 static const size_t s_buffer_size; 00098 00099 }; // class basic_socketbuf 00100 } // namespace net 00101 } // namespace claw 00102 00103 #include <claw/impl/basic_socketbuf.tpp> 00104 00105 #endif // __CLAW_BASIC_SOCKETBUF_HPP__