Claw  1.7.0
basic_socketbuf.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
31 #ifndef __CLAW_BASIC_SOCKETBUF_HPP__
32 #define __CLAW_BASIC_SOCKETBUF_HPP__
33 
34 #include <streambuf>
35 #include <string>
36 #include <claw/basic_socket.hpp>
37 
38 namespace claw
39 {
40  namespace net
41  {
47  template< typename CharT, typename Traits >
48  class basic_socketbuf : private basic_socket,
49  public std::basic_streambuf<CharT, Traits>
50  {
51  public:
52  typedef CharT char_type;
53  typedef Traits traits_type;
54  typedef typename traits_type::int_type int_type;
55  typedef typename traits_type::pos_type pos_type;
56  typedef typename traits_type::off_type off_type;
57 
59 
60  public:
61  basic_socketbuf( int read_limit = -1 );
62  virtual ~basic_socketbuf();
63 
64  self_type* open( const std::string& addr, int port );
66  self_type* close();
67 
68  bool is_open() const;
69 
70  void set_read_time_limit( int read_limit );
71 
72  protected:
73  virtual int sync();
74  virtual int_type underflow();
75  virtual int_type overflow( int_type c = traits_type::eof() );
76 
77  private:
78  bool connect( const std::string& addr, int port );
79 
80  void create_buffers();
81  void destroy_buffers();
82 
83  bool buffered() const;
84 
85  private:
88  int m_read_limit;
89 
91  char_type* m_input_buffer;
92 
94  size_t m_input_buffer_size;
95 
97  char_type* m_output_buffer;
98 
100  size_t m_output_buffer_size;
101 
103  static const size_t s_buffer_size;
104 
105  }; // class basic_socketbuf
106  } // namespace net
107 } // namespace claw
108 
110 
111 #endif // __CLAW_BASIC_SOCKETBUF_HPP__