reactive_socket_recvmsg_op.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // detail/reactive_socket_recvmsg_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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_RECVMSG_OP_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include "asio/detail/addressof.hpp"
  17. #include "asio/detail/bind_handler.hpp"
  18. #include "asio/detail/buffer_sequence_adapter.hpp"
  19. #include "asio/detail/fenced_block.hpp"
  20. #include "asio/detail/reactor_op.hpp"
  21. #include "asio/detail/socket_ops.hpp"
  22. #include "asio/socket_base.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace detail {
  26. template <typename MutableBufferSequence>
  27. class reactive_socket_recvmsg_op_base : public reactor_op
  28. {
  29. public:
  30. reactive_socket_recvmsg_op_base(socket_type socket,
  31. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  32. socket_base::message_flags& out_flags, func_type complete_func)
  33. : reactor_op(&reactive_socket_recvmsg_op_base::do_perform, complete_func),
  34. socket_(socket),
  35. buffers_(buffers),
  36. in_flags_(in_flags),
  37. out_flags_(out_flags)
  38. {
  39. }
  40. static bool do_perform(reactor_op* base)
  41. {
  42. reactive_socket_recvmsg_op_base* o(
  43. static_cast<reactive_socket_recvmsg_op_base*>(base));
  44. buffer_sequence_adapter<asio::mutable_buffer,
  45. MutableBufferSequence> bufs(o->buffers_);
  46. return socket_ops::non_blocking_recvmsg(o->socket_,
  47. bufs.buffers(), bufs.count(),
  48. o->in_flags_, o->out_flags_,
  49. o->ec_, o->bytes_transferred_);
  50. }
  51. private:
  52. socket_type socket_;
  53. MutableBufferSequence buffers_;
  54. socket_base::message_flags in_flags_;
  55. socket_base::message_flags& out_flags_;
  56. };
  57. template <typename MutableBufferSequence, typename Handler>
  58. class reactive_socket_recvmsg_op :
  59. public reactive_socket_recvmsg_op_base<MutableBufferSequence>
  60. {
  61. public:
  62. ASIO_DEFINE_HANDLER_PTR(reactive_socket_recvmsg_op);
  63. reactive_socket_recvmsg_op(socket_type socket,
  64. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  65. socket_base::message_flags& out_flags, Handler& handler)
  66. : reactive_socket_recvmsg_op_base<MutableBufferSequence>(socket, buffers,
  67. in_flags, out_flags, &reactive_socket_recvmsg_op::do_complete),
  68. handler_(ASIO_MOVE_CAST(Handler)(handler))
  69. {
  70. }
  71. static void do_complete(io_service_impl* owner, operation* base,
  72. const asio::error_code& /*ec*/,
  73. std::size_t /*bytes_transferred*/)
  74. {
  75. // Take ownership of the handler object.
  76. reactive_socket_recvmsg_op* o(
  77. static_cast<reactive_socket_recvmsg_op*>(base));
  78. ptr p = { asio::detail::addressof(o->handler_), o, o };
  79. ASIO_HANDLER_COMPLETION((o));
  80. // Make a copy of the handler so that the memory can be deallocated before
  81. // the upcall is made. Even if we're not about to make an upcall, a
  82. // sub-object of the handler may be the true owner of the memory associated
  83. // with the handler. Consequently, a local copy of the handler is required
  84. // to ensure that any owning sub-object remains valid until after we have
  85. // deallocated the memory here.
  86. detail::binder2<Handler, asio::error_code, std::size_t>
  87. handler(o->handler_, o->ec_, o->bytes_transferred_);
  88. p.h = asio::detail::addressof(handler.handler_);
  89. p.reset();
  90. // Make the upcall if required.
  91. if (owner)
  92. {
  93. fenced_block b(fenced_block::half);
  94. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  95. asio_handler_invoke_helpers::invoke(handler, handler.handler_);
  96. ASIO_HANDLER_INVOCATION_END;
  97. }
  98. }
  99. private:
  100. Handler handler_;
  101. };
  102. } // namespace detail
  103. } // namespace asio
  104. #include "asio/detail/pop_options.hpp"
  105. #endif // ASIO_DETAIL_REACTIVE_SOCKET_RECVMSG_OP_HPP