|
17 | 17 |
|
18 | 18 | #include "asio/detail/config.hpp"
|
19 | 19 | #include <cstddef>
|
| 20 | +#include "asio/async_result.hpp" |
20 | 21 | #include "asio/buffered_read_stream_fwd.hpp"
|
21 | 22 | #include "asio/buffer.hpp"
|
22 | 23 | #include "asio/detail/bind_handler.hpp"
|
@@ -133,199 +134,65 @@ class buffered_read_stream
|
133 | 134 | /// Start an asynchronous write. The data being written must be valid for the
|
134 | 135 | /// lifetime of the asynchronous operation.
|
135 | 136 | template <typename ConstBufferSequence, typename WriteHandler>
|
136 |
| - void async_write_some(const ConstBufferSequence& buffers, |
137 |
| - WriteHandler handler) |
| 137 | + ASIO_INITFN_RESULT_TYPE(WriteHandler, |
| 138 | + void (asio::error_code, std::size_t)) |
| 139 | + async_write_some(const ConstBufferSequence& buffers, |
| 140 | + ASIO_MOVE_ARG(WriteHandler) handler) |
138 | 141 | {
|
139 |
| - next_layer_.async_write_some(buffers, handler); |
| 142 | + detail::async_result_init< |
| 143 | + WriteHandler, void (asio::error_code, std::size_t)> init( |
| 144 | + ASIO_MOVE_CAST(WriteHandler)(handler)); |
| 145 | + |
| 146 | + next_layer_.async_write_some(buffers, |
| 147 | + ASIO_MOVE_CAST(ASIO_HANDLER_TYPE(WriteHandler, |
| 148 | + void (asio::error_code, std::size_t)))(init.handler)); |
| 149 | + |
| 150 | + return init.result.get(); |
140 | 151 | }
|
141 | 152 |
|
142 | 153 | /// Fill the buffer with some data. Returns the number of bytes placed in the
|
143 | 154 | /// buffer as a result of the operation. Throws an exception on failure.
|
144 |
| - std::size_t fill() |
145 |
| - { |
146 |
| - detail::buffer_resize_guard<detail::buffered_stream_storage> |
147 |
| - resize_guard(storage_); |
148 |
| - std::size_t previous_size = storage_.size(); |
149 |
| - storage_.resize(storage_.capacity()); |
150 |
| - storage_.resize(previous_size + next_layer_.read_some(buffer( |
151 |
| - storage_.data() + previous_size, |
152 |
| - storage_.size() - previous_size))); |
153 |
| - resize_guard.commit(); |
154 |
| - return storage_.size() - previous_size; |
155 |
| - } |
| 155 | + std::size_t fill(); |
156 | 156 |
|
157 | 157 | /// Fill the buffer with some data. Returns the number of bytes placed in the
|
158 | 158 | /// buffer as a result of the operation, or 0 if an error occurred.
|
159 |
| - std::size_t fill(asio::error_code& ec) |
160 |
| - { |
161 |
| - detail::buffer_resize_guard<detail::buffered_stream_storage> |
162 |
| - resize_guard(storage_); |
163 |
| - std::size_t previous_size = storage_.size(); |
164 |
| - storage_.resize(storage_.capacity()); |
165 |
| - storage_.resize(previous_size + next_layer_.read_some(buffer( |
166 |
| - storage_.data() + previous_size, |
167 |
| - storage_.size() - previous_size), |
168 |
| - ec)); |
169 |
| - resize_guard.commit(); |
170 |
| - return storage_.size() - previous_size; |
171 |
| - } |
172 |
| - |
173 |
| - template <typename ReadHandler> |
174 |
| - class fill_handler |
175 |
| - { |
176 |
| - public: |
177 |
| - fill_handler(asio::io_service& io_service, |
178 |
| - detail::buffered_stream_storage& storage, |
179 |
| - std::size_t previous_size, ReadHandler handler) |
180 |
| - : io_service_(io_service), |
181 |
| - storage_(storage), |
182 |
| - previous_size_(previous_size), |
183 |
| - handler_(handler) |
184 |
| - { |
185 |
| - } |
186 |
| - |
187 |
| - void operator()(const asio::error_code& ec, |
188 |
| - std::size_t bytes_transferred) |
189 |
| - { |
190 |
| - storage_.resize(previous_size_ + bytes_transferred); |
191 |
| - io_service_.dispatch(detail::bind_handler( |
192 |
| - handler_, ec, bytes_transferred)); |
193 |
| - } |
194 |
| - |
195 |
| - private: |
196 |
| - asio::io_service& io_service_; |
197 |
| - detail::buffered_stream_storage& storage_; |
198 |
| - std::size_t previous_size_; |
199 |
| - ReadHandler handler_; |
200 |
| - }; |
| 159 | + std::size_t fill(asio::error_code& ec); |
201 | 160 |
|
202 | 161 | /// Start an asynchronous fill.
|
203 | 162 | template <typename ReadHandler>
|
204 |
| - void async_fill(ReadHandler handler) |
205 |
| - { |
206 |
| - std::size_t previous_size = storage_.size(); |
207 |
| - storage_.resize(storage_.capacity()); |
208 |
| - next_layer_.async_read_some( |
209 |
| - buffer( |
210 |
| - storage_.data() + previous_size, |
211 |
| - storage_.size() - previous_size), |
212 |
| - fill_handler<ReadHandler>(get_io_service(), |
213 |
| - storage_, previous_size, handler)); |
214 |
| - } |
| 163 | + ASIO_INITFN_RESULT_TYPE(ReadHandler, |
| 164 | + void (asio::error_code, std::size_t)) |
| 165 | + async_fill(ASIO_MOVE_ARG(ReadHandler) handler); |
215 | 166 |
|
216 | 167 | /// Read some data from the stream. Returns the number of bytes read. Throws
|
217 | 168 | /// an exception on failure.
|
218 | 169 | template <typename MutableBufferSequence>
|
219 |
| - std::size_t read_some(const MutableBufferSequence& buffers) |
220 |
| - { |
221 |
| - if (asio::buffer_size(buffers) == 0) |
222 |
| - return 0; |
223 |
| - |
224 |
| - if (storage_.empty()) |
225 |
| - fill(); |
226 |
| - |
227 |
| - return copy(buffers); |
228 |
| - } |
| 170 | + std::size_t read_some(const MutableBufferSequence& buffers); |
229 | 171 |
|
230 | 172 | /// Read some data from the stream. Returns the number of bytes read or 0 if
|
231 | 173 | /// an error occurred.
|
232 | 174 | template <typename MutableBufferSequence>
|
233 | 175 | std::size_t read_some(const MutableBufferSequence& buffers,
|
234 |
| - asio::error_code& ec) |
235 |
| - { |
236 |
| - ec = asio::error_code(); |
237 |
| - |
238 |
| - if (asio::buffer_size(buffers) == 0) |
239 |
| - return 0; |
240 |
| - |
241 |
| - if (storage_.empty() && !fill(ec)) |
242 |
| - return 0; |
243 |
| - |
244 |
| - return copy(buffers); |
245 |
| - } |
246 |
| - |
247 |
| - template <typename MutableBufferSequence, typename ReadHandler> |
248 |
| - class read_some_handler |
249 |
| - { |
250 |
| - public: |
251 |
| - read_some_handler(asio::io_service& io_service, |
252 |
| - detail::buffered_stream_storage& storage, |
253 |
| - const MutableBufferSequence& buffers, ReadHandler handler) |
254 |
| - : io_service_(io_service), |
255 |
| - storage_(storage), |
256 |
| - buffers_(buffers), |
257 |
| - handler_(handler) |
258 |
| - { |
259 |
| - } |
260 |
| - |
261 |
| - void operator()(const asio::error_code& ec, std::size_t) |
262 |
| - { |
263 |
| - if (ec || storage_.empty()) |
264 |
| - { |
265 |
| - std::size_t length = 0; |
266 |
| - io_service_.dispatch(detail::bind_handler(handler_, ec, length)); |
267 |
| - } |
268 |
| - else |
269 |
| - { |
270 |
| - std::size_t bytes_copied = asio::buffer_copy( |
271 |
| - buffers_, storage_.data(), storage_.size()); |
272 |
| - storage_.consume(bytes_copied); |
273 |
| - io_service_.dispatch(detail::bind_handler(handler_, ec, bytes_copied)); |
274 |
| - } |
275 |
| - } |
276 |
| - |
277 |
| - private: |
278 |
| - asio::io_service& io_service_; |
279 |
| - detail::buffered_stream_storage& storage_; |
280 |
| - MutableBufferSequence buffers_; |
281 |
| - ReadHandler handler_; |
282 |
| - }; |
| 176 | + asio::error_code& ec); |
283 | 177 |
|
284 | 178 | /// Start an asynchronous read. The buffer into which the data will be read
|
285 | 179 | /// must be valid for the lifetime of the asynchronous operation.
|
286 | 180 | template <typename MutableBufferSequence, typename ReadHandler>
|
287 |
| - void async_read_some(const MutableBufferSequence& buffers, |
288 |
| - ReadHandler handler) |
289 |
| - { |
290 |
| - if (asio::buffer_size(buffers) == 0) |
291 |
| - { |
292 |
| - get_io_service().post(detail::bind_handler( |
293 |
| - handler, asio::error_code(), 0)); |
294 |
| - } |
295 |
| - else if (storage_.empty()) |
296 |
| - { |
297 |
| - async_fill(read_some_handler<MutableBufferSequence, ReadHandler>( |
298 |
| - get_io_service(), storage_, buffers, handler)); |
299 |
| - } |
300 |
| - else |
301 |
| - { |
302 |
| - std::size_t length = copy(buffers); |
303 |
| - get_io_service().post(detail::bind_handler( |
304 |
| - handler, asio::error_code(), length)); |
305 |
| - } |
306 |
| - } |
| 181 | + ASIO_INITFN_RESULT_TYPE(ReadHandler, |
| 182 | + void (asio::error_code, std::size_t)) |
| 183 | + async_read_some(const MutableBufferSequence& buffers, |
| 184 | + ASIO_MOVE_ARG(ReadHandler) handler); |
307 | 185 |
|
308 | 186 | /// Peek at the incoming data on the stream. Returns the number of bytes read.
|
309 | 187 | /// Throws an exception on failure.
|
310 | 188 | template <typename MutableBufferSequence>
|
311 |
| - std::size_t peek(const MutableBufferSequence& buffers) |
312 |
| - { |
313 |
| - if (storage_.empty()) |
314 |
| - fill(); |
315 |
| - return peek_copy(buffers); |
316 |
| - } |
| 189 | + std::size_t peek(const MutableBufferSequence& buffers); |
317 | 190 |
|
318 | 191 | /// Peek at the incoming data on the stream. Returns the number of bytes read,
|
319 | 192 | /// or 0 if an error occurred.
|
320 | 193 | template <typename MutableBufferSequence>
|
321 | 194 | std::size_t peek(const MutableBufferSequence& buffers,
|
322 |
| - asio::error_code& ec) |
323 |
| - { |
324 |
| - ec = asio::error_code(); |
325 |
| - if (storage_.empty() && !fill(ec)) |
326 |
| - return 0; |
327 |
| - return peek_copy(buffers); |
328 |
| - } |
| 195 | + asio::error_code& ec); |
329 | 196 |
|
330 | 197 | /// Determine the amount of data that may be read without blocking.
|
331 | 198 | std::size_t in_avail()
|
@@ -372,4 +239,6 @@ class buffered_read_stream
|
372 | 239 |
|
373 | 240 | #include "asio/detail/pop_options.hpp"
|
374 | 241 |
|
| 242 | +#include "asio/impl/buffered_read_stream.hpp" |
| 243 | + |
375 | 244 | #endif // ASIO_BUFFERED_READ_STREAM_HPP
|
0 commit comments