reactive_socket_recvfrom_op.hpp 6.7 KB

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