reactive_socket_recvmsg_op.hpp 4.5 KB

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