annotate DEPENDENCIES/generic/include/boost/asio/impl/buffered_read_stream.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 //
Chris@16 2 // impl/buffered_read_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_IMPL_BUFFERED_READ_STREAM_HPP
Chris@16 12 #define BOOST_ASIO_IMPL_BUFFERED_READ_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/handler_alloc_helpers.hpp>
Chris@16 19 #include <boost/asio/detail/handler_cont_helpers.hpp>
Chris@16 20 #include <boost/asio/detail/handler_invoke_helpers.hpp>
Chris@16 21 #include <boost/asio/detail/handler_type_requirements.hpp>
Chris@16 22
Chris@16 23 #include <boost/asio/detail/push_options.hpp>
Chris@16 24
Chris@16 25 namespace boost {
Chris@16 26 namespace asio {
Chris@16 27
Chris@16 28 template <typename Stream>
Chris@16 29 std::size_t buffered_read_stream<Stream>::fill()
Chris@16 30 {
Chris@16 31 detail::buffer_resize_guard<detail::buffered_stream_storage>
Chris@16 32 resize_guard(storage_);
Chris@16 33 std::size_t previous_size = storage_.size();
Chris@16 34 storage_.resize(storage_.capacity());
Chris@16 35 storage_.resize(previous_size + next_layer_.read_some(buffer(
Chris@16 36 storage_.data() + previous_size,
Chris@16 37 storage_.size() - previous_size)));
Chris@16 38 resize_guard.commit();
Chris@16 39 return storage_.size() - previous_size;
Chris@16 40 }
Chris@16 41
Chris@16 42 template <typename Stream>
Chris@16 43 std::size_t buffered_read_stream<Stream>::fill(boost::system::error_code& ec)
Chris@16 44 {
Chris@16 45 detail::buffer_resize_guard<detail::buffered_stream_storage>
Chris@16 46 resize_guard(storage_);
Chris@16 47 std::size_t previous_size = storage_.size();
Chris@16 48 storage_.resize(storage_.capacity());
Chris@16 49 storage_.resize(previous_size + next_layer_.read_some(buffer(
Chris@16 50 storage_.data() + previous_size,
Chris@16 51 storage_.size() - previous_size),
Chris@16 52 ec));
Chris@16 53 resize_guard.commit();
Chris@16 54 return storage_.size() - previous_size;
Chris@16 55 }
Chris@16 56
Chris@16 57 namespace detail
Chris@16 58 {
Chris@16 59 template <typename ReadHandler>
Chris@16 60 class buffered_fill_handler
Chris@16 61 {
Chris@16 62 public:
Chris@16 63 buffered_fill_handler(detail::buffered_stream_storage& storage,
Chris@16 64 std::size_t previous_size, ReadHandler& handler)
Chris@16 65 : storage_(storage),
Chris@16 66 previous_size_(previous_size),
Chris@16 67 handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
Chris@16 68 {
Chris@16 69 }
Chris@16 70
Chris@16 71 #if defined(BOOST_ASIO_HAS_MOVE)
Chris@16 72 buffered_fill_handler(const buffered_fill_handler& other)
Chris@16 73 : storage_(other.storage_),
Chris@16 74 previous_size_(other.previous_size_),
Chris@16 75 handler_(other.handler_)
Chris@16 76 {
Chris@16 77 }
Chris@16 78
Chris@16 79 buffered_fill_handler(buffered_fill_handler&& other)
Chris@16 80 : storage_(other.storage_),
Chris@16 81 previous_size_(other.previous_size_),
Chris@16 82 handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
Chris@16 83 {
Chris@16 84 }
Chris@16 85 #endif // defined(BOOST_ASIO_HAS_MOVE)
Chris@16 86
Chris@16 87 void operator()(const boost::system::error_code& ec,
Chris@16 88 const std::size_t bytes_transferred)
Chris@16 89 {
Chris@16 90 storage_.resize(previous_size_ + bytes_transferred);
Chris@16 91 handler_(ec, bytes_transferred);
Chris@16 92 }
Chris@16 93
Chris@16 94 //private:
Chris@16 95 detail::buffered_stream_storage& storage_;
Chris@16 96 std::size_t previous_size_;
Chris@16 97 ReadHandler handler_;
Chris@16 98 };
Chris@16 99
Chris@16 100 template <typename ReadHandler>
Chris@16 101 inline void* asio_handler_allocate(std::size_t size,
Chris@16 102 buffered_fill_handler<ReadHandler>* this_handler)
Chris@16 103 {
Chris@16 104 return boost_asio_handler_alloc_helpers::allocate(
Chris@16 105 size, this_handler->handler_);
Chris@16 106 }
Chris@16 107
Chris@16 108 template <typename ReadHandler>
Chris@16 109 inline void asio_handler_deallocate(void* pointer, std::size_t size,
Chris@16 110 buffered_fill_handler<ReadHandler>* this_handler)
Chris@16 111 {
Chris@16 112 boost_asio_handler_alloc_helpers::deallocate(
Chris@16 113 pointer, size, this_handler->handler_);
Chris@16 114 }
Chris@16 115
Chris@16 116 template <typename ReadHandler>
Chris@16 117 inline bool asio_handler_is_continuation(
Chris@16 118 buffered_fill_handler<ReadHandler>* this_handler)
Chris@16 119 {
Chris@16 120 return boost_asio_handler_cont_helpers::is_continuation(
Chris@16 121 this_handler->handler_);
Chris@16 122 }
Chris@16 123
Chris@16 124 template <typename Function, typename ReadHandler>
Chris@16 125 inline void asio_handler_invoke(Function& function,
Chris@16 126 buffered_fill_handler<ReadHandler>* this_handler)
Chris@16 127 {
Chris@16 128 boost_asio_handler_invoke_helpers::invoke(
Chris@16 129 function, this_handler->handler_);
Chris@16 130 }
Chris@16 131
Chris@16 132 template <typename Function, typename ReadHandler>
Chris@16 133 inline void asio_handler_invoke(const Function& function,
Chris@16 134 buffered_fill_handler<ReadHandler>* this_handler)
Chris@16 135 {
Chris@16 136 boost_asio_handler_invoke_helpers::invoke(
Chris@16 137 function, this_handler->handler_);
Chris@16 138 }
Chris@16 139 } // namespace detail
Chris@16 140
Chris@16 141 template <typename Stream>
Chris@16 142 template <typename ReadHandler>
Chris@16 143 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
Chris@16 144 void (boost::system::error_code, std::size_t))
Chris@16 145 buffered_read_stream<Stream>::async_fill(
Chris@16 146 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
Chris@16 147 {
Chris@16 148 // If you get an error on the following line it means that your handler does
Chris@16 149 // not meet the documented type requirements for a ReadHandler.
Chris@16 150 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
Chris@16 151
Chris@16 152 detail::async_result_init<
Chris@16 153 ReadHandler, void (boost::system::error_code, std::size_t)> init(
Chris@16 154 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
Chris@16 155
Chris@16 156 std::size_t previous_size = storage_.size();
Chris@16 157 storage_.resize(storage_.capacity());
Chris@16 158 next_layer_.async_read_some(
Chris@16 159 buffer(
Chris@16 160 storage_.data() + previous_size,
Chris@16 161 storage_.size() - previous_size),
Chris@16 162 detail::buffered_fill_handler<BOOST_ASIO_HANDLER_TYPE(
Chris@16 163 ReadHandler, void (boost::system::error_code, std::size_t))>(
Chris@16 164 storage_, previous_size, init.handler));
Chris@16 165
Chris@16 166 return init.result.get();
Chris@16 167 }
Chris@16 168
Chris@16 169 template <typename Stream>
Chris@16 170 template <typename MutableBufferSequence>
Chris@16 171 std::size_t buffered_read_stream<Stream>::read_some(
Chris@16 172 const MutableBufferSequence& buffers)
Chris@16 173 {
Chris@16 174 if (boost::asio::buffer_size(buffers) == 0)
Chris@16 175 return 0;
Chris@16 176
Chris@16 177 if (storage_.empty())
Chris@16 178 this->fill();
Chris@16 179
Chris@16 180 return this->copy(buffers);
Chris@16 181 }
Chris@16 182
Chris@16 183 template <typename Stream>
Chris@16 184 template <typename MutableBufferSequence>
Chris@16 185 std::size_t buffered_read_stream<Stream>::read_some(
Chris@16 186 const MutableBufferSequence& buffers, boost::system::error_code& ec)
Chris@16 187 {
Chris@16 188 ec = boost::system::error_code();
Chris@16 189
Chris@16 190 if (boost::asio::buffer_size(buffers) == 0)
Chris@16 191 return 0;
Chris@16 192
Chris@16 193 if (storage_.empty() && !this->fill(ec))
Chris@16 194 return 0;
Chris@16 195
Chris@16 196 return this->copy(buffers);
Chris@16 197 }
Chris@16 198
Chris@16 199 namespace detail
Chris@16 200 {
Chris@16 201 template <typename MutableBufferSequence, typename ReadHandler>
Chris@16 202 class buffered_read_some_handler
Chris@16 203 {
Chris@16 204 public:
Chris@16 205 buffered_read_some_handler(detail::buffered_stream_storage& storage,
Chris@16 206 const MutableBufferSequence& buffers, ReadHandler& handler)
Chris@16 207 : storage_(storage),
Chris@16 208 buffers_(buffers),
Chris@16 209 handler_(handler)
Chris@16 210 {
Chris@16 211 }
Chris@16 212
Chris@16 213 #if defined(BOOST_ASIO_HAS_MOVE)
Chris@16 214 buffered_read_some_handler(const buffered_read_some_handler& other)
Chris@16 215 : storage_(other.storage_),
Chris@16 216 buffers_(other.buffers_),
Chris@16 217 handler_(other.handler_)
Chris@16 218 {
Chris@16 219 }
Chris@16 220
Chris@16 221 buffered_read_some_handler(buffered_read_some_handler&& other)
Chris@16 222 : storage_(other.storage_),
Chris@16 223 buffers_(other.buffers_),
Chris@16 224 handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
Chris@16 225 {
Chris@16 226 }
Chris@16 227 #endif // defined(BOOST_ASIO_HAS_MOVE)
Chris@16 228
Chris@16 229 void operator()(const boost::system::error_code& ec, std::size_t)
Chris@16 230 {
Chris@16 231 if (ec || storage_.empty())
Chris@16 232 {
Chris@16 233 const std::size_t length = 0;
Chris@16 234 handler_(ec, length);
Chris@16 235 }
Chris@16 236 else
Chris@16 237 {
Chris@16 238 const std::size_t bytes_copied = boost::asio::buffer_copy(
Chris@16 239 buffers_, storage_.data(), storage_.size());
Chris@16 240 storage_.consume(bytes_copied);
Chris@16 241 handler_(ec, bytes_copied);
Chris@16 242 }
Chris@16 243 }
Chris@16 244
Chris@16 245 //private:
Chris@16 246 detail::buffered_stream_storage& storage_;
Chris@16 247 MutableBufferSequence buffers_;
Chris@16 248 ReadHandler handler_;
Chris@16 249 };
Chris@16 250
Chris@16 251 template <typename MutableBufferSequence, typename ReadHandler>
Chris@16 252 inline void* asio_handler_allocate(std::size_t size,
Chris@16 253 buffered_read_some_handler<
Chris@16 254 MutableBufferSequence, ReadHandler>* this_handler)
Chris@16 255 {
Chris@16 256 return boost_asio_handler_alloc_helpers::allocate(
Chris@16 257 size, this_handler->handler_);
Chris@16 258 }
Chris@16 259
Chris@16 260 template <typename MutableBufferSequence, typename ReadHandler>
Chris@16 261 inline void asio_handler_deallocate(void* pointer, std::size_t size,
Chris@16 262 buffered_read_some_handler<
Chris@16 263 MutableBufferSequence, ReadHandler>* this_handler)
Chris@16 264 {
Chris@16 265 boost_asio_handler_alloc_helpers::deallocate(
Chris@16 266 pointer, size, this_handler->handler_);
Chris@16 267 }
Chris@16 268
Chris@16 269 template <typename MutableBufferSequence, typename ReadHandler>
Chris@16 270 inline bool asio_handler_is_continuation(
Chris@16 271 buffered_read_some_handler<
Chris@16 272 MutableBufferSequence, ReadHandler>* this_handler)
Chris@16 273 {
Chris@16 274 return boost_asio_handler_cont_helpers::is_continuation(
Chris@16 275 this_handler->handler_);
Chris@16 276 }
Chris@16 277
Chris@16 278 template <typename Function, typename MutableBufferSequence,
Chris@16 279 typename ReadHandler>
Chris@16 280 inline void asio_handler_invoke(Function& function,
Chris@16 281 buffered_read_some_handler<
Chris@16 282 MutableBufferSequence, ReadHandler>* this_handler)
Chris@16 283 {
Chris@16 284 boost_asio_handler_invoke_helpers::invoke(
Chris@16 285 function, this_handler->handler_);
Chris@16 286 }
Chris@16 287
Chris@16 288 template <typename Function, typename MutableBufferSequence,
Chris@16 289 typename ReadHandler>
Chris@16 290 inline void asio_handler_invoke(const Function& function,
Chris@16 291 buffered_read_some_handler<
Chris@16 292 MutableBufferSequence, ReadHandler>* this_handler)
Chris@16 293 {
Chris@16 294 boost_asio_handler_invoke_helpers::invoke(
Chris@16 295 function, this_handler->handler_);
Chris@16 296 }
Chris@16 297 } // namespace detail
Chris@16 298
Chris@16 299 template <typename Stream>
Chris@16 300 template <typename MutableBufferSequence, typename ReadHandler>
Chris@16 301 BOOST_ASIO_INITFN_RESULT_TYPE(ReadHandler,
Chris@16 302 void (boost::system::error_code, std::size_t))
Chris@16 303 buffered_read_stream<Stream>::async_read_some(
Chris@16 304 const MutableBufferSequence& buffers,
Chris@16 305 BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
Chris@16 306 {
Chris@16 307 // If you get an error on the following line it means that your handler does
Chris@16 308 // not meet the documented type requirements for a ReadHandler.
Chris@16 309 BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
Chris@16 310
Chris@16 311 detail::async_result_init<
Chris@16 312 ReadHandler, void (boost::system::error_code, std::size_t)> init(
Chris@16 313 BOOST_ASIO_MOVE_CAST(ReadHandler)(handler));
Chris@16 314
Chris@16 315 if (boost::asio::buffer_size(buffers) == 0 || !storage_.empty())
Chris@16 316 {
Chris@16 317 next_layer_.async_read_some(boost::asio::mutable_buffers_1(0, 0),
Chris@16 318 detail::buffered_read_some_handler<
Chris@16 319 MutableBufferSequence, BOOST_ASIO_HANDLER_TYPE(
Chris@16 320 ReadHandler, void (boost::system::error_code, std::size_t))>(
Chris@16 321 storage_, buffers, init.handler));
Chris@16 322 }
Chris@16 323 else
Chris@16 324 {
Chris@16 325 this->async_fill(detail::buffered_read_some_handler<
Chris@16 326 MutableBufferSequence, BOOST_ASIO_HANDLER_TYPE(
Chris@16 327 ReadHandler, void (boost::system::error_code, std::size_t))>(
Chris@16 328 storage_, buffers, init.handler));
Chris@16 329 }
Chris@16 330
Chris@16 331 return init.result.get();
Chris@16 332 }
Chris@16 333
Chris@16 334 template <typename Stream>
Chris@16 335 template <typename MutableBufferSequence>
Chris@16 336 std::size_t buffered_read_stream<Stream>::peek(
Chris@16 337 const MutableBufferSequence& buffers)
Chris@16 338 {
Chris@16 339 if (storage_.empty())
Chris@16 340 this->fill();
Chris@16 341 return this->peek_copy(buffers);
Chris@16 342 }
Chris@16 343
Chris@16 344 template <typename Stream>
Chris@16 345 template <typename MutableBufferSequence>
Chris@16 346 std::size_t buffered_read_stream<Stream>::peek(
Chris@16 347 const MutableBufferSequence& buffers, boost::system::error_code& ec)
Chris@16 348 {
Chris@16 349 ec = boost::system::error_code();
Chris@16 350 if (storage_.empty() && !this->fill(ec))
Chris@16 351 return 0;
Chris@16 352 return this->peek_copy(buffers);
Chris@16 353 }
Chris@16 354
Chris@16 355 } // namespace asio
Chris@16 356 } // namespace boost
Chris@16 357
Chris@16 358 #include <boost/asio/detail/pop_options.hpp>
Chris@16 359
Chris@16 360 #endif // BOOST_ASIO_IMPL_BUFFERED_READ_STREAM_HPP