reactive_socket_sendto_op.hpp 4.3 KB

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