reactive_socket_recvfrom_op.hpp 7.0 KB

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