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