claw  1.9.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@stuff-o-matic.com
24 */
31 #ifndef __CLAW_BASIC_SOCKETBUF_HPP__
32 #define __CLAW_BASIC_SOCKETBUF_HPP__
33 
35 
36 #include <streambuf>
37 #include <string>
38 
39 namespace claw
40 {
41  namespace net
42  {
48  template <typename CharT, typename Traits>
50  : private basic_socket
51  , public std::basic_streambuf<CharT, Traits>
52  {
53  public:
55  typedef CharT char_type;
56 
58  typedef Traits traits_type;
59 
61  typedef typename traits_type::int_type int_type;
62 
64  typedef typename traits_type::pos_type pos_type;
65 
67  typedef typename traits_type::off_type off_type;
68 
71 
72  public:
73  basic_socketbuf(int read_limit = -1);
74  virtual ~basic_socketbuf();
75 
76  self_type* open(const std::string& addr, int port);
78  self_type* close();
79 
80  bool is_open() const;
81 
82  void set_read_time_limit(int read_limit);
83 
84  protected:
85  virtual int sync();
86  virtual int_type underflow();
87  virtual int_type overflow(int_type c = traits_type::eof());
88 
89  private:
90  bool connect(const std::string& addr, int port);
91 
92  void create_buffers();
93  void destroy_buffers();
94 
95  bool buffered() const;
96 
97  private:
100  int m_read_limit;
101 
103  char_type* m_input_buffer;
104 
106  size_t m_input_buffer_size;
107 
109  char_type* m_output_buffer;
110 
112  size_t m_output_buffer_size;
113 
115  static const size_t s_buffer_size;
116 
117  }; // class basic_socketbuf
118  }
119 }
120 
121 #include <claw/net/basic_socketbuf.tpp>
122 
123 #endif // __CLAW_BASIC_SOCKETBUF_HPP__
basic_socket * open()
Open the socket.
traits_type::off_type off_type
The type used to represent a delta in the positions.
traits_type::int_type int_type
The type used to represent integers.
traits_type::pos_type pos_type
The type used to represent a position in the stream.
Common interface for claw::net::basic_socketbuf and claw::net::socket_server.
CharT char_type
The type of the characters in the stream.
int descriptor
Type of the system description of the socket.
basic_socketbuf< char_type, traits_type > self_type
The type of this implementation.
Common interface for claw::net::basic_socketbuf and claw::net::socket_server.
Socket buffer to be used with std::basic_stream, for easy socket reading and writing.
This is the main namespace.
Definition: application.hpp:49
Traits traits_type
The traits carry some informations about the char_type.