File indexing completed on 2025-01-18 09:28:58
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_ASIO_IMPL_WRITE_AT_HPP
0012 #define BOOST_ASIO_IMPL_WRITE_AT_HPP
0013
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif
0017
0018 #include <boost/asio/associator.hpp>
0019 #include <boost/asio/buffer.hpp>
0020 #include <boost/asio/detail/array_fwd.hpp>
0021 #include <boost/asio/detail/base_from_cancellation_state.hpp>
0022 #include <boost/asio/detail/base_from_completion_cond.hpp>
0023 #include <boost/asio/detail/bind_handler.hpp>
0024 #include <boost/asio/detail/consuming_buffers.hpp>
0025 #include <boost/asio/detail/dependent_type.hpp>
0026 #include <boost/asio/detail/handler_cont_helpers.hpp>
0027 #include <boost/asio/detail/handler_tracking.hpp>
0028 #include <boost/asio/detail/handler_type_requirements.hpp>
0029 #include <boost/asio/detail/non_const_lvalue.hpp>
0030 #include <boost/asio/detail/throw_error.hpp>
0031
0032 #include <boost/asio/detail/push_options.hpp>
0033
0034 namespace boost {
0035 namespace asio {
0036
0037 namespace detail
0038 {
0039 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
0040 typename ConstBufferIterator, typename CompletionCondition>
0041 std::size_t write_at_buffer_sequence(SyncRandomAccessWriteDevice& d,
0042 uint64_t offset, const ConstBufferSequence& buffers,
0043 const ConstBufferIterator&, CompletionCondition completion_condition,
0044 boost::system::error_code& ec)
0045 {
0046 ec = boost::system::error_code();
0047 boost::asio::detail::consuming_buffers<const_buffer,
0048 ConstBufferSequence, ConstBufferIterator> tmp(buffers);
0049 while (!tmp.empty())
0050 {
0051 if (std::size_t max_size = detail::adapt_completion_condition_result(
0052 completion_condition(ec, tmp.total_consumed())))
0053 {
0054 tmp.consume(d.write_some_at(offset + tmp.total_consumed(),
0055 tmp.prepare(max_size), ec));
0056 }
0057 else
0058 break;
0059 }
0060 return tmp.total_consumed();
0061 }
0062 }
0063
0064 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
0065 typename CompletionCondition>
0066 std::size_t write_at(SyncRandomAccessWriteDevice& d,
0067 uint64_t offset, const ConstBufferSequence& buffers,
0068 CompletionCondition completion_condition, boost::system::error_code& ec)
0069 {
0070 return detail::write_at_buffer_sequence(d, offset, buffers,
0071 boost::asio::buffer_sequence_begin(buffers),
0072 static_cast<CompletionCondition&&>(completion_condition), ec);
0073 }
0074
0075 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
0076 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0077 uint64_t offset, const ConstBufferSequence& buffers)
0078 {
0079 boost::system::error_code ec;
0080 std::size_t bytes_transferred = write_at(
0081 d, offset, buffers, transfer_all(), ec);
0082 boost::asio::detail::throw_error(ec, "write_at");
0083 return bytes_transferred;
0084 }
0085
0086 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
0087 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0088 uint64_t offset, const ConstBufferSequence& buffers,
0089 boost::system::error_code& ec)
0090 {
0091 return write_at(d, offset, buffers, transfer_all(), ec);
0092 }
0093
0094 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
0095 typename CompletionCondition>
0096 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0097 uint64_t offset, const ConstBufferSequence& buffers,
0098 CompletionCondition completion_condition)
0099 {
0100 boost::system::error_code ec;
0101 std::size_t bytes_transferred = write_at(d, offset, buffers,
0102 static_cast<CompletionCondition&&>(completion_condition), ec);
0103 boost::asio::detail::throw_error(ec, "write_at");
0104 return bytes_transferred;
0105 }
0106
0107 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
0108 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0109
0110 template <typename SyncRandomAccessWriteDevice, typename Allocator,
0111 typename CompletionCondition>
0112 std::size_t write_at(SyncRandomAccessWriteDevice& d,
0113 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
0114 CompletionCondition completion_condition, boost::system::error_code& ec)
0115 {
0116 std::size_t bytes_transferred = write_at(d, offset, b.data(),
0117 static_cast<CompletionCondition&&>(completion_condition), ec);
0118 b.consume(bytes_transferred);
0119 return bytes_transferred;
0120 }
0121
0122 template <typename SyncRandomAccessWriteDevice, typename Allocator>
0123 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0124 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b)
0125 {
0126 boost::system::error_code ec;
0127 std::size_t bytes_transferred = write_at(d, offset, b, transfer_all(), ec);
0128 boost::asio::detail::throw_error(ec, "write_at");
0129 return bytes_transferred;
0130 }
0131
0132 template <typename SyncRandomAccessWriteDevice, typename Allocator>
0133 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0134 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
0135 boost::system::error_code& ec)
0136 {
0137 return write_at(d, offset, b, transfer_all(), ec);
0138 }
0139
0140 template <typename SyncRandomAccessWriteDevice, typename Allocator,
0141 typename CompletionCondition>
0142 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0143 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
0144 CompletionCondition completion_condition)
0145 {
0146 boost::system::error_code ec;
0147 std::size_t bytes_transferred = write_at(d, offset, b,
0148 static_cast<CompletionCondition&&>(completion_condition), ec);
0149 boost::asio::detail::throw_error(ec, "write_at");
0150 return bytes_transferred;
0151 }
0152
0153 #endif
0154 #endif
0155
0156 namespace detail
0157 {
0158 template <typename AsyncRandomAccessWriteDevice,
0159 typename ConstBufferSequence, typename ConstBufferIterator,
0160 typename CompletionCondition, typename WriteHandler>
0161 class write_at_op
0162 : public base_from_cancellation_state<WriteHandler>,
0163 base_from_completion_cond<CompletionCondition>
0164 {
0165 public:
0166 write_at_op(AsyncRandomAccessWriteDevice& device,
0167 uint64_t offset, const ConstBufferSequence& buffers,
0168 CompletionCondition& completion_condition, WriteHandler& handler)
0169 : base_from_cancellation_state<WriteHandler>(
0170 handler, enable_partial_cancellation()),
0171 base_from_completion_cond<CompletionCondition>(completion_condition),
0172 device_(device),
0173 offset_(offset),
0174 buffers_(buffers),
0175 start_(0),
0176 handler_(static_cast<WriteHandler&&>(handler))
0177 {
0178 }
0179
0180 write_at_op(const write_at_op& other)
0181 : base_from_cancellation_state<WriteHandler>(other),
0182 base_from_completion_cond<CompletionCondition>(other),
0183 device_(other.device_),
0184 offset_(other.offset_),
0185 buffers_(other.buffers_),
0186 start_(other.start_),
0187 handler_(other.handler_)
0188 {
0189 }
0190
0191 write_at_op(write_at_op&& other)
0192 : base_from_cancellation_state<WriteHandler>(
0193 static_cast<base_from_cancellation_state<WriteHandler>&&>(other)),
0194 base_from_completion_cond<CompletionCondition>(
0195 static_cast<base_from_completion_cond<CompletionCondition>&&>(other)),
0196 device_(other.device_),
0197 offset_(other.offset_),
0198 buffers_(static_cast<buffers_type&&>(other.buffers_)),
0199 start_(other.start_),
0200 handler_(static_cast<WriteHandler&&>(other.handler_))
0201 {
0202 }
0203
0204 void operator()(boost::system::error_code ec,
0205 std::size_t bytes_transferred, int start = 0)
0206 {
0207 std::size_t max_size;
0208 switch (start_ = start)
0209 {
0210 case 1:
0211 max_size = this->check_for_completion(ec, buffers_.total_consumed());
0212 for (;;)
0213 {
0214 {
0215 BOOST_ASIO_HANDLER_LOCATION((__FILE__, __LINE__, "async_write_at"));
0216 device_.async_write_some_at(
0217 offset_ + buffers_.total_consumed(), buffers_.prepare(max_size),
0218 static_cast<write_at_op&&>(*this));
0219 }
0220 return; default:
0221 buffers_.consume(bytes_transferred);
0222 if ((!ec && bytes_transferred == 0) || buffers_.empty())
0223 break;
0224 max_size = this->check_for_completion(ec, buffers_.total_consumed());
0225 if (max_size == 0)
0226 break;
0227 if (this->cancelled() != cancellation_type::none)
0228 {
0229 ec = boost::asio::error::operation_aborted;
0230 break;
0231 }
0232 }
0233
0234 static_cast<WriteHandler&&>(handler_)(
0235 static_cast<const boost::system::error_code&>(ec),
0236 static_cast<const std::size_t&>(buffers_.total_consumed()));
0237 }
0238 }
0239
0240
0241 typedef boost::asio::detail::consuming_buffers<const_buffer,
0242 ConstBufferSequence, ConstBufferIterator> buffers_type;
0243
0244 AsyncRandomAccessWriteDevice& device_;
0245 uint64_t offset_;
0246 buffers_type buffers_;
0247 int start_;
0248 WriteHandler handler_;
0249 };
0250
0251 template <typename AsyncRandomAccessWriteDevice,
0252 typename ConstBufferSequence, typename ConstBufferIterator,
0253 typename CompletionCondition, typename WriteHandler>
0254 inline bool asio_handler_is_continuation(
0255 write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
0256 ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
0257 {
0258 return this_handler->start_ == 0 ? true
0259 : boost_asio_handler_cont_helpers::is_continuation(
0260 this_handler->handler_);
0261 }
0262
0263 template <typename AsyncRandomAccessWriteDevice,
0264 typename ConstBufferSequence, typename ConstBufferIterator,
0265 typename CompletionCondition, typename WriteHandler>
0266 inline void start_write_at_op(AsyncRandomAccessWriteDevice& d,
0267 uint64_t offset, const ConstBufferSequence& buffers,
0268 const ConstBufferIterator&, CompletionCondition& completion_condition,
0269 WriteHandler& handler)
0270 {
0271 detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
0272 ConstBufferIterator, CompletionCondition, WriteHandler>(
0273 d, offset, buffers, completion_condition, handler)(
0274 boost::system::error_code(), 0, 1);
0275 }
0276
0277 template <typename AsyncRandomAccessWriteDevice>
0278 class initiate_async_write_at
0279 {
0280 public:
0281 typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
0282
0283 explicit initiate_async_write_at(AsyncRandomAccessWriteDevice& device)
0284 : device_(device)
0285 {
0286 }
0287
0288 executor_type get_executor() const noexcept
0289 {
0290 return device_.get_executor();
0291 }
0292
0293 template <typename WriteHandler, typename ConstBufferSequence,
0294 typename CompletionCondition>
0295 void operator()(WriteHandler&& handler,
0296 uint64_t offset, const ConstBufferSequence& buffers,
0297 CompletionCondition&& completion_cond) const
0298 {
0299
0300
0301 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
0302
0303 non_const_lvalue<WriteHandler> handler2(handler);
0304 non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
0305 start_write_at_op(device_, offset, buffers,
0306 boost::asio::buffer_sequence_begin(buffers),
0307 completion_cond2.value, handler2.value);
0308 }
0309
0310 private:
0311 AsyncRandomAccessWriteDevice& device_;
0312 };
0313 }
0314
0315 #if !defined(GENERATING_DOCUMENTATION)
0316
0317 template <template <typename, typename> class Associator,
0318 typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
0319 typename ConstBufferIterator, typename CompletionCondition,
0320 typename WriteHandler, typename DefaultCandidate>
0321 struct associator<Associator,
0322 detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
0323 ConstBufferIterator, CompletionCondition, WriteHandler>,
0324 DefaultCandidate>
0325 : Associator<WriteHandler, DefaultCandidate>
0326 {
0327 static typename Associator<WriteHandler, DefaultCandidate>::type get(
0328 const detail::write_at_op<AsyncRandomAccessWriteDevice,
0329 ConstBufferSequence, ConstBufferIterator,
0330 CompletionCondition, WriteHandler>& h) noexcept
0331 {
0332 return Associator<WriteHandler, DefaultCandidate>::get(h.handler_);
0333 }
0334
0335 static auto get(
0336 const detail::write_at_op<AsyncRandomAccessWriteDevice,
0337 ConstBufferSequence, ConstBufferIterator,
0338 CompletionCondition, WriteHandler>& h,
0339 const DefaultCandidate& c) noexcept
0340 -> decltype(Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c))
0341 {
0342 return Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c);
0343 }
0344 };
0345
0346 #endif
0347
0348 template <typename AsyncRandomAccessWriteDevice,
0349 typename ConstBufferSequence, typename CompletionCondition,
0350 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
0351 std::size_t)) WriteToken>
0352 inline auto async_write_at(AsyncRandomAccessWriteDevice& d,
0353 uint64_t offset, const ConstBufferSequence& buffers,
0354 CompletionCondition completion_condition, WriteToken&& token)
0355 -> decltype(
0356 async_initiate<WriteToken,
0357 void (boost::system::error_code, std::size_t)>(
0358 declval<detail::initiate_async_write_at<
0359 AsyncRandomAccessWriteDevice>>(),
0360 token, offset, buffers,
0361 static_cast<CompletionCondition&&>(completion_condition)))
0362 {
0363 return async_initiate<WriteToken,
0364 void (boost::system::error_code, std::size_t)>(
0365 detail::initiate_async_write_at<AsyncRandomAccessWriteDevice>(d),
0366 token, offset, buffers,
0367 static_cast<CompletionCondition&&>(completion_condition));
0368 }
0369
0370 template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
0371 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
0372 std::size_t)) WriteToken>
0373 inline auto async_write_at(AsyncRandomAccessWriteDevice& d,
0374 uint64_t offset, const ConstBufferSequence& buffers, WriteToken&& token)
0375 -> decltype(
0376 async_initiate<WriteToken,
0377 void (boost::system::error_code, std::size_t)>(
0378 declval<detail::initiate_async_write_at<
0379 AsyncRandomAccessWriteDevice>>(),
0380 token, offset, buffers, transfer_all()))
0381 {
0382 return async_initiate<WriteToken,
0383 void (boost::system::error_code, std::size_t)>(
0384 detail::initiate_async_write_at<AsyncRandomAccessWriteDevice>(d),
0385 token, offset, buffers, transfer_all());
0386 }
0387
0388 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
0389 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0390
0391 namespace detail
0392 {
0393 template <typename Allocator, typename WriteHandler>
0394 class write_at_streambuf_op
0395 {
0396 public:
0397 write_at_streambuf_op(
0398 boost::asio::basic_streambuf<Allocator>& streambuf,
0399 WriteHandler& handler)
0400 : streambuf_(streambuf),
0401 handler_(static_cast<WriteHandler&&>(handler))
0402 {
0403 }
0404
0405 write_at_streambuf_op(const write_at_streambuf_op& other)
0406 : streambuf_(other.streambuf_),
0407 handler_(other.handler_)
0408 {
0409 }
0410
0411 write_at_streambuf_op(write_at_streambuf_op&& other)
0412 : streambuf_(other.streambuf_),
0413 handler_(static_cast<WriteHandler&&>(other.handler_))
0414 {
0415 }
0416
0417 void operator()(const boost::system::error_code& ec,
0418 const std::size_t bytes_transferred)
0419 {
0420 streambuf_.consume(bytes_transferred);
0421 static_cast<WriteHandler&&>(handler_)(ec, bytes_transferred);
0422 }
0423
0424
0425 boost::asio::basic_streambuf<Allocator>& streambuf_;
0426 WriteHandler handler_;
0427 };
0428
0429 template <typename Allocator, typename WriteHandler>
0430 inline bool asio_handler_is_continuation(
0431 write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
0432 {
0433 return boost_asio_handler_cont_helpers::is_continuation(
0434 this_handler->handler_);
0435 }
0436
0437 template <typename AsyncRandomAccessWriteDevice>
0438 class initiate_async_write_at_streambuf
0439 {
0440 public:
0441 typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
0442
0443 explicit initiate_async_write_at_streambuf(
0444 AsyncRandomAccessWriteDevice& device)
0445 : device_(device)
0446 {
0447 }
0448
0449 executor_type get_executor() const noexcept
0450 {
0451 return device_.get_executor();
0452 }
0453
0454 template <typename WriteHandler,
0455 typename Allocator, typename CompletionCondition>
0456 void operator()(WriteHandler&& handler,
0457 uint64_t offset, basic_streambuf<Allocator>* b,
0458 CompletionCondition&& completion_condition) const
0459 {
0460
0461
0462 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
0463
0464 non_const_lvalue<WriteHandler> handler2(handler);
0465 async_write_at(device_, offset, b->data(),
0466 static_cast<CompletionCondition&&>(completion_condition),
0467 write_at_streambuf_op<Allocator, decay_t<WriteHandler>>(
0468 *b, handler2.value));
0469 }
0470
0471 private:
0472 AsyncRandomAccessWriteDevice& device_;
0473 };
0474 }
0475
0476 #if !defined(GENERATING_DOCUMENTATION)
0477
0478 template <template <typename, typename> class Associator,
0479 typename Executor, typename WriteHandler, typename DefaultCandidate>
0480 struct associator<Associator,
0481 detail::write_at_streambuf_op<Executor, WriteHandler>,
0482 DefaultCandidate>
0483 : Associator<WriteHandler, DefaultCandidate>
0484 {
0485 static typename Associator<WriteHandler, DefaultCandidate>::type get(
0486 const detail::write_at_streambuf_op<Executor, WriteHandler>& h) noexcept
0487 {
0488 return Associator<WriteHandler, DefaultCandidate>::get(h.handler_);
0489 }
0490
0491 static auto get(
0492 const detail::write_at_streambuf_op<Executor, WriteHandler>& h,
0493 const DefaultCandidate& c) noexcept
0494 -> decltype(Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c))
0495 {
0496 return Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c);
0497 }
0498 };
0499
0500 #endif
0501
0502 template <typename AsyncRandomAccessWriteDevice,
0503 typename Allocator, typename CompletionCondition,
0504 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
0505 std::size_t)) WriteToken>
0506 inline auto async_write_at(AsyncRandomAccessWriteDevice& d,
0507 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
0508 CompletionCondition completion_condition, WriteToken&& token)
0509 -> decltype(
0510 async_initiate<WriteToken,
0511 void (boost::system::error_code, std::size_t)>(
0512 declval<detail::initiate_async_write_at_streambuf<
0513 AsyncRandomAccessWriteDevice>>(),
0514 token, offset, &b,
0515 static_cast<CompletionCondition&&>(completion_condition)))
0516 {
0517 return async_initiate<WriteToken,
0518 void (boost::system::error_code, std::size_t)>(
0519 detail::initiate_async_write_at_streambuf<
0520 AsyncRandomAccessWriteDevice>(d),
0521 token, offset, &b,
0522 static_cast<CompletionCondition&&>(completion_condition));
0523 }
0524
0525 template <typename AsyncRandomAccessWriteDevice, typename Allocator,
0526 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
0527 std::size_t)) WriteToken>
0528 inline auto async_write_at(AsyncRandomAccessWriteDevice& d,
0529 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
0530 WriteToken&& token)
0531 -> decltype(
0532 async_initiate<WriteToken,
0533 void (boost::system::error_code, std::size_t)>(
0534 declval<detail::initiate_async_write_at_streambuf<
0535 AsyncRandomAccessWriteDevice>>(),
0536 token, offset, &b, transfer_all()))
0537 {
0538 return async_initiate<WriteToken,
0539 void (boost::system::error_code, std::size_t)>(
0540 detail::initiate_async_write_at_streambuf<
0541 AsyncRandomAccessWriteDevice>(d),
0542 token, offset, &b, transfer_all());
0543 }
0544
0545 #endif
0546 #endif
0547
0548 }
0549 }
0550
0551 #include <boost/asio/detail/pop_options.hpp>
0552
0553 #endif