Claw
1.7.3
|
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-2011 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@gamned.org 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: 00053 typedef CharT char_type; 00054 00056 typedef Traits traits_type; 00057 00059 typedef typename traits_type::int_type int_type; 00060 00062 typedef typename traits_type::pos_type pos_type; 00063 00065 typedef typename traits_type::off_type off_type; 00066 00068 typedef basic_socketbuf<char_type, traits_type> self_type; 00069 00070 public: 00071 basic_socketbuf( int read_limit = -1 ); 00072 virtual ~basic_socketbuf(); 00073 00074 self_type* open( const std::string& addr, int port ); 00075 self_type* open( socket_traits::descriptor d ); 00076 self_type* close(); 00077 00078 bool is_open() const; 00079 00080 void set_read_time_limit( int read_limit ); 00081 00082 protected: 00083 virtual int sync(); 00084 virtual int_type underflow(); 00085 virtual int_type overflow( int_type c = traits_type::eof() ); 00086 00087 private: 00088 bool connect( const std::string& addr, int port ); 00089 00090 void create_buffers(); 00091 void destroy_buffers(); 00092 00093 bool buffered() const; 00094 00095 private: 00098 int m_read_limit; 00099 00101 char_type* m_input_buffer; 00102 00104 size_t m_input_buffer_size; 00105 00107 char_type* m_output_buffer; 00108 00110 size_t m_output_buffer_size; 00111 00113 static const size_t s_buffer_size; 00114 00115 }; // class basic_socketbuf 00116 } // namespace net 00117 } // namespace claw 00118 00119 #include <claw/impl/basic_socketbuf.tpp> 00120 00121 #endif // __CLAW_BASIC_SOCKETBUF_HPP__