Chris@16
|
1 //
|
Chris@16
|
2 // buffered_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_STREAM_HPP
|
Chris@16
|
12 #define BOOST_ASIO_BUFFERED_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.hpp>
|
Chris@16
|
22 #include <boost/asio/buffered_write_stream.hpp>
|
Chris@16
|
23 #include <boost/asio/buffered_stream_fwd.hpp>
|
Chris@16
|
24 #include <boost/asio/detail/noncopyable.hpp>
|
Chris@16
|
25 #include <boost/asio/error.hpp>
|
Chris@16
|
26 #include <boost/asio/io_service.hpp>
|
Chris@16
|
27
|
Chris@16
|
28 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost {
|
Chris@16
|
31 namespace asio {
|
Chris@16
|
32
|
Chris@16
|
33 /// Adds buffering to the read- and write-related operations of a stream.
|
Chris@16
|
34 /**
|
Chris@16
|
35 * The buffered_stream class template can be used to add buffering to the
|
Chris@16
|
36 * synchronous and asynchronous read and write operations of a stream.
|
Chris@16
|
37 *
|
Chris@16
|
38 * @par Thread Safety
|
Chris@16
|
39 * @e Distinct @e objects: Safe.@n
|
Chris@16
|
40 * @e Shared @e objects: Unsafe.
|
Chris@16
|
41 *
|
Chris@16
|
42 * @par Concepts:
|
Chris@16
|
43 * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
|
Chris@16
|
44 */
|
Chris@16
|
45 template <typename Stream>
|
Chris@16
|
46 class buffered_stream
|
Chris@16
|
47 : private noncopyable
|
Chris@16
|
48 {
|
Chris@16
|
49 public:
|
Chris@16
|
50 /// The type of the next layer.
|
Chris@16
|
51 typedef typename remove_reference<Stream>::type next_layer_type;
|
Chris@16
|
52
|
Chris@16
|
53 /// The type of the lowest layer.
|
Chris@16
|
54 typedef typename next_layer_type::lowest_layer_type lowest_layer_type;
|
Chris@16
|
55
|
Chris@16
|
56 /// Construct, passing the specified argument to initialise the next layer.
|
Chris@16
|
57 template <typename Arg>
|
Chris@16
|
58 explicit buffered_stream(Arg& a)
|
Chris@16
|
59 : inner_stream_impl_(a),
|
Chris@16
|
60 stream_impl_(inner_stream_impl_)
|
Chris@16
|
61 {
|
Chris@16
|
62 }
|
Chris@16
|
63
|
Chris@16
|
64 /// Construct, passing the specified argument to initialise the next layer.
|
Chris@16
|
65 template <typename Arg>
|
Chris@16
|
66 explicit buffered_stream(Arg& a, std::size_t read_buffer_size,
|
Chris@16
|
67 std::size_t write_buffer_size)
|
Chris@16
|
68 : inner_stream_impl_(a, write_buffer_size),
|
Chris@16
|
69 stream_impl_(inner_stream_impl_, read_buffer_size)
|
Chris@16
|
70 {
|
Chris@16
|
71 }
|
Chris@16
|
72
|
Chris@16
|
73 /// Get a reference to the next layer.
|
Chris@16
|
74 next_layer_type& next_layer()
|
Chris@16
|
75 {
|
Chris@16
|
76 return stream_impl_.next_layer().next_layer();
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 /// Get a reference to the lowest layer.
|
Chris@16
|
80 lowest_layer_type& lowest_layer()
|
Chris@16
|
81 {
|
Chris@16
|
82 return stream_impl_.lowest_layer();
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 /// Get a const reference to the lowest layer.
|
Chris@16
|
86 const lowest_layer_type& lowest_layer() const
|
Chris@16
|
87 {
|
Chris@16
|
88 return stream_impl_.lowest_layer();
|
Chris@16
|
89 }
|
Chris@16
|
90
|
Chris@16
|
91 /// Get the io_service associated with the object.
|
Chris@16
|
92 boost::asio::io_service& get_io_service()
|
Chris@16
|
93 {
|
Chris@16
|
94 return stream_impl_.get_io_service();
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 /// Close the stream.
|
Chris@16
|
98 void close()
|
Chris@16
|
99 {
|
Chris@16
|
100 stream_impl_.close();
|
Chris@16
|
101 }
|
Chris@16
|
102
|
Chris@16
|
103 /// Close the stream.
|
Chris@16
|
104 boost::system::error_code close(boost::system::error_code& ec)
|
Chris@16
|
105 {
|
Chris@16
|
106 return stream_impl_.close(ec);
|
Chris@16
|
107 }
|
Chris@16
|
108
|
Chris@16
|
109 /// Flush all data from the buffer to the next layer. Returns the number of
|
Chris@16
|
110 /// bytes written to the next layer on the last write operation. Throws an
|
Chris@16
|
111 /// exception on failure.
|
Chris@16
|
112 std::size_t flush()
|
Chris@16
|
113 {
|
Chris@16
|
114 return stream_impl_.next_layer().flush();
|
Chris@16
|
115 }
|
Chris@16
|
116
|
Chris@16
|
117 /// Flush all data from the buffer to the next layer. Returns the number of
|
Chris@16
|
118 /// bytes written to the next layer on the last write operation, or 0 if an
|
Chris@16
|
119 /// error occurred.
|
Chris@16
|
120 std::size_t flush(boost::system::error_code& ec)
|
Chris@16
|
121 {
|
Chris@16
|
122 return stream_impl_.next_layer().flush(ec);
|
Chris@16
|
123 }
|
Chris@16
|
124
|
Chris@16
|
125 /// Start an asynchronous flush.
|
Chris@16
|
126 template <typename WriteHandler>
|
Chris@16
|
127 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
128 void (boost::system::error_code, std::size_t))
|
Chris@16
|
129 async_flush(BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
130 {
|
Chris@16
|
131 return stream_impl_.next_layer().async_flush(
|
Chris@16
|
132 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
133 }
|
Chris@16
|
134
|
Chris@16
|
135 /// Write the given data to the stream. Returns the number of bytes written.
|
Chris@16
|
136 /// Throws an exception on failure.
|
Chris@16
|
137 template <typename ConstBufferSequence>
|
Chris@16
|
138 std::size_t write_some(const ConstBufferSequence& buffers)
|
Chris@16
|
139 {
|
Chris@16
|
140 return stream_impl_.write_some(buffers);
|
Chris@16
|
141 }
|
Chris@16
|
142
|
Chris@16
|
143 /// Write the given data to the stream. Returns the number of bytes written,
|
Chris@16
|
144 /// or 0 if an error occurred.
|
Chris@16
|
145 template <typename ConstBufferSequence>
|
Chris@16
|
146 std::size_t write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
147 boost::system::error_code& ec)
|
Chris@16
|
148 {
|
Chris@16
|
149 return stream_impl_.write_some(buffers, ec);
|
Chris@16
|
150 }
|
Chris@16
|
151
|
Chris@16
|
152 /// Start an asynchronous write. The data being written must be valid for the
|
Chris@16
|
153 /// lifetime of the asynchronous operation.
|
Chris@16
|
154 template <typename ConstBufferSequence, typename WriteHandler>
|
Chris@16
|
155 BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
Chris@16
|
156 void (boost::system::error_code, std::size_t))
|
Chris@16
|
157 async_write_some(const ConstBufferSequence& buffers,
|
Chris@16
|
158 BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
|
Chris@16
|
159 {
|
Chris@16
|
160 return stream_impl_.async_write_some(buffers,
|
Chris@16
|
161 BOOST_ASIO_MOVE_CAST(WriteHandler)(handler));
|
Chris@16
|
162 }
|
Chris@16
|
163
|
Chris@16
|
164 /// Fill the buffer with some data. Returns the number of bytes placed in the
|
Chris@16
|
165 /// buffer as a result of the operation. Throws an exception on failure.
|
Chris@16
|
166 std::size_t fill()
|
Chris@16
|
167 {
|
Chris@16
|
168 return stream_impl_.fill();
|
Chris@16
|
169 }
|
Chris@16
|
170
|
Chris@16
|
171 /// Fill the buffer with some data. Returns the number of bytes placed in the
|
Chris@16
|
172 /// buffer as a result of the operation, or 0 if an error occurred.
|
Chris@16
|
173 std::size_t fill(boost::system::error_code& ec)
|
Chris@16
|
174 {
|
Chris@16
|
175 return stream_impl_.fill(ec);
|
Chris@16
|
176 }
|
Chris@16
|
177
|
Chris@16
|
178 /// Start an asynchronous fill.
|
Chris@16
|
179 template <typename ReadHandler>
|
Chris@16
|
180 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
181 void (boost::system::error_code, std::size_t))
|
Chris@16
|
182 async_fill(BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
|
Chris@16
|
183 {
|
Chris@16
|
184 return stream_impl_.async_fill(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
|
Chris@16
|
185 }
|
Chris@16
|
186
|
Chris@16
|
187 /// Read some data from the stream. Returns the number of bytes read. Throws
|
Chris@16
|
188 /// an exception on failure.
|
Chris@16
|
189 template <typename MutableBufferSequence>
|
Chris@16
|
190 std::size_t read_some(const MutableBufferSequence& buffers)
|
Chris@16
|
191 {
|
Chris@16
|
192 return stream_impl_.read_some(buffers);
|
Chris@16
|
193 }
|
Chris@16
|
194
|
Chris@16
|
195 /// Read some data from the stream. Returns the number of bytes read or 0 if
|
Chris@16
|
196 /// an error occurred.
|
Chris@16
|
197 template <typename MutableBufferSequence>
|
Chris@16
|
198 std::size_t read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
199 boost::system::error_code& ec)
|
Chris@16
|
200 {
|
Chris@16
|
201 return stream_impl_.read_some(buffers, ec);
|
Chris@16
|
202 }
|
Chris@16
|
203
|
Chris@16
|
204 /// Start an asynchronous read. The buffer into which the data will be read
|
Chris@16
|
205 /// must be valid for the lifetime of the asynchronous operation.
|
Chris@16
|
206 template <typename MutableBufferSequence, typename ReadHandler>
|
Chris@16
|
207 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
Chris@16
|
208 void (boost::system::error_code, std::size_t))
|
Chris@16
|
209 async_read_some(const MutableBufferSequence& buffers,
|
Chris@16
|
210 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
|
Chris@16
|
211 {
|
Chris@16
|
212 return stream_impl_.async_read_some(buffers,
|
Chris@16
|
213 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
|
Chris@16
|
214 }
|
Chris@16
|
215
|
Chris@16
|
216 /// Peek at the incoming data on the stream. Returns the number of bytes read.
|
Chris@16
|
217 /// Throws an exception on failure.
|
Chris@16
|
218 template <typename MutableBufferSequence>
|
Chris@16
|
219 std::size_t peek(const MutableBufferSequence& buffers)
|
Chris@16
|
220 {
|
Chris@16
|
221 return stream_impl_.peek(buffers);
|
Chris@16
|
222 }
|
Chris@16
|
223
|
Chris@16
|
224 /// Peek at the incoming data on the stream. Returns the number of bytes read,
|
Chris@16
|
225 /// or 0 if an error occurred.
|
Chris@16
|
226 template <typename MutableBufferSequence>
|
Chris@16
|
227 std::size_t peek(const MutableBufferSequence& buffers,
|
Chris@16
|
228 boost::system::error_code& ec)
|
Chris@16
|
229 {
|
Chris@16
|
230 return stream_impl_.peek(buffers, ec);
|
Chris@16
|
231 }
|
Chris@16
|
232
|
Chris@16
|
233 /// Determine the amount of data that may be read without blocking.
|
Chris@16
|
234 std::size_t in_avail()
|
Chris@16
|
235 {
|
Chris@16
|
236 return stream_impl_.in_avail();
|
Chris@16
|
237 }
|
Chris@16
|
238
|
Chris@16
|
239 /// Determine the amount of data that may be read without blocking.
|
Chris@16
|
240 std::size_t in_avail(boost::system::error_code& ec)
|
Chris@16
|
241 {
|
Chris@16
|
242 return stream_impl_.in_avail(ec);
|
Chris@16
|
243 }
|
Chris@16
|
244
|
Chris@16
|
245 private:
|
Chris@16
|
246 // The buffered write stream.
|
Chris@16
|
247 typedef buffered_write_stream<Stream> write_stream_type;
|
Chris@16
|
248 write_stream_type inner_stream_impl_;
|
Chris@16
|
249
|
Chris@16
|
250 // The buffered read stream.
|
Chris@16
|
251 typedef buffered_read_stream<write_stream_type&> read_stream_type;
|
Chris@16
|
252 read_stream_type stream_impl_;
|
Chris@16
|
253 };
|
Chris@16
|
254
|
Chris@16
|
255 } // namespace asio
|
Chris@16
|
256 } // namespace boost
|
Chris@16
|
257
|
Chris@16
|
258 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
259
|
Chris@16
|
260 #endif // BOOST_ASIO_BUFFERED_STREAM_HPP
|