io.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //
  2. // ssl/detail/io.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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. #ifndef BOOST_ASIO_SSL_DETAIL_IO_HPP
  11. #define BOOST_ASIO_SSL_DETAIL_IO_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/detail/base_from_cancellation_state.hpp>
  17. #include <boost/asio/detail/handler_tracking.hpp>
  18. #include <boost/asio/ssl/detail/engine.hpp>
  19. #include <boost/asio/ssl/detail/stream_core.hpp>
  20. #include <boost/asio/write.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace ssl {
  25. namespace detail {
  26. template <typename Stream, typename Operation>
  27. std::size_t io(Stream& next_layer, stream_core& core,
  28. const Operation& op, boost::system::error_code& ec)
  29. {
  30. boost::system::error_code io_ec;
  31. std::size_t bytes_transferred = 0;
  32. do switch (op(core.engine_, ec, bytes_transferred))
  33. {
  34. case engine::want_input_and_retry:
  35. // If the input buffer is empty then we need to read some more data from
  36. // the underlying transport.
  37. if (core.input_.size() == 0)
  38. {
  39. core.input_ = boost::asio::buffer(core.input_buffer_,
  40. next_layer.read_some(core.input_buffer_, io_ec));
  41. if (!ec)
  42. ec = io_ec;
  43. }
  44. // Pass the new input data to the engine.
  45. core.input_ = core.engine_.put_input(core.input_);
  46. // Try the operation again.
  47. continue;
  48. case engine::want_output_and_retry:
  49. // Get output data from the engine and write it to the underlying
  50. // transport.
  51. boost::asio::write(next_layer,
  52. core.engine_.get_output(core.output_buffer_), io_ec);
  53. if (!ec)
  54. ec = io_ec;
  55. // Try the operation again.
  56. continue;
  57. case engine::want_output:
  58. // Get output data from the engine and write it to the underlying
  59. // transport.
  60. boost::asio::write(next_layer,
  61. core.engine_.get_output(core.output_buffer_), io_ec);
  62. if (!ec)
  63. ec = io_ec;
  64. // Operation is complete. Return result to caller.
  65. core.engine_.map_error_code(ec);
  66. op.complete_sync(ec);
  67. return bytes_transferred;
  68. default:
  69. // Operation is complete. Return result to caller.
  70. core.engine_.map_error_code(ec);
  71. op.complete_sync(ec);
  72. return bytes_transferred;
  73. } while (!ec);
  74. // Operation failed. Return result to caller.
  75. core.engine_.map_error_code(ec);
  76. op.complete_sync(ec);
  77. return 0;
  78. }
  79. template <typename Stream, typename Operation, typename Handler>
  80. class io_op
  81. : public boost::asio::detail::base_from_cancellation_state<Handler>
  82. {
  83. public:
  84. io_op(Stream& next_layer, stream_core& core,
  85. const Operation& op, Handler& handler)
  86. : boost::asio::detail::base_from_cancellation_state<Handler>(handler),
  87. next_layer_(next_layer),
  88. core_(core),
  89. op_(op),
  90. start_(0),
  91. want_(engine::want_nothing),
  92. bytes_transferred_(0),
  93. handler_(static_cast<Handler&&>(handler))
  94. {
  95. }
  96. io_op(const io_op& other)
  97. : boost::asio::detail::base_from_cancellation_state<Handler>(other),
  98. next_layer_(other.next_layer_),
  99. core_(other.core_),
  100. op_(other.op_),
  101. start_(other.start_),
  102. want_(other.want_),
  103. ec_(other.ec_),
  104. bytes_transferred_(other.bytes_transferred_),
  105. handler_(other.handler_)
  106. {
  107. }
  108. io_op(io_op&& other)
  109. : boost::asio::detail::base_from_cancellation_state<Handler>(
  110. static_cast<
  111. boost::asio::detail::base_from_cancellation_state<Handler>&&>(other)),
  112. next_layer_(other.next_layer_),
  113. core_(other.core_),
  114. op_(static_cast<Operation&&>(other.op_)),
  115. start_(other.start_),
  116. want_(other.want_),
  117. ec_(other.ec_),
  118. bytes_transferred_(other.bytes_transferred_),
  119. handler_(static_cast<Handler&&>(other.handler_))
  120. {
  121. }
  122. void operator()(boost::system::error_code ec,
  123. std::size_t bytes_transferred = ~std::size_t(0), int start = 0)
  124. {
  125. switch (start_ = start)
  126. {
  127. case 1: // Called after at least one async operation.
  128. do
  129. {
  130. switch (want_ = op_(core_.engine_, ec_, bytes_transferred_))
  131. {
  132. case engine::want_input_and_retry:
  133. // If the input buffer already has data in it we can pass it to the
  134. // engine and then retry the operation immediately.
  135. if (core_.input_.size() != 0)
  136. {
  137. core_.input_ = core_.engine_.put_input(core_.input_);
  138. continue;
  139. }
  140. // The engine wants more data to be read from input. However, we
  141. // cannot allow more than one read operation at a time on the
  142. // underlying transport. The pending_read_ timer's expiry is set to
  143. // pos_infin if a read is in progress, and neg_infin otherwise.
  144. if (core_.expiry(core_.pending_read_) == core_.neg_infin())
  145. {
  146. // Prevent other read operations from being started.
  147. core_.pending_read_.expires_at(core_.pos_infin());
  148. BOOST_ASIO_HANDLER_LOCATION((
  149. __FILE__, __LINE__, Operation::tracking_name()));
  150. // Start reading some data from the underlying transport.
  151. next_layer_.async_read_some(
  152. boost::asio::buffer(core_.input_buffer_),
  153. static_cast<io_op&&>(*this));
  154. }
  155. else
  156. {
  157. BOOST_ASIO_HANDLER_LOCATION((
  158. __FILE__, __LINE__, Operation::tracking_name()));
  159. // Wait until the current read operation completes.
  160. core_.pending_read_.async_wait(static_cast<io_op&&>(*this));
  161. }
  162. // Yield control until asynchronous operation completes. Control
  163. // resumes at the "default:" label below.
  164. return;
  165. case engine::want_output_and_retry:
  166. case engine::want_output:
  167. // The engine wants some data to be written to the output. However, we
  168. // cannot allow more than one write operation at a time on the
  169. // underlying transport. The pending_write_ timer's expiry is set to
  170. // pos_infin if a write is in progress, and neg_infin otherwise.
  171. if (core_.expiry(core_.pending_write_) == core_.neg_infin())
  172. {
  173. // Prevent other write operations from being started.
  174. core_.pending_write_.expires_at(core_.pos_infin());
  175. BOOST_ASIO_HANDLER_LOCATION((
  176. __FILE__, __LINE__, Operation::tracking_name()));
  177. // Start writing all the data to the underlying transport.
  178. boost::asio::async_write(next_layer_,
  179. core_.engine_.get_output(core_.output_buffer_),
  180. static_cast<io_op&&>(*this));
  181. }
  182. else
  183. {
  184. BOOST_ASIO_HANDLER_LOCATION((
  185. __FILE__, __LINE__, Operation::tracking_name()));
  186. // Wait until the current write operation completes.
  187. core_.pending_write_.async_wait(static_cast<io_op&&>(*this));
  188. }
  189. // Yield control until asynchronous operation completes. Control
  190. // resumes at the "default:" label below.
  191. return;
  192. default:
  193. // The SSL operation is done and we can invoke the handler, but we
  194. // have to keep in mind that this function might be being called from
  195. // the async operation's initiating function. In this case we're not
  196. // allowed to call the handler directly. Instead, issue a zero-sized
  197. // read so the handler runs "as-if" posted using io_context::post().
  198. if (start)
  199. {
  200. BOOST_ASIO_HANDLER_LOCATION((
  201. __FILE__, __LINE__, Operation::tracking_name()));
  202. next_layer_.async_read_some(
  203. boost::asio::buffer(core_.input_buffer_, 0),
  204. static_cast<io_op&&>(*this));
  205. // Yield control until asynchronous operation completes. Control
  206. // resumes at the "default:" label below.
  207. return;
  208. }
  209. else
  210. {
  211. // Continue on to run handler directly.
  212. break;
  213. }
  214. }
  215. default:
  216. if (bytes_transferred == ~std::size_t(0))
  217. bytes_transferred = 0; // Timer cancellation, no data transferred.
  218. else if (!ec_)
  219. ec_ = ec;
  220. switch (want_)
  221. {
  222. case engine::want_input_and_retry:
  223. // Add received data to the engine's input.
  224. core_.input_ = boost::asio::buffer(
  225. core_.input_buffer_, bytes_transferred);
  226. core_.input_ = core_.engine_.put_input(core_.input_);
  227. // Release any waiting read operations.
  228. core_.pending_read_.expires_at(core_.neg_infin());
  229. // Check for cancellation before continuing.
  230. if (this->cancelled() != cancellation_type::none)
  231. {
  232. ec_ = boost::asio::error::operation_aborted;
  233. break;
  234. }
  235. // Try the operation again.
  236. continue;
  237. case engine::want_output_and_retry:
  238. // Release any waiting write operations.
  239. core_.pending_write_.expires_at(core_.neg_infin());
  240. // Check for cancellation before continuing.
  241. if (this->cancelled() != cancellation_type::none)
  242. {
  243. ec_ = boost::asio::error::operation_aborted;
  244. break;
  245. }
  246. // Try the operation again.
  247. continue;
  248. case engine::want_output:
  249. // Release any waiting write operations.
  250. core_.pending_write_.expires_at(core_.neg_infin());
  251. // Fall through to call handler.
  252. default:
  253. // Pass the result to the handler.
  254. op_.call_handler(handler_,
  255. core_.engine_.map_error_code(ec_),
  256. ec_ ? 0 : bytes_transferred_);
  257. // Our work here is done.
  258. return;
  259. }
  260. } while (!ec_);
  261. // Operation failed. Pass the result to the handler.
  262. op_.call_handler(handler_, core_.engine_.map_error_code(ec_), 0);
  263. }
  264. }
  265. //private:
  266. Stream& next_layer_;
  267. stream_core& core_;
  268. Operation op_;
  269. int start_;
  270. engine::want want_;
  271. boost::system::error_code ec_;
  272. std::size_t bytes_transferred_;
  273. Handler handler_;
  274. };
  275. template <typename Stream, typename Operation, typename Handler>
  276. inline bool asio_handler_is_continuation(
  277. io_op<Stream, Operation, Handler>* this_handler)
  278. {
  279. return this_handler->start_ == 0 ? true
  280. : boost_asio_handler_cont_helpers::is_continuation(this_handler->handler_);
  281. }
  282. template <typename Stream, typename Operation, typename Handler>
  283. inline void async_io(Stream& next_layer, stream_core& core,
  284. const Operation& op, Handler& handler)
  285. {
  286. io_op<Stream, Operation, Handler>(
  287. next_layer, core, op, handler)(
  288. boost::system::error_code(), 0, 1);
  289. }
  290. } // namespace detail
  291. } // namespace ssl
  292. template <template <typename, typename> class Associator,
  293. typename Stream, typename Operation,
  294. typename Handler, typename DefaultCandidate>
  295. struct associator<Associator,
  296. ssl::detail::io_op<Stream, Operation, Handler>,
  297. DefaultCandidate>
  298. : Associator<Handler, DefaultCandidate>
  299. {
  300. static typename Associator<Handler, DefaultCandidate>::type get(
  301. const ssl::detail::io_op<Stream, Operation, Handler>& h) noexcept
  302. {
  303. return Associator<Handler, DefaultCandidate>::get(h.handler_);
  304. }
  305. static auto get(const ssl::detail::io_op<Stream, Operation, Handler>& h,
  306. const DefaultCandidate& c) noexcept
  307. -> decltype(Associator<Handler, DefaultCandidate>::get(h.handler_, c))
  308. {
  309. return Associator<Handler, DefaultCandidate>::get(h.handler_, c);
  310. }
  311. };
  312. } // namespace asio
  313. } // namespace boost
  314. #include <boost/asio/detail/pop_options.hpp>
  315. #endif // BOOST_ASIO_SSL_DETAIL_IO_HPP