reactive_socket_recvmsg_op.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // detail/reactive_socket_recvmsg_op.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_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP
  11. #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_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/bind_handler.hpp>
  17. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  18. #include <boost/asio/detail/fenced_block.hpp>
  19. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  20. #include <boost/asio/detail/handler_work.hpp>
  21. #include <boost/asio/detail/memory.hpp>
  22. #include <boost/asio/detail/reactor_op.hpp>
  23. #include <boost/asio/detail/socket_ops.hpp>
  24. #include <boost/asio/socket_base.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace detail {
  29. template <typename MutableBufferSequence>
  30. class reactive_socket_recvmsg_op_base : public reactor_op
  31. {
  32. public:
  33. reactive_socket_recvmsg_op_base(const boost::system::error_code& success_ec,
  34. socket_type socket, const MutableBufferSequence& buffers,
  35. socket_base::message_flags in_flags,
  36. socket_base::message_flags& out_flags, func_type complete_func)
  37. : reactor_op(success_ec,
  38. &reactive_socket_recvmsg_op_base::do_perform, complete_func),
  39. socket_(socket),
  40. buffers_(buffers),
  41. in_flags_(in_flags),
  42. out_flags_(out_flags)
  43. {
  44. }
  45. static status do_perform(reactor_op* base)
  46. {
  47. BOOST_ASIO_ASSUME(base != 0);
  48. reactive_socket_recvmsg_op_base* o(
  49. static_cast<reactive_socket_recvmsg_op_base*>(base));
  50. buffer_sequence_adapter<boost::asio::mutable_buffer,
  51. MutableBufferSequence> bufs(o->buffers_);
  52. status result = socket_ops::non_blocking_recvmsg(o->socket_,
  53. bufs.buffers(), bufs.count(),
  54. o->in_flags_, o->out_flags_,
  55. o->ec_, o->bytes_transferred_) ? done : not_done;
  56. BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_recvmsg",
  57. o->ec_, o->bytes_transferred_));
  58. return result;
  59. }
  60. private:
  61. socket_type socket_;
  62. MutableBufferSequence buffers_;
  63. socket_base::message_flags in_flags_;
  64. socket_base::message_flags& out_flags_;
  65. };
  66. template <typename MutableBufferSequence, typename Handler, typename IoExecutor>
  67. class reactive_socket_recvmsg_op :
  68. public reactive_socket_recvmsg_op_base<MutableBufferSequence>
  69. {
  70. public:
  71. typedef Handler handler_type;
  72. typedef IoExecutor io_executor_type;
  73. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvmsg_op);
  74. reactive_socket_recvmsg_op(const boost::system::error_code& success_ec,
  75. socket_type socket, const MutableBufferSequence& buffers,
  76. socket_base::message_flags in_flags,
  77. socket_base::message_flags& out_flags, Handler& handler,
  78. const IoExecutor& io_ex)
  79. : reactive_socket_recvmsg_op_base<MutableBufferSequence>(
  80. success_ec, socket, buffers, in_flags, out_flags,
  81. &reactive_socket_recvmsg_op::do_complete),
  82. handler_(static_cast<Handler&&>(handler)),
  83. work_(handler_, io_ex)
  84. {
  85. }
  86. static void do_complete(void* owner, operation* base,
  87. const boost::system::error_code& /*ec*/,
  88. std::size_t /*bytes_transferred*/)
  89. {
  90. // Take ownership of the handler object.
  91. BOOST_ASIO_ASSUME(base != 0);
  92. reactive_socket_recvmsg_op* o(
  93. static_cast<reactive_socket_recvmsg_op*>(base));
  94. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  95. BOOST_ASIO_HANDLER_COMPLETION((*o));
  96. // Take ownership of the operation's outstanding work.
  97. handler_work<Handler, IoExecutor> w(
  98. static_cast<handler_work<Handler, IoExecutor>&&>(
  99. o->work_));
  100. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  101. // Make a copy of the handler so that the memory can be deallocated before
  102. // the upcall is made. Even if we're not about to make an upcall, a
  103. // sub-object of the handler may be the true owner of the memory associated
  104. // with the handler. Consequently, a local copy of the handler is required
  105. // to ensure that any owning sub-object remains valid until after we have
  106. // deallocated the memory here.
  107. detail::binder2<Handler, boost::system::error_code, std::size_t>
  108. handler(o->handler_, o->ec_, o->bytes_transferred_);
  109. p.h = boost::asio::detail::addressof(handler.handler_);
  110. p.reset();
  111. // Make the upcall if required.
  112. if (owner)
  113. {
  114. fenced_block b(fenced_block::half);
  115. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  116. w.complete(handler, handler.handler_);
  117. BOOST_ASIO_HANDLER_INVOCATION_END;
  118. }
  119. }
  120. static void do_immediate(operation* base, bool, const void* io_ex)
  121. {
  122. // Take ownership of the handler object.
  123. BOOST_ASIO_ASSUME(base != 0);
  124. reactive_socket_recvmsg_op* o(
  125. static_cast<reactive_socket_recvmsg_op*>(base));
  126. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  127. BOOST_ASIO_HANDLER_COMPLETION((*o));
  128. // Take ownership of the operation's outstanding work.
  129. immediate_handler_work<Handler, IoExecutor> w(
  130. static_cast<handler_work<Handler, IoExecutor>&&>(
  131. o->work_));
  132. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  133. // Make a copy of the handler so that the memory can be deallocated before
  134. // the upcall is made. Even if we're not about to make an upcall, a
  135. // sub-object of the handler may be the true owner of the memory associated
  136. // with the handler. Consequently, a local copy of the handler is required
  137. // to ensure that any owning sub-object remains valid until after we have
  138. // deallocated the memory here.
  139. detail::binder2<Handler, boost::system::error_code, std::size_t>
  140. handler(o->handler_, o->ec_, o->bytes_transferred_);
  141. p.h = boost::asio::detail::addressof(handler.handler_);
  142. p.reset();
  143. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  144. w.complete(handler, handler.handler_, io_ex);
  145. BOOST_ASIO_HANDLER_INVOCATION_END;
  146. }
  147. private:
  148. Handler handler_;
  149. handler_work<Handler, IoExecutor> work_;
  150. };
  151. } // namespace detail
  152. } // namespace asio
  153. } // namespace boost
  154. #include <boost/asio/detail/pop_options.hpp>
  155. #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP