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