reactive_socket_recvmsg_op.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // detail/reactive_socket_recvmsg_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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/addressof.hpp>
  17. #include <boost/asio/detail/bind_handler.hpp>
  18. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  19. #include <boost/asio/detail/fenced_block.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 bool 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. return 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_);
  51. }
  52. private:
  53. socket_type socket_;
  54. MutableBufferSequence buffers_;
  55. socket_base::message_flags in_flags_;
  56. socket_base::message_flags& out_flags_;
  57. };
  58. template <typename MutableBufferSequence, typename Handler>
  59. class reactive_socket_recvmsg_op :
  60. public reactive_socket_recvmsg_op_base<MutableBufferSequence>
  61. {
  62. public:
  63. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvmsg_op);
  64. reactive_socket_recvmsg_op(socket_type socket,
  65. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  66. socket_base::message_flags& out_flags, Handler& handler)
  67. : reactive_socket_recvmsg_op_base<MutableBufferSequence>(socket, buffers,
  68. in_flags, out_flags, &reactive_socket_recvmsg_op::do_complete),
  69. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
  70. {
  71. }
  72. static void do_complete(io_service_impl* owner, operation* base,
  73. const boost::system::error_code& /*ec*/,
  74. std::size_t /*bytes_transferred*/)
  75. {
  76. // Take ownership of the handler object.
  77. reactive_socket_recvmsg_op* o(
  78. static_cast<reactive_socket_recvmsg_op*>(base));
  79. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  80. BOOST_ASIO_HANDLER_COMPLETION((o));
  81. // Make a copy of the handler so that the memory can be deallocated before
  82. // the upcall is made. Even if we're not about to make an upcall, a
  83. // sub-object of the handler may be the true owner of the memory associated
  84. // with the handler. Consequently, a local copy of the handler is required
  85. // to ensure that any owning sub-object remains valid until after we have
  86. // deallocated the memory here.
  87. detail::binder2<Handler, boost::system::error_code, std::size_t>
  88. handler(o->handler_, o->ec_, o->bytes_transferred_);
  89. p.h = boost::asio::detail::addressof(handler.handler_);
  90. p.reset();
  91. // Make the upcall if required.
  92. if (owner)
  93. {
  94. fenced_block b(fenced_block::half);
  95. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  96. boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_);
  97. BOOST_ASIO_HANDLER_INVOCATION_END;
  98. }
  99. }
  100. private:
  101. Handler handler_;
  102. };
  103. } // namespace detail
  104. } // namespace asio
  105. } // namespace boost
  106. #include <boost/asio/detail/pop_options.hpp>
  107. #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP