reactive_socket_sendto_op.hpp 3.9 KB

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