Chris@16
|
1 //
|
Chris@16
|
2 // buffered_read_stream.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_BUFFERED_READ_STREAM_HPP
|
Chris@16
|
12 #define BOOST_ASIO_BUFFERED_READ_STREAM_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19 #include <cstddef>
|
Chris@16
|
20 #include <boost/asio/async_result.hpp>
|
Chris@16
|
21 #include <boost/asio/buffered_read_stream_fwd.hpp>
|
Chris@16
|
22 #include <boost/asio/buffer.hpp>
|
Chris@16
|
23 #include <boost/asio/detail/bind_handler.hpp>
|
Chris@16
|
24 #include <boost/asio/detail/buffer_resize_guard.hpp>
|
Chris@16
|
25 #include <boost/asio/detail/buffered_stream_storage.hpp>
|
Chris@16
|
26 #include <boost/asio/detail/noncopyable.hpp>
|
Chris@16
|
27 #include <boost/asio/detail/type_traits.hpp>
|
Chris@16
|
28 #include <boost/asio/error.hpp>
|
Chris@16
|
29 #include <boost/asio/io_service.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost {
|
Chris@16
|
34 namespace asio {
|
Chris@16
|
35
|
Chris@16
|
36 /// Adds buffering to the read-related operations of a stream.
|
Chris@16
|
37 /**
|
Chris@16
|
38 * The buffered_read_stream class template can be used to add buffering to the
|
Chris@16
|
39 * synchronous and asynchronous read operations of a stream.
|
Chris@16
|
40 *
|
Chris@16
|
41 * @par Thread Safety
|
Chris@16
|
42 * @e Distinct @e objects: Safe.@n
|
Chris@16
|
43 * @e Shared @e objects: Unsafe.
|
Chris@16
|
44 *
|
Chris@16
|
45 * @par Concepts:
|
Chris@16
|
46 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
|
Chris@16
|
47 */
|
Chris@16
|
48 template <typename Stream>
|
Chris@16
|
49 class buffered_read_stream
|
Chris@16
|
50 : private noncopyable
|
Chris@16
|
51 {
|
Chris@16
|
52 public:
|
Chris@16
|
53 /// The type of the next layer.
|
Chris@16
|
54 typedef typename remove_reference<Stream>::type next_layer_type;
|
Chris@16
|
55
|
Chris@16
|
56 /// The type of the lowest layer.
|
Chris@16
|
57 typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
|
Chris@16
|
58
|
Chris@16
|
59 #if defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
60 /// The default buffer size.
|
Chris@16
|
61 static const std::size_t default_buffer_size = implementation_defined;
|
Chris@16
|
62 #else
|
Chris@16
|
63 BOOST_ASIO_STATIC_CONSTANT(std::size_t, default_buffer_size = 1024);
|
Chris@16
|
64 #endif
|
Chris@16
|
65
|
Chris@16
|
66 /// Construct, passing the specified argument to initialise the next layer.
|
Chris@16
|
67 template <typename Arg>
|
Chris@16
|
68 explicit buffered_read_stream(Arg& a)
|
Chris@16
|
69 : next_layer_(a),
|
Chris@16
|
70 storage_(default_buffer_size)
|
Chris@16
|
71 {
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 /// Construct, passing the specified argument to initialise the next layer.
|
Chris@16
|
75 template <typename Arg>
|
Chris@16
|
76 buffered_read_stream(Arg& a, std::size_t buffer_size)
|
Chris@16
|
77 : next_layer_(a),
|
Chris@16
|
78 storage_(buffer_size)
|
Chris@16
|
79 {
|
Chris@16
|
80 }
|
Chris@16
|
81
|
Chris@16
|
82 /// Get a reference to the next layer.
|
Chris@16
|
83 next_layer_type& next_layer()
|
Chris@16
|
84 {
|
Chris@16
|
85 return next_layer_;
|
Chris@16
|
86 }
|
Chris@16
|
87
|
Chris@16
|
88 /// Get a reference to the lowest layer.
|
Chris@16
|
89 lowest_layer_type& lowest_layer()
|
Chris@16
|
90 {
|
Chris@16
|
91 return next_layer_.lowest_layer();
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 /// Get a const reference to the lowest layer.
|
Chris@16
|
95 const lowest_layer_type& lowest_layer() const
|
Chris@16
|
96 {
|
Chris@16
|
97 return next_layer_.lowest_layer();
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 /// Get the io_service associated with the object.
|
Chris@16
|
101 boost::asio::io_service& get_io_service()
|
Chris@16
|
102 {
|
Chris@16
|
103 return next_layer_.get_io_service();
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 /// Close the stream.
|
Chris@16
|
107 void close()
|
Chris@16
|
108 {
|
Chris@16
|
109 next_layer_.close();
|
Chris@16
|
110 }
|
Chris@16
|
111
|
Chris@16
|
112 /// Close the stream.
|
Chris@16
|
113 boost::system::error_code close(boost::system::error_code& ec)
|
Chris@16
|
114 {
|
Chris@16
|
115 return next_layer_.close(ec);
|
Chris@16
|
116 }
|
Chris@16
|
117
|
Chris@16
|
118 /// Write the given data to the stream. Returns the number of bytes written.
|
Chris@16
|
119 /// Throws an exception on failure.
|
Chris@16
|
120 template <typename ConstBufferSequence>
|
Chris@16
|
121 std::size_t write_some(const ConstBufferSequence& buffers)
|
Chris@16
|
122 {
|
Chris@16
|
123 return next_layer_.write_some(buffers);
|
Chris@16
|
124 }
|
Chris@16
|
125
|
Chris@16
|
126 /// Write the given data to the stream. Returns the number of bytes written,
|
Chris@16
|
127 /// or 0 if an error occurred.
|
Chris@16
|
128 template <typename ConstBufferSequence>
|
Chris@16
|
129 std::size_t write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
130 boost::system::error_code& ec)
|
Chris@16
|
131 {
|
Chris@16
|
132 return next_layer_.write_some(buffers, ec);
|
Chris@16
|
133 }
|
Chris@16
|
134
|
Chris@16
|
135 /// Start an asynchronous write. The data being written must be valid for the
|
Chris@16
|
136 /// lifetime of the asynchronous operation.
|
Chris@16
|
137 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
138 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
139 void (boost::system::error_code, std::size_t))
|
Chris@16
|
140 async_write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
141 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
142 {
|
Chris@16
|
143 detail::async_result_init<
|
Chris@16
|
144 WriteHandler, void (boost::system::error_code, std::size_t)> init(
|
Chris@16
|
145 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
146
|
Chris@16
|
147 next_layer_.async_write_some(buffers,
|
Chris@16
|
148 BOOST_ASIO_MOVE_CAST(BOOST_ASIO_HANDLER_TYPE(WriteHandler,
|
Chris@16
|
149 void (boost::system::error_code, std::size_t)))(init.handler));
|
Chris@16
|
150
|
Chris@16
|
151 return init.result.get();
|
Chris@16
|
152 }
|
Chris@16
|
153
|
Chris@16
|
154 /// Fill the buffer with some data. Returns the number of bytes placed in the
|
Chris@16
|
155 /// buffer as a result of the operation. Throws an exception on failure.
|
Chris@16
|
156 std::size_t fill();
|
Chris@16
|
157
|
Chris@16
|
158 /// Fill the buffer with some data. Returns the number of bytes placed in the
|
Chris@16
|
159 /// buffer as a result of the operation, or 0 if an error occurred.
|
Chris@16
|
160 std::size_t fill(boost::system::error_code& ec);
|
Chris@16
|
161
|
Chris@16
|
162 /// Start an asynchronous fill.
|
Chris@16
|
163 template <typename ReadHandler>
|
Chris@16
|
164 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
165 void (boost::system::error_code, std::size_t))
|
Chris@16
|
166 async_fill(BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
|
Chris@16
|
167
|
Chris@16
|
168 /// Read some data from the stream. Returns the number of bytes read. Throws
|
Chris@16
|
169 /// an exception on failure.
|
Chris@16
|
170 template <typename MutableBufferSequence>
|
Chris@16
|
171 std::size_t read_some(const MutableBufferSequence& buffers);
|
Chris@16
|
172
|
Chris@16
|
173 /// Read some data from the stream. Returns the number of bytes read or 0 if
|
Chris@16
|
174 /// an error occurred.
|
Chris@16
|
175 template <typename MutableBufferSequence>
|
Chris@16
|
176 std::size_t read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
177 boost::system::error_code& ec);
|
Chris@16
|
178
|
Chris@16
|
179 /// Start an asynchronous read. The buffer into which the data will be read
|
Chris@16
|
180 /// must be valid for the lifetime of the asynchronous operation.
|
Chris@16
|
181 template <typename MutableBufferSequence, typename ReadHandler>
|
Chris@16
|
182 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
183 void (boost::system::error_code, std::size_t))
|
Chris@16
|
184 async_read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
185 BOOST_ASIO_MOVE_ARG(ReadHandler) handler);
|
Chris@16
|
186
|
Chris@16
|
187 /// Peek at the incoming data on the stream. Returns the number of bytes read.
|
Chris@16
|
188 /// Throws an exception on failure.
|
Chris@16
|
189 template <typename MutableBufferSequence>
|
Chris@16
|
190 std::size_t peek(const MutableBufferSequence& buffers);
|
Chris@16
|
191
|
Chris@16
|
192 /// Peek at the incoming data on the stream. Returns the number of bytes read,
|
Chris@16
|
193 /// or 0 if an error occurred.
|
Chris@16
|
194 template <typename MutableBufferSequence>
|
Chris@16
|
195 std::size_t peek(const MutableBufferSequence& buffers,
|
Chris@16
|
196 boost::system::error_code& ec);
|
Chris@16
|
197
|
Chris@16
|
198 /// Determine the amount of data that may be read without blocking.
|
Chris@16
|
199 std::size_t in_avail()
|
Chris@16
|
200 {
|
Chris@16
|
201 return storage_.size();
|
Chris@16
|
202 }
|
Chris@16
|
203
|
Chris@16
|
204 /// Determine the amount of data that may be read without blocking.
|
Chris@16
|
205 std::size_t in_avail(boost::system::error_code& ec)
|
Chris@16
|
206 {
|
Chris@16
|
207 ec = boost::system::error_code();
|
Chris@16
|
208 return storage_.size();
|
Chris@16
|
209 }
|
Chris@16
|
210
|
Chris@16
|
211 private:
|
Chris@16
|
212 /// Copy data out of the internal buffer to the specified target buffer.
|
Chris@16
|
213 /// Returns the number of bytes copied.
|
Chris@16
|
214 template <typename MutableBufferSequence>
|
Chris@16
|
215 std::size_t copy(const MutableBufferSequence& buffers)
|
Chris@16
|
216 {
|
Chris@16
|
217 std::size_t bytes_copied = boost::asio::buffer_copy(
|
Chris@16
|
218 buffers, storage_.data(), storage_.size());
|
Chris@16
|
219 storage_.consume(bytes_copied);
|
Chris@16
|
220 return bytes_copied;
|
Chris@16
|
221 }
|
Chris@16
|
222
|
Chris@16
|
223 /// Copy data from the internal buffer to the specified target buffer, without
|
Chris@16
|
224 /// removing the data from the internal buffer. Returns the number of bytes
|
Chris@16
|
225 /// copied.
|
Chris@16
|
226 template <typename MutableBufferSequence>
|
Chris@16
|
227 std::size_t peek_copy(const MutableBufferSequence& buffers)
|
Chris@16
|
228 {
|
Chris@16
|
229 return boost::asio::buffer_copy(buffers, storage_.data(), storage_.size());
|
Chris@16
|
230 }
|
Chris@16
|
231
|
Chris@16
|
232 /// The next layer.
|
Chris@16
|
233 Stream next_layer_;
|
Chris@16
|
234
|
Chris@16
|
235 // The data in the buffer.
|
Chris@16
|
236 detail::buffered_stream_storage storage_;
|
Chris@16
|
237 };
|
Chris@16
|
238
|
Chris@16
|
239 } // namespace asio
|
Chris@16
|
240 } // namespace boost
|
Chris@16
|
241
|
Chris@16
|
242 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
243
|
Chris@16
|
244 #include <boost/asio/impl/buffered_read_stream.hpp>
|
Chris@16
|
245
|
Chris@16
|
246 #endif // BOOST_ASIO_BUFFERED_READ_STREAM_HPP
|