reactive_socket_recvfrom_op.hpp 4.3 KB

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