1 //
2 // impl/read_at.hpp
3 // ~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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_IMPL_READ_AT_HPP
12 #define BOOST_ASIO_IMPL_READ_AT_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <algorithm>
19 #include <boost/asio/associated_allocator.hpp>
20 #include <boost/asio/associated_executor.hpp>
21 #include <boost/asio/buffer.hpp>
22 #include <boost/asio/completion_condition.hpp>
23 #include <boost/asio/detail/array_fwd.hpp>
24 #include <boost/asio/detail/base_from_completion_cond.hpp>
25 #include <boost/asio/detail/bind_handler.hpp>
26 #include <boost/asio/detail/consuming_buffers.hpp>
27 #include <boost/asio/detail/dependent_type.hpp>
28 #include <boost/asio/detail/handler_alloc_helpers.hpp>
29 #include <boost/asio/detail/handler_cont_helpers.hpp>
30 #include <boost/asio/detail/handler_invoke_helpers.hpp>
31 #include <boost/asio/detail/handler_tracking.hpp>
32 #include <boost/asio/detail/handler_type_requirements.hpp>
33 #include <boost/asio/detail/non_const_lvalue.hpp>
34 #include <boost/asio/detail/throw_error.hpp>
35 #include <boost/asio/error.hpp>
36
37 #include <boost/asio/detail/push_options.hpp>
38
39 namespace boost {
40 namespace asio {
41
42 namespace detail
43 {
44 template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
45 typename MutableBufferIterator, typename CompletionCondition>
read_at_buffer_sequence(SyncRandomAccessReadDevice & d,uint64_t offset,const MutableBufferSequence & buffers,const MutableBufferIterator &,CompletionCondition completion_condition,boost::system::error_code & ec)46 std::size_t read_at_buffer_sequence(SyncRandomAccessReadDevice& d,
47 uint64_t offset, const MutableBufferSequence& buffers,
48 const MutableBufferIterator&, CompletionCondition completion_condition,
49 boost::system::error_code& ec)
50 {
51 ec = boost::system::error_code();
52 boost::asio::detail::consuming_buffers<mutable_buffer,
53 MutableBufferSequence, MutableBufferIterator> tmp(buffers);
54 while (!tmp.empty())
55 {
56 if (std::size_t max_size = detail::adapt_completion_condition_result(
57 completion_condition(ec, tmp.total_consumed())))
58 {
59 tmp.consume(d.read_some_at(offset + tmp.total_consumed(),
60 tmp.prepare(max_size), ec));
61 }
62 else
63 break;
64 }
65 return tmp.total_consumed();
66 }
67 } // namespace detail
68
69 template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
70 typename CompletionCondition>
read_at(SyncRandomAccessReadDevice & d,uint64_t offset,const MutableBufferSequence & buffers,CompletionCondition completion_condition,boost::system::error_code & ec)71 std::size_t read_at(SyncRandomAccessReadDevice& d,
72 uint64_t offset, const MutableBufferSequence& buffers,
73 CompletionCondition completion_condition, boost::system::error_code& ec)
74 {
75 return detail::read_at_buffer_sequence(d, offset, buffers,
76 boost::asio::buffer_sequence_begin(buffers),
77 BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
78 }
79
80 template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence>
read_at(SyncRandomAccessReadDevice & d,uint64_t offset,const MutableBufferSequence & buffers)81 inline std::size_t read_at(SyncRandomAccessReadDevice& d,
82 uint64_t offset, const MutableBufferSequence& buffers)
83 {
84 boost::system::error_code ec;
85 std::size_t bytes_transferred = read_at(
86 d, offset, buffers, transfer_all(), ec);
87 boost::asio::detail::throw_error(ec, "read_at");
88 return bytes_transferred;
89 }
90
91 template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence>
read_at(SyncRandomAccessReadDevice & d,uint64_t offset,const MutableBufferSequence & buffers,boost::system::error_code & ec)92 inline std::size_t read_at(SyncRandomAccessReadDevice& d,
93 uint64_t offset, const MutableBufferSequence& buffers,
94 boost::system::error_code& ec)
95 {
96 return read_at(d, offset, buffers, transfer_all(), ec);
97 }
98
99 template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
100 typename CompletionCondition>
read_at(SyncRandomAccessReadDevice & d,uint64_t offset,const MutableBufferSequence & buffers,CompletionCondition completion_condition)101 inline std::size_t read_at(SyncRandomAccessReadDevice& d,
102 uint64_t offset, const MutableBufferSequence& buffers,
103 CompletionCondition completion_condition)
104 {
105 boost::system::error_code ec;
106 std::size_t bytes_transferred = read_at(d, offset, buffers,
107 BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
108 boost::asio::detail::throw_error(ec, "read_at");
109 return bytes_transferred;
110 }
111
112 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
113 #if !defined(BOOST_ASIO_NO_IOSTREAM)
114
115 template <typename SyncRandomAccessReadDevice, typename Allocator,
116 typename CompletionCondition>
read_at(SyncRandomAccessReadDevice & d,uint64_t offset,boost::asio::basic_streambuf<Allocator> & b,CompletionCondition completion_condition,boost::system::error_code & ec)117 std::size_t read_at(SyncRandomAccessReadDevice& d,
118 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
119 CompletionCondition completion_condition, boost::system::error_code& ec)
120 {
121 ec = boost::system::error_code();
122 std::size_t total_transferred = 0;
123 std::size_t max_size = detail::adapt_completion_condition_result(
124 completion_condition(ec, total_transferred));
125 std::size_t bytes_available = read_size_helper(b, max_size);
126 while (bytes_available > 0)
127 {
128 std::size_t bytes_transferred = d.read_some_at(
129 offset + total_transferred, b.prepare(bytes_available), ec);
130 b.commit(bytes_transferred);
131 total_transferred += bytes_transferred;
132 max_size = detail::adapt_completion_condition_result(
133 completion_condition(ec, total_transferred));
134 bytes_available = read_size_helper(b, max_size);
135 }
136 return total_transferred;
137 }
138
139 template <typename SyncRandomAccessReadDevice, typename Allocator>
read_at(SyncRandomAccessReadDevice & d,uint64_t offset,boost::asio::basic_streambuf<Allocator> & b)140 inline std::size_t read_at(SyncRandomAccessReadDevice& d,
141 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b)
142 {
143 boost::system::error_code ec;
144 std::size_t bytes_transferred = read_at(
145 d, offset, b, transfer_all(), ec);
146 boost::asio::detail::throw_error(ec, "read_at");
147 return bytes_transferred;
148 }
149
150 template <typename SyncRandomAccessReadDevice, typename Allocator>
read_at(SyncRandomAccessReadDevice & d,uint64_t offset,boost::asio::basic_streambuf<Allocator> & b,boost::system::error_code & ec)151 inline std::size_t read_at(SyncRandomAccessReadDevice& d,
152 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
153 boost::system::error_code& ec)
154 {
155 return read_at(d, offset, b, transfer_all(), ec);
156 }
157
158 template <typename SyncRandomAccessReadDevice, typename Allocator,
159 typename CompletionCondition>
read_at(SyncRandomAccessReadDevice & d,uint64_t offset,boost::asio::basic_streambuf<Allocator> & b,CompletionCondition completion_condition)160 inline std::size_t read_at(SyncRandomAccessReadDevice& d,
161 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
162 CompletionCondition completion_condition)
163 {
164 boost::system::error_code ec;
165 std::size_t bytes_transferred = read_at(d, offset, b,
166 BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
167 boost::asio::detail::throw_error(ec, "read_at");
168 return bytes_transferred;
169 }
170
171 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
172 #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
173
174 namespace detail
175 {
176 template <typename AsyncRandomAccessReadDevice,
177 typename MutableBufferSequence, typename MutableBufferIterator,
178 typename CompletionCondition, typename ReadHandler>
179 class read_at_op
180 : detail::base_from_completion_cond<CompletionCondition>
181 {
182 public:
read_at_op(AsyncRandomAccessReadDevice & device,uint64_t offset,const MutableBufferSequence & buffers,CompletionCondition & completion_condition,ReadHandler & handler)183 read_at_op(AsyncRandomAccessReadDevice& device,
184 uint64_t offset, const MutableBufferSequence& buffers,
185 CompletionCondition& completion_condition, ReadHandler& handler)
186 : detail::base_from_completion_cond<
187 CompletionCondition>(completion_condition),
188 device_(device),
189 offset_(offset),
190 buffers_(buffers),
191 start_(0),
192 handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
193 {
194 }
195
196 #if defined(BOOST_ASIO_HAS_MOVE)
read_at_op(const read_at_op & other)197 read_at_op(const read_at_op& other)
198 : detail::base_from_completion_cond<CompletionCondition>(other),
199 device_(other.device_),
200 offset_(other.offset_),
201 buffers_(other.buffers_),
202 start_(other.start_),
203 handler_(other.handler_)
204 {
205 }
206
read_at_op(read_at_op && other)207 read_at_op(read_at_op&& other)
208 : detail::base_from_completion_cond<CompletionCondition>(
209 BOOST_ASIO_MOVE_CAST(detail::base_from_completion_cond<
210 CompletionCondition>)(other)),
211 device_(other.device_),
212 offset_(other.offset_),
213 buffers_(BOOST_ASIO_MOVE_CAST(buffers_type)(other.buffers_)),
214 start_(other.start_),
215 handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
216 {
217 }
218 #endif // defined(BOOST_ASIO_HAS_MOVE)
219
operator ()(const boost::system::error_code & ec,std::size_t bytes_transferred,int start=0)220 void operator()(const boost::system::error_code& ec,
221 std::size_t bytes_transferred, int start = 0)
222 {
223 std::size_t max_size;
224 switch (start_ = start)
225 {
226 case 1:
227 max_size = this->check_for_completion(ec, buffers_.total_consumed());
228 do
229 {
230 {
231 BOOST_ASIO_HANDLER_LOCATION((__FILE__, __LINE__, "async_read_at"));
232 device_.async_read_some_at(
233 offset_ + buffers_.total_consumed(), buffers_.prepare(max_size),
234 BOOST_ASIO_MOVE_CAST(read_at_op)(*this));
235 }
236 return; default:
237 buffers_.consume(bytes_transferred);
238 if ((!ec && bytes_transferred == 0) || buffers_.empty())
239 break;
240 max_size = this->check_for_completion(ec, buffers_.total_consumed());
241 } while (max_size > 0);
242
243 handler_(ec, buffers_.total_consumed());
244 }
245 }
246
247 //private:
248 typedef boost::asio::detail::consuming_buffers<mutable_buffer,
249 MutableBufferSequence, MutableBufferIterator> buffers_type;
250
251 AsyncRandomAccessReadDevice& device_;
252 uint64_t offset_;
253 buffers_type buffers_;
254 int start_;
255 ReadHandler handler_;
256 };
257
258 template <typename AsyncRandomAccessReadDevice,
259 typename MutableBufferSequence, typename MutableBufferIterator,
260 typename CompletionCondition, typename ReadHandler>
261 inline asio_handler_allocate_is_deprecated
asio_handler_allocate(std::size_t size,read_at_op<AsyncRandomAccessReadDevice,MutableBufferSequence,MutableBufferIterator,CompletionCondition,ReadHandler> * this_handler)262 asio_handler_allocate(std::size_t size,
263 read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
264 MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
265 {
266 #if defined(BOOST_ASIO_NO_DEPRECATED)
267 boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
268 return asio_handler_allocate_is_no_longer_used();
269 #else // defined(BOOST_ASIO_NO_DEPRECATED)
270 return boost_asio_handler_alloc_helpers::allocate(
271 size, this_handler->handler_);
272 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
273 }
274
275 template <typename AsyncRandomAccessReadDevice,
276 typename MutableBufferSequence, typename MutableBufferIterator,
277 typename CompletionCondition, typename ReadHandler>
278 inline asio_handler_deallocate_is_deprecated
asio_handler_deallocate(void * pointer,std::size_t size,read_at_op<AsyncRandomAccessReadDevice,MutableBufferSequence,MutableBufferIterator,CompletionCondition,ReadHandler> * this_handler)279 asio_handler_deallocate(void* pointer, std::size_t size,
280 read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
281 MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
282 {
283 boost_asio_handler_alloc_helpers::deallocate(
284 pointer, size, this_handler->handler_);
285 #if defined(BOOST_ASIO_NO_DEPRECATED)
286 return asio_handler_deallocate_is_no_longer_used();
287 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
288 }
289
290 template <typename AsyncRandomAccessReadDevice,
291 typename MutableBufferSequence, typename MutableBufferIterator,
292 typename CompletionCondition, typename ReadHandler>
asio_handler_is_continuation(read_at_op<AsyncRandomAccessReadDevice,MutableBufferSequence,MutableBufferIterator,CompletionCondition,ReadHandler> * this_handler)293 inline bool asio_handler_is_continuation(
294 read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
295 MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
296 {
297 return this_handler->start_ == 0 ? true
298 : boost_asio_handler_cont_helpers::is_continuation(
299 this_handler->handler_);
300 }
301
302 template <typename Function, typename AsyncRandomAccessReadDevice,
303 typename MutableBufferSequence, typename MutableBufferIterator,
304 typename CompletionCondition, typename ReadHandler>
305 inline asio_handler_invoke_is_deprecated
asio_handler_invoke(Function & function,read_at_op<AsyncRandomAccessReadDevice,MutableBufferSequence,MutableBufferIterator,CompletionCondition,ReadHandler> * this_handler)306 asio_handler_invoke(Function& function,
307 read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
308 MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
309 {
310 boost_asio_handler_invoke_helpers::invoke(
311 function, this_handler->handler_);
312 #if defined(BOOST_ASIO_NO_DEPRECATED)
313 return asio_handler_invoke_is_no_longer_used();
314 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
315 }
316
317 template <typename Function, typename AsyncRandomAccessReadDevice,
318 typename MutableBufferSequence, typename MutableBufferIterator,
319 typename CompletionCondition, typename ReadHandler>
320 inline asio_handler_invoke_is_deprecated
asio_handler_invoke(const Function & function,read_at_op<AsyncRandomAccessReadDevice,MutableBufferSequence,MutableBufferIterator,CompletionCondition,ReadHandler> * this_handler)321 asio_handler_invoke(const Function& function,
322 read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
323 MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
324 {
325 boost_asio_handler_invoke_helpers::invoke(
326 function, this_handler->handler_);
327 #if defined(BOOST_ASIO_NO_DEPRECATED)
328 return asio_handler_invoke_is_no_longer_used();
329 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
330 }
331
332 template <typename AsyncRandomAccessReadDevice,
333 typename MutableBufferSequence, typename MutableBufferIterator,
334 typename CompletionCondition, typename ReadHandler>
start_read_at_buffer_sequence_op(AsyncRandomAccessReadDevice & d,uint64_t offset,const MutableBufferSequence & buffers,const MutableBufferIterator &,CompletionCondition & completion_condition,ReadHandler & handler)335 inline void start_read_at_buffer_sequence_op(AsyncRandomAccessReadDevice& d,
336 uint64_t offset, const MutableBufferSequence& buffers,
337 const MutableBufferIterator&, CompletionCondition& completion_condition,
338 ReadHandler& handler)
339 {
340 detail::read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
341 MutableBufferIterator, CompletionCondition, ReadHandler>(
342 d, offset, buffers, completion_condition, handler)(
343 boost::system::error_code(), 0, 1);
344 }
345
346 template <typename AsyncRandomAccessReadDevice>
347 class initiate_async_read_at_buffer_sequence
348 {
349 public:
350 typedef typename AsyncRandomAccessReadDevice::executor_type executor_type;
351
initiate_async_read_at_buffer_sequence(AsyncRandomAccessReadDevice & device)352 explicit initiate_async_read_at_buffer_sequence(
353 AsyncRandomAccessReadDevice& device)
354 : device_(device)
355 {
356 }
357
get_executor() const358 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
359 {
360 return device_.get_executor();
361 }
362
363 template <typename ReadHandler, typename MutableBufferSequence,
364 typename CompletionCondition>
operator ()(BOOST_ASIO_MOVE_ARG (ReadHandler)handler,uint64_t offset,const MutableBufferSequence & buffers,BOOST_ASIO_MOVE_ARG (CompletionCondition)completion_cond) const365 void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
366 uint64_t offset, const MutableBufferSequence& buffers,
367 BOOST_ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
368 {
369 // If you get an error on the following line it means that your handler
370 // does not meet the documented type requirements for a ReadHandler.
371 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
372
373 non_const_lvalue<ReadHandler> handler2(handler);
374 non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
375 start_read_at_buffer_sequence_op(device_, offset, buffers,
376 boost::asio::buffer_sequence_begin(buffers),
377 completion_cond2.value, handler2.value);
378 }
379
380 private:
381 AsyncRandomAccessReadDevice& device_;
382 };
383 } // namespace detail
384
385 #if !defined(GENERATING_DOCUMENTATION)
386
387 template <typename AsyncRandomAccessReadDevice,
388 typename MutableBufferSequence, typename MutableBufferIterator,
389 typename CompletionCondition, typename ReadHandler, typename Allocator>
390 struct associated_allocator<
391 detail::read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
392 MutableBufferIterator, CompletionCondition, ReadHandler>,
393 Allocator>
394 {
395 typedef typename associated_allocator<ReadHandler, Allocator>::type type;
396
getboost::asio::associated_allocator397 static type get(
398 const detail::read_at_op<AsyncRandomAccessReadDevice,
399 MutableBufferSequence, MutableBufferIterator,
400 CompletionCondition, ReadHandler>& h,
401 const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
402 {
403 return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
404 }
405 };
406
407 template <typename AsyncRandomAccessReadDevice,
408 typename MutableBufferSequence, typename MutableBufferIterator,
409 typename CompletionCondition, typename ReadHandler, typename Executor>
410 struct associated_executor<
411 detail::read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
412 MutableBufferIterator, CompletionCondition, ReadHandler>,
413 Executor>
414 : detail::associated_executor_forwarding_base<ReadHandler, Executor>
415 {
416 typedef typename associated_executor<ReadHandler, Executor>::type type;
417
getboost::asio::associated_executor418 static type get(
419 const detail::read_at_op<AsyncRandomAccessReadDevice,
420 MutableBufferSequence, MutableBufferIterator,
421 CompletionCondition, ReadHandler>& h,
422 const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
423 {
424 return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
425 }
426 };
427
428 #endif // !defined(GENERATING_DOCUMENTATION)
429
430 template <typename AsyncRandomAccessReadDevice,
431 typename MutableBufferSequence, typename CompletionCondition,
432 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
433 std::size_t)) ReadHandler>
BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,void (boost::system::error_code,std::size_t))434 inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
435 void (boost::system::error_code, std::size_t))
436 async_read_at(AsyncRandomAccessReadDevice& d,
437 uint64_t offset, const MutableBufferSequence& buffers,
438 CompletionCondition completion_condition,
439 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
440 {
441 return async_initiate<ReadHandler,
442 void (boost::system::error_code, std::size_t)>(
443 detail::initiate_async_read_at_buffer_sequence<
444 AsyncRandomAccessReadDevice>(d),
445 handler, offset, buffers,
446 BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
447 }
448
449 template <typename AsyncRandomAccessReadDevice, typename MutableBufferSequence,
450 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
451 std::size_t)) ReadHandler>
BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,void (boost::system::error_code,std::size_t))452 inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
453 void (boost::system::error_code, std::size_t))
454 async_read_at(AsyncRandomAccessReadDevice& d,
455 uint64_t offset, const MutableBufferSequence& buffers,
456 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
457 {
458 return async_initiate<ReadHandler,
459 void (boost::system::error_code, std::size_t)>(
460 detail::initiate_async_read_at_buffer_sequence<
461 AsyncRandomAccessReadDevice>(d),
462 handler, offset, buffers, transfer_all());
463 }
464
465 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
466 #if !defined(BOOST_ASIO_NO_IOSTREAM)
467
468 namespace detail
469 {
470 template <typename AsyncRandomAccessReadDevice, typename Allocator,
471 typename CompletionCondition, typename ReadHandler>
472 class read_at_streambuf_op
473 : detail::base_from_completion_cond<CompletionCondition>
474 {
475 public:
read_at_streambuf_op(AsyncRandomAccessReadDevice & device,uint64_t offset,basic_streambuf<Allocator> & streambuf,CompletionCondition & completion_condition,ReadHandler & handler)476 read_at_streambuf_op(AsyncRandomAccessReadDevice& device,
477 uint64_t offset, basic_streambuf<Allocator>& streambuf,
478 CompletionCondition& completion_condition, ReadHandler& handler)
479 : detail::base_from_completion_cond<
480 CompletionCondition>(completion_condition),
481 device_(device),
482 offset_(offset),
483 streambuf_(streambuf),
484 start_(0),
485 total_transferred_(0),
486 handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
487 {
488 }
489
490 #if defined(BOOST_ASIO_HAS_MOVE)
read_at_streambuf_op(const read_at_streambuf_op & other)491 read_at_streambuf_op(const read_at_streambuf_op& other)
492 : detail::base_from_completion_cond<CompletionCondition>(other),
493 device_(other.device_),
494 offset_(other.offset_),
495 streambuf_(other.streambuf_),
496 start_(other.start_),
497 total_transferred_(other.total_transferred_),
498 handler_(other.handler_)
499 {
500 }
501
read_at_streambuf_op(read_at_streambuf_op && other)502 read_at_streambuf_op(read_at_streambuf_op&& other)
503 : detail::base_from_completion_cond<CompletionCondition>(
504 BOOST_ASIO_MOVE_CAST(detail::base_from_completion_cond<
505 CompletionCondition>)(other)),
506 device_(other.device_),
507 offset_(other.offset_),
508 streambuf_(other.streambuf_),
509 start_(other.start_),
510 total_transferred_(other.total_transferred_),
511 handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
512 {
513 }
514 #endif // defined(BOOST_ASIO_HAS_MOVE)
515
operator ()(const boost::system::error_code & ec,std::size_t bytes_transferred,int start=0)516 void operator()(const boost::system::error_code& ec,
517 std::size_t bytes_transferred, int start = 0)
518 {
519 std::size_t max_size, bytes_available;
520 switch (start_ = start)
521 {
522 case 1:
523 max_size = this->check_for_completion(ec, total_transferred_);
524 bytes_available = read_size_helper(streambuf_, max_size);
525 for (;;)
526 {
527 {
528 BOOST_ASIO_HANDLER_LOCATION((__FILE__, __LINE__, "async_read_at"));
529 device_.async_read_some_at(offset_ + total_transferred_,
530 streambuf_.prepare(bytes_available),
531 BOOST_ASIO_MOVE_CAST(read_at_streambuf_op)(*this));
532 }
533 return; default:
534 total_transferred_ += bytes_transferred;
535 streambuf_.commit(bytes_transferred);
536 max_size = this->check_for_completion(ec, total_transferred_);
537 bytes_available = read_size_helper(streambuf_, max_size);
538 if ((!ec && bytes_transferred == 0) || bytes_available == 0)
539 break;
540 }
541
542 handler_(ec, static_cast<const std::size_t&>(total_transferred_));
543 }
544 }
545
546 //private:
547 AsyncRandomAccessReadDevice& device_;
548 uint64_t offset_;
549 boost::asio::basic_streambuf<Allocator>& streambuf_;
550 int start_;
551 std::size_t total_transferred_;
552 ReadHandler handler_;
553 };
554
555 template <typename AsyncRandomAccessReadDevice, typename Allocator,
556 typename CompletionCondition, typename ReadHandler>
557 inline asio_handler_allocate_is_deprecated
asio_handler_allocate(std::size_t size,read_at_streambuf_op<AsyncRandomAccessReadDevice,Allocator,CompletionCondition,ReadHandler> * this_handler)558 asio_handler_allocate(std::size_t size,
559 read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
560 CompletionCondition, ReadHandler>* this_handler)
561 {
562 #if defined(BOOST_ASIO_NO_DEPRECATED)
563 boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
564 return asio_handler_allocate_is_no_longer_used();
565 #else // defined(BOOST_ASIO_NO_DEPRECATED)
566 return boost_asio_handler_alloc_helpers::allocate(
567 size, this_handler->handler_);
568 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
569 }
570
571 template <typename AsyncRandomAccessReadDevice, typename Allocator,
572 typename CompletionCondition, typename ReadHandler>
573 inline asio_handler_deallocate_is_deprecated
asio_handler_deallocate(void * pointer,std::size_t size,read_at_streambuf_op<AsyncRandomAccessReadDevice,Allocator,CompletionCondition,ReadHandler> * this_handler)574 asio_handler_deallocate(void* pointer, std::size_t size,
575 read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
576 CompletionCondition, ReadHandler>* this_handler)
577 {
578 boost_asio_handler_alloc_helpers::deallocate(
579 pointer, size, this_handler->handler_);
580 #if defined(BOOST_ASIO_NO_DEPRECATED)
581 return asio_handler_deallocate_is_no_longer_used();
582 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
583 }
584
585 template <typename AsyncRandomAccessReadDevice, typename Allocator,
586 typename CompletionCondition, typename ReadHandler>
asio_handler_is_continuation(read_at_streambuf_op<AsyncRandomAccessReadDevice,Allocator,CompletionCondition,ReadHandler> * this_handler)587 inline bool asio_handler_is_continuation(
588 read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
589 CompletionCondition, ReadHandler>* this_handler)
590 {
591 return this_handler->start_ == 0 ? true
592 : boost_asio_handler_cont_helpers::is_continuation(
593 this_handler->handler_);
594 }
595
596 template <typename Function, typename AsyncRandomAccessReadDevice,
597 typename Allocator, typename CompletionCondition, typename ReadHandler>
598 inline asio_handler_invoke_is_deprecated
asio_handler_invoke(Function & function,read_at_streambuf_op<AsyncRandomAccessReadDevice,Allocator,CompletionCondition,ReadHandler> * this_handler)599 asio_handler_invoke(Function& function,
600 read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
601 CompletionCondition, ReadHandler>* this_handler)
602 {
603 boost_asio_handler_invoke_helpers::invoke(
604 function, this_handler->handler_);
605 #if defined(BOOST_ASIO_NO_DEPRECATED)
606 return asio_handler_invoke_is_no_longer_used();
607 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
608 }
609
610 template <typename Function, typename AsyncRandomAccessReadDevice,
611 typename Allocator, typename CompletionCondition, typename ReadHandler>
612 inline asio_handler_invoke_is_deprecated
asio_handler_invoke(const Function & function,read_at_streambuf_op<AsyncRandomAccessReadDevice,Allocator,CompletionCondition,ReadHandler> * this_handler)613 asio_handler_invoke(const Function& function,
614 read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
615 CompletionCondition, ReadHandler>* this_handler)
616 {
617 boost_asio_handler_invoke_helpers::invoke(
618 function, this_handler->handler_);
619 #if defined(BOOST_ASIO_NO_DEPRECATED)
620 return asio_handler_invoke_is_no_longer_used();
621 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
622 }
623
624 template <typename AsyncRandomAccessReadDevice>
625 class initiate_async_read_at_streambuf
626 {
627 public:
628 typedef typename AsyncRandomAccessReadDevice::executor_type executor_type;
629
initiate_async_read_at_streambuf(AsyncRandomAccessReadDevice & device)630 explicit initiate_async_read_at_streambuf(
631 AsyncRandomAccessReadDevice& device)
632 : device_(device)
633 {
634 }
635
get_executor() const636 executor_type get_executor() const BOOST_ASIO_NOEXCEPT
637 {
638 return device_.get_executor();
639 }
640
641 template <typename ReadHandler,
642 typename Allocator, typename CompletionCondition>
operator ()(BOOST_ASIO_MOVE_ARG (ReadHandler)handler,uint64_t offset,basic_streambuf<Allocator> * b,BOOST_ASIO_MOVE_ARG (CompletionCondition)completion_cond) const643 void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
644 uint64_t offset, basic_streambuf<Allocator>* b,
645 BOOST_ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
646 {
647 // If you get an error on the following line it means that your handler
648 // does not meet the documented type requirements for a ReadHandler.
649 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
650
651 non_const_lvalue<ReadHandler> handler2(handler);
652 non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
653 read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
654 CompletionCondition, typename decay<ReadHandler>::type>(
655 device_, offset, *b, completion_cond2.value, handler2.value)(
656 boost::system::error_code(), 0, 1);
657 }
658
659 private:
660 AsyncRandomAccessReadDevice& device_;
661 };
662 } // namespace detail
663
664 #if !defined(GENERATING_DOCUMENTATION)
665
666 template <typename AsyncRandomAccessReadDevice, typename Allocator,
667 typename CompletionCondition, typename ReadHandler, typename Allocator1>
668 struct associated_allocator<
669 detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
670 Allocator, CompletionCondition, ReadHandler>,
671 Allocator1>
672 {
673 typedef typename associated_allocator<ReadHandler, Allocator1>::type type;
674
getboost::asio::associated_allocator675 static type get(
676 const detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
677 Allocator, CompletionCondition, ReadHandler>& h,
678 const Allocator1& a = Allocator1()) BOOST_ASIO_NOEXCEPT
679 {
680 return associated_allocator<ReadHandler, Allocator1>::get(h.handler_, a);
681 }
682 };
683
684 template <typename AsyncRandomAccessReadDevice, typename Executor,
685 typename CompletionCondition, typename ReadHandler, typename Executor1>
686 struct associated_executor<
687 detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
688 Executor, CompletionCondition, ReadHandler>,
689 Executor1>
690 : detail::associated_executor_forwarding_base<ReadHandler, Executor>
691 {
692 typedef typename associated_executor<ReadHandler, Executor1>::type type;
693
getboost::asio::associated_executor694 static type get(
695 const detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
696 Executor, CompletionCondition, ReadHandler>& h,
697 const Executor1& ex = Executor1()) BOOST_ASIO_NOEXCEPT
698 {
699 return associated_executor<ReadHandler, Executor1>::get(h.handler_, ex);
700 }
701 };
702
703 #endif // !defined(GENERATING_DOCUMENTATION)
704
705 template <typename AsyncRandomAccessReadDevice,
706 typename Allocator, typename CompletionCondition,
707 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
708 std::size_t)) ReadHandler>
BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,void (boost::system::error_code,std::size_t))709 inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
710 void (boost::system::error_code, std::size_t))
711 async_read_at(AsyncRandomAccessReadDevice& d,
712 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
713 CompletionCondition completion_condition,
714 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
715 {
716 return async_initiate<ReadHandler,
717 void (boost::system::error_code, std::size_t)>(
718 detail::initiate_async_read_at_streambuf<AsyncRandomAccessReadDevice>(d),
719 handler, offset, &b,
720 BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
721 }
722
723 template <typename AsyncRandomAccessReadDevice, typename Allocator,
724 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
725 std::size_t)) ReadHandler>
BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,void (boost::system::error_code,std::size_t))726 inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
727 void (boost::system::error_code, std::size_t))
728 async_read_at(AsyncRandomAccessReadDevice& d,
729 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
730 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
731 {
732 return async_initiate<ReadHandler,
733 void (boost::system::error_code, std::size_t)>(
734 detail::initiate_async_read_at_streambuf<AsyncRandomAccessReadDevice>(d),
735 handler, offset, &b, transfer_all());
736 }
737
738 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
739 #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
740
741 } // namespace asio
742 } // namespace boost
743
744 #include <boost/asio/detail/pop_options.hpp>
745
746 #endif // BOOST_ASIO_IMPL_READ_AT_HPP
747