File indexing completed on 2025-07-11 08:05:21
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 constraint_t<
0070 is_completion_condition<CompletionCondition>::value
0071 >)
0072 {
0073 return detail::write_at_buffer_sequence(d, offset, buffers,
0074 boost::asio::buffer_sequence_begin(buffers),
0075 static_cast<CompletionCondition&&>(completion_condition), ec);
0076 }
0077
0078 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
0079 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0080 uint64_t offset, const ConstBufferSequence& buffers)
0081 {
0082 boost::system::error_code ec;
0083 std::size_t bytes_transferred = write_at(
0084 d, offset, buffers, transfer_all(), ec);
0085 boost::asio::detail::throw_error(ec, "write_at");
0086 return bytes_transferred;
0087 }
0088
0089 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
0090 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0091 uint64_t offset, const ConstBufferSequence& buffers,
0092 boost::system::error_code& ec)
0093 {
0094 return write_at(d, offset, buffers, transfer_all(), ec);
0095 }
0096
0097 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
0098 typename CompletionCondition>
0099 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0100 uint64_t offset, const ConstBufferSequence& buffers,
0101 CompletionCondition completion_condition,
0102 constraint_t<
0103 is_completion_condition<CompletionCondition>::value
0104 >)
0105 {
0106 boost::system::error_code ec;
0107 std::size_t bytes_transferred = write_at(d, offset, buffers,
0108 static_cast<CompletionCondition&&>(completion_condition), ec);
0109 boost::asio::detail::throw_error(ec, "write_at");
0110 return bytes_transferred;
0111 }
0112
0113 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
0114 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0115
0116 template <typename SyncRandomAccessWriteDevice, typename Allocator,
0117 typename CompletionCondition>
0118 std::size_t write_at(SyncRandomAccessWriteDevice& d,
0119 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
0120 CompletionCondition completion_condition, boost::system::error_code& ec,
0121 constraint_t<
0122 is_completion_condition<CompletionCondition>::value
0123 >)
0124 {
0125 std::size_t bytes_transferred = write_at(d, offset, b.data(),
0126 static_cast<CompletionCondition&&>(completion_condition), ec);
0127 b.consume(bytes_transferred);
0128 return bytes_transferred;
0129 }
0130
0131 template <typename SyncRandomAccessWriteDevice, typename Allocator>
0132 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0133 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b)
0134 {
0135 boost::system::error_code ec;
0136 std::size_t bytes_transferred = write_at(d, offset, b, transfer_all(), ec);
0137 boost::asio::detail::throw_error(ec, "write_at");
0138 return bytes_transferred;
0139 }
0140
0141 template <typename SyncRandomAccessWriteDevice, typename Allocator>
0142 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0143 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
0144 boost::system::error_code& ec)
0145 {
0146 return write_at(d, offset, b, transfer_all(), ec);
0147 }
0148
0149 template <typename SyncRandomAccessWriteDevice, typename Allocator,
0150 typename CompletionCondition>
0151 inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
0152 uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
0153 CompletionCondition completion_condition,
0154 constraint_t<
0155 is_completion_condition<CompletionCondition>::value
0156 >)
0157 {
0158 boost::system::error_code ec;
0159 std::size_t bytes_transferred = write_at(d, offset, b,
0160 static_cast<CompletionCondition&&>(completion_condition), ec);
0161 boost::asio::detail::throw_error(ec, "write_at");
0162 return bytes_transferred;
0163 }
0164
0165 #endif
0166 #endif
0167
0168 namespace detail
0169 {
0170 template <typename AsyncRandomAccessWriteDevice,
0171 typename ConstBufferSequence, typename ConstBufferIterator,
0172 typename CompletionCondition, typename WriteHandler>
0173 class write_at_op
0174 : public base_from_cancellation_state<WriteHandler>,
0175 base_from_completion_cond<CompletionCondition>
0176 {
0177 public:
0178 write_at_op(AsyncRandomAccessWriteDevice& device,
0179 uint64_t offset, const ConstBufferSequence& buffers,
0180 CompletionCondition& completion_condition, WriteHandler& handler)
0181 : base_from_cancellation_state<WriteHandler>(
0182 handler, enable_partial_cancellation()),
0183 base_from_completion_cond<CompletionCondition>(completion_condition),
0184 device_(device),
0185 offset_(offset),
0186 buffers_(buffers),
0187 start_(0),
0188 handler_(static_cast<WriteHandler&&>(handler))
0189 {
0190 }
0191
0192 write_at_op(const write_at_op& other)
0193 : base_from_cancellation_state<WriteHandler>(other),
0194 base_from_completion_cond<CompletionCondition>(other),
0195 device_(other.device_),
0196 offset_(other.offset_),
0197 buffers_(other.buffers_),
0198 start_(other.start_),
0199 handler_(other.handler_)
0200 {
0201 }
0202
0203 write_at_op(write_at_op&& other)
0204 : base_from_cancellation_state<WriteHandler>(
0205 static_cast<base_from_cancellation_state<WriteHandler>&&>(other)),
0206 base_from_completion_cond<CompletionCondition>(
0207 static_cast<base_from_completion_cond<CompletionCondition>&&>(other)),
0208 device_(other.device_),
0209 offset_(other.offset_),
0210 buffers_(static_cast<buffers_type&&>(other.buffers_)),
0211 start_(other.start_),
0212 handler_(static_cast<WriteHandler&&>(other.handler_))
0213 {
0214 }
0215
0216 void operator()(boost::system::error_code ec,
0217 std::size_t bytes_transferred, int start = 0)
0218 {
0219 std::size_t max_size;
0220 switch (start_ = start)
0221 {
0222 case 1:
0223 max_size = this->check_for_completion(ec, buffers_.total_consumed());
0224 for (;;)
0225 {
0226 {
0227 BOOST_ASIO_HANDLER_LOCATION((__FILE__, __LINE__, "async_write_at"));
0228 device_.async_write_some_at(
0229 offset_ + buffers_.total_consumed(), buffers_.prepare(max_size),
0230 static_cast<write_at_op&&>(*this));
0231 }
0232 return; default:
0233 buffers_.consume(bytes_transferred);
0234 if ((!ec && bytes_transferred == 0) || buffers_.empty())
0235 break;
0236 max_size = this->check_for_completion(ec, buffers_.total_consumed());
0237 if (max_size == 0)
0238 break;
0239 if (this->cancelled() != cancellation_type::none)
0240 {
0241 ec = boost::asio::error::operation_aborted;
0242 break;
0243 }
0244 }
0245
0246 static_cast<WriteHandler&&>(handler_)(
0247 static_cast<const boost::system::error_code&>(ec),
0248 static_cast<const std::size_t&>(buffers_.total_consumed()));
0249 }
0250 }
0251
0252
0253 typedef boost::asio::detail::consuming_buffers<const_buffer,
0254 ConstBufferSequence, ConstBufferIterator> buffers_type;
0255
0256 AsyncRandomAccessWriteDevice& device_;
0257 uint64_t offset_;
0258 buffers_type buffers_;
0259 int start_;
0260 WriteHandler handler_;
0261 };
0262
0263 template <typename AsyncRandomAccessWriteDevice,
0264 typename ConstBufferSequence, typename ConstBufferIterator,
0265 typename CompletionCondition, typename WriteHandler>
0266 inline bool asio_handler_is_continuation(
0267 write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
0268 ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
0269 {
0270 return this_handler->start_ == 0 ? true
0271 : boost_asio_handler_cont_helpers::is_continuation(
0272 this_handler->handler_);
0273 }
0274
0275 template <typename AsyncRandomAccessWriteDevice,
0276 typename ConstBufferSequence, typename ConstBufferIterator,
0277 typename CompletionCondition, typename WriteHandler>
0278 inline void start_write_at_op(AsyncRandomAccessWriteDevice& d,
0279 uint64_t offset, const ConstBufferSequence& buffers,
0280 const ConstBufferIterator&, CompletionCondition& completion_condition,
0281 WriteHandler& handler)
0282 {
0283 detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
0284 ConstBufferIterator, CompletionCondition, WriteHandler>(
0285 d, offset, buffers, completion_condition, handler)(
0286 boost::system::error_code(), 0, 1);
0287 }
0288
0289 template <typename AsyncRandomAccessWriteDevice>
0290 class initiate_async_write_at
0291 {
0292 public:
0293 typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
0294
0295 explicit initiate_async_write_at(AsyncRandomAccessWriteDevice& device)
0296 : device_(device)
0297 {
0298 }
0299
0300 executor_type get_executor() const noexcept
0301 {
0302 return device_.get_executor();
0303 }
0304
0305 template <typename WriteHandler, typename ConstBufferSequence,
0306 typename CompletionCondition>
0307 void operator()(WriteHandler&& handler,
0308 uint64_t offset, const ConstBufferSequence& buffers,
0309 CompletionCondition&& completion_cond) const
0310 {
0311
0312
0313 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
0314
0315 non_const_lvalue<WriteHandler> handler2(handler);
0316 non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
0317 start_write_at_op(device_, offset, buffers,
0318 boost::asio::buffer_sequence_begin(buffers),
0319 completion_cond2.value, handler2.value);
0320 }
0321
0322 private:
0323 AsyncRandomAccessWriteDevice& device_;
0324 };
0325 }
0326
0327 #if !defined(GENERATING_DOCUMENTATION)
0328
0329 template <template <typename, typename> class Associator,
0330 typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
0331 typename ConstBufferIterator, typename CompletionCondition,
0332 typename WriteHandler, typename DefaultCandidate>
0333 struct associator<Associator,
0334 detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
0335 ConstBufferIterator, CompletionCondition, WriteHandler>,
0336 DefaultCandidate>
0337 : Associator<WriteHandler, DefaultCandidate>
0338 {
0339 static typename Associator<WriteHandler, DefaultCandidate>::type get(
0340 const detail::write_at_op<AsyncRandomAccessWriteDevice,
0341 ConstBufferSequence, ConstBufferIterator,
0342 CompletionCondition, WriteHandler>& h) noexcept
0343 {
0344 return Associator<WriteHandler, DefaultCandidate>::get(h.handler_);
0345 }
0346
0347 static auto get(
0348 const detail::write_at_op<AsyncRandomAccessWriteDevice,
0349 ConstBufferSequence, ConstBufferIterator,
0350 CompletionCondition, WriteHandler>& h,
0351 const DefaultCandidate& c) noexcept
0352 -> decltype(Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c))
0353 {
0354 return Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c);
0355 }
0356 };
0357
0358 #endif
0359
0360 #if !defined(BOOST_ASIO_NO_EXTENSIONS)
0361 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0362
0363 namespace detail
0364 {
0365 template <typename Allocator, typename WriteHandler>
0366 class write_at_streambuf_op
0367 {
0368 public:
0369 write_at_streambuf_op(
0370 boost::asio::basic_streambuf<Allocator>& streambuf,
0371 WriteHandler& handler)
0372 : streambuf_(streambuf),
0373 handler_(static_cast<WriteHandler&&>(handler))
0374 {
0375 }
0376
0377 write_at_streambuf_op(const write_at_streambuf_op& other)
0378 : streambuf_(other.streambuf_),
0379 handler_(other.handler_)
0380 {
0381 }
0382
0383 write_at_streambuf_op(write_at_streambuf_op&& other)
0384 : streambuf_(other.streambuf_),
0385 handler_(static_cast<WriteHandler&&>(other.handler_))
0386 {
0387 }
0388
0389 void operator()(const boost::system::error_code& ec,
0390 const std::size_t bytes_transferred)
0391 {
0392 streambuf_.consume(bytes_transferred);
0393 static_cast<WriteHandler&&>(handler_)(ec, bytes_transferred);
0394 }
0395
0396
0397 boost::asio::basic_streambuf<Allocator>& streambuf_;
0398 WriteHandler handler_;
0399 };
0400
0401 template <typename Allocator, typename WriteHandler>
0402 inline bool asio_handler_is_continuation(
0403 write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
0404 {
0405 return boost_asio_handler_cont_helpers::is_continuation(
0406 this_handler->handler_);
0407 }
0408
0409 template <typename AsyncRandomAccessWriteDevice>
0410 class initiate_async_write_at_streambuf
0411 {
0412 public:
0413 typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
0414
0415 explicit initiate_async_write_at_streambuf(
0416 AsyncRandomAccessWriteDevice& device)
0417 : device_(device)
0418 {
0419 }
0420
0421 executor_type get_executor() const noexcept
0422 {
0423 return device_.get_executor();
0424 }
0425
0426 template <typename WriteHandler,
0427 typename Allocator, typename CompletionCondition>
0428 void operator()(WriteHandler&& handler,
0429 uint64_t offset, basic_streambuf<Allocator>* b,
0430 CompletionCondition&& completion_condition) const
0431 {
0432
0433
0434 BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
0435
0436 non_const_lvalue<WriteHandler> handler2(handler);
0437 async_write_at(device_, offset, b->data(),
0438 static_cast<CompletionCondition&&>(completion_condition),
0439 write_at_streambuf_op<Allocator, decay_t<WriteHandler>>(
0440 *b, handler2.value));
0441 }
0442
0443 private:
0444 AsyncRandomAccessWriteDevice& device_;
0445 };
0446 }
0447
0448 #if !defined(GENERATING_DOCUMENTATION)
0449
0450 template <template <typename, typename> class Associator,
0451 typename Executor, typename WriteHandler, typename DefaultCandidate>
0452 struct associator<Associator,
0453 detail::write_at_streambuf_op<Executor, WriteHandler>,
0454 DefaultCandidate>
0455 : Associator<WriteHandler, DefaultCandidate>
0456 {
0457 static typename Associator<WriteHandler, DefaultCandidate>::type get(
0458 const detail::write_at_streambuf_op<Executor, WriteHandler>& h) noexcept
0459 {
0460 return Associator<WriteHandler, DefaultCandidate>::get(h.handler_);
0461 }
0462
0463 static auto get(
0464 const detail::write_at_streambuf_op<Executor, WriteHandler>& h,
0465 const DefaultCandidate& c) noexcept
0466 -> decltype(Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c))
0467 {
0468 return Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c);
0469 }
0470 };
0471
0472 #endif
0473
0474 #endif
0475 #endif
0476
0477 }
0478 }
0479
0480 #include <boost/asio/detail/pop_options.hpp>
0481
0482 #endif