reactive_socket_recvfrom_op.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // detail/reactive_socket_recvfrom_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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/memory.hpp>
  20. #include <boost/asio/detail/reactor_op.hpp>
  21. #include <boost/asio/detail/socket_ops.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace detail {
  26. template <typename MutableBufferSequence, typename Endpoint>
  27. class reactive_socket_recvfrom_op_base : public reactor_op
  28. {
  29. public:
  30. reactive_socket_recvfrom_op_base(socket_type socket, int protocol_type,
  31. const MutableBufferSequence& buffers, Endpoint& endpoint,
  32. socket_base::message_flags flags, func_type complete_func)
  33. : reactor_op(&reactive_socket_recvfrom_op_base::do_perform, complete_func),
  34. socket_(socket),
  35. protocol_type_(protocol_type),
  36. buffers_(buffers),
  37. sender_endpoint_(endpoint),
  38. flags_(flags)
  39. {
  40. }
  41. static status do_perform(reactor_op* base)
  42. {
  43. reactive_socket_recvfrom_op_base* o(
  44. static_cast<reactive_socket_recvfrom_op_base*>(base));
  45. buffer_sequence_adapter<boost::asio::mutable_buffer,
  46. MutableBufferSequence> bufs(o->buffers_);
  47. std::size_t addr_len = o->sender_endpoint_.capacity();
  48. status result = socket_ops::non_blocking_recvfrom(o->socket_,
  49. bufs.buffers(), bufs.count(), o->flags_,
  50. o->sender_endpoint_.data(), &addr_len,
  51. o->ec_, o->bytes_transferred_) ? done : not_done;
  52. if (result && !o->ec_)
  53. o->sender_endpoint_.resize(addr_len);
  54. BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_recvfrom",
  55. o->ec_, o->bytes_transferred_));
  56. return result;
  57. }
  58. private:
  59. socket_type socket_;
  60. int protocol_type_;
  61. MutableBufferSequence buffers_;
  62. Endpoint& sender_endpoint_;
  63. socket_base::message_flags flags_;
  64. };
  65. template <typename MutableBufferSequence, typename Endpoint,
  66. typename Handler, typename IoExecutor>
  67. class reactive_socket_recvfrom_op :
  68. public reactive_socket_recvfrom_op_base<MutableBufferSequence, Endpoint>
  69. {
  70. public:
  71. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvfrom_op);
  72. reactive_socket_recvfrom_op(socket_type socket, int protocol_type,
  73. const MutableBufferSequence& buffers, Endpoint& endpoint,
  74. socket_base::message_flags flags, Handler& handler,
  75. const IoExecutor& io_ex)
  76. : reactive_socket_recvfrom_op_base<MutableBufferSequence, Endpoint>(
  77. socket, protocol_type, buffers, endpoint, flags,
  78. &reactive_socket_recvfrom_op::do_complete),
  79. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
  80. io_executor_(io_ex)
  81. {
  82. handler_work<Handler, IoExecutor>::start(handler_, io_executor_);
  83. }
  84. static void do_complete(void* owner, operation* base,
  85. const boost::system::error_code& /*ec*/,
  86. std::size_t /*bytes_transferred*/)
  87. {
  88. // Take ownership of the handler object.
  89. reactive_socket_recvfrom_op* o(
  90. static_cast<reactive_socket_recvfrom_op*>(base));
  91. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  92. handler_work<Handler, IoExecutor> w(o->handler_, o->io_executor_);
  93. BOOST_ASIO_HANDLER_COMPLETION((*o));
  94. // Make a copy of the handler so that the memory can be deallocated before
  95. // the upcall is made. Even if we're not about to make an upcall, a
  96. // sub-object of the handler may be the true owner of the memory associated
  97. // with the handler. Consequently, a local copy of the handler is required
  98. // to ensure that any owning sub-object remains valid until after we have
  99. // deallocated the memory here.
  100. detail::binder2<Handler, boost::system::error_code, std::size_t>
  101. handler(o->handler_, o->ec_, o->bytes_transferred_);
  102. p.h = boost::asio::detail::addressof(handler.handler_);
  103. p.reset();
  104. // Make the upcall if required.
  105. if (owner)
  106. {
  107. fenced_block b(fenced_block::half);
  108. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  109. w.complete(handler, handler.handler_);
  110. BOOST_ASIO_HANDLER_INVOCATION_END;
  111. }
  112. }
  113. private:
  114. Handler handler_;
  115. IoExecutor io_executor_;
  116. };
  117. } // namespace detail
  118. } // namespace asio
  119. } // namespace boost
  120. #include <boost/asio/detail/pop_options.hpp>
  121. #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVFROM_OP_HPP