io.hpp 12 KB

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