reactive_socket_send_op.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // detail/reactive_socket_send_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_SEND_OP_HPP
  11. #define ASIO_DETAIL_REACTIVE_SOCKET_SEND_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/bind_handler.hpp"
  17. #include "asio/detail/buffer_sequence_adapter.hpp"
  18. #include "asio/detail/fenced_block.hpp"
  19. #include "asio/detail/handler_alloc_helpers.hpp"
  20. #include "asio/detail/handler_work.hpp"
  21. #include "asio/detail/memory.hpp"
  22. #include "asio/detail/reactor_op.hpp"
  23. #include "asio/detail/socket_ops.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace detail {
  27. template <typename ConstBufferSequence>
  28. class reactive_socket_send_op_base : public reactor_op
  29. {
  30. public:
  31. reactive_socket_send_op_base(const asio::error_code& success_ec,
  32. socket_type socket, socket_ops::state_type state,
  33. const ConstBufferSequence& buffers,
  34. socket_base::message_flags flags, func_type complete_func)
  35. : reactor_op(success_ec,
  36. &reactive_socket_send_op_base::do_perform, complete_func),
  37. socket_(socket),
  38. state_(state),
  39. buffers_(buffers),
  40. flags_(flags)
  41. {
  42. }
  43. static status do_perform(reactor_op* base)
  44. {
  45. ASIO_ASSUME(base != 0);
  46. reactive_socket_send_op_base* o(
  47. static_cast<reactive_socket_send_op_base*>(base));
  48. typedef buffer_sequence_adapter<asio::const_buffer,
  49. ConstBufferSequence> bufs_type;
  50. status result;
  51. if (bufs_type::is_single_buffer)
  52. {
  53. result = socket_ops::non_blocking_send1(o->socket_,
  54. bufs_type::first(o->buffers_).data(),
  55. bufs_type::first(o->buffers_).size(), o->flags_,
  56. o->ec_, o->bytes_transferred_) ? done : not_done;
  57. if (result == done)
  58. if ((o->state_ & socket_ops::stream_oriented) != 0)
  59. if (o->bytes_transferred_ < bufs_type::first(o->buffers_).size())
  60. result = done_and_exhausted;
  61. }
  62. else
  63. {
  64. bufs_type bufs(o->buffers_);
  65. result = socket_ops::non_blocking_send(o->socket_,
  66. bufs.buffers(), bufs.count(), o->flags_,
  67. o->ec_, o->bytes_transferred_) ? done : not_done;
  68. if (result == done)
  69. if ((o->state_ & socket_ops::stream_oriented) != 0)
  70. if (o->bytes_transferred_ < bufs.total_size())
  71. result = done_and_exhausted;
  72. }
  73. ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_send",
  74. o->ec_, o->bytes_transferred_));
  75. return result;
  76. }
  77. private:
  78. socket_type socket_;
  79. socket_ops::state_type state_;
  80. ConstBufferSequence buffers_;
  81. socket_base::message_flags flags_;
  82. };
  83. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  84. class reactive_socket_send_op :
  85. public reactive_socket_send_op_base<ConstBufferSequence>
  86. {
  87. public:
  88. typedef Handler handler_type;
  89. typedef IoExecutor io_executor_type;
  90. ASIO_DEFINE_HANDLER_PTR(reactive_socket_send_op);
  91. reactive_socket_send_op(const asio::error_code& success_ec,
  92. socket_type socket, socket_ops::state_type state,
  93. const ConstBufferSequence& buffers, socket_base::message_flags flags,
  94. Handler& handler, const IoExecutor& io_ex)
  95. : reactive_socket_send_op_base<ConstBufferSequence>(success_ec, socket,
  96. state, buffers, flags, &reactive_socket_send_op::do_complete),
  97. handler_(static_cast<Handler&&>(handler)),
  98. work_(handler_, io_ex)
  99. {
  100. }
  101. static void do_complete(void* owner, operation* base,
  102. const asio::error_code& /*ec*/,
  103. std::size_t /*bytes_transferred*/)
  104. {
  105. // Take ownership of the handler object.
  106. ASIO_ASSUME(base != 0);
  107. reactive_socket_send_op* o(static_cast<reactive_socket_send_op*>(base));
  108. ptr p = { asio::detail::addressof(o->handler_), o, o };
  109. ASIO_HANDLER_COMPLETION((*o));
  110. // Take ownership of the operation's outstanding work.
  111. handler_work<Handler, IoExecutor> w(
  112. static_cast<handler_work<Handler, IoExecutor>&&>(
  113. o->work_));
  114. ASIO_ERROR_LOCATION(o->ec_);
  115. // Make a copy of the handler so that the memory can be deallocated before
  116. // the upcall is made. Even if we're not about to make an upcall, a
  117. // sub-object of the handler may be the true owner of the memory associated
  118. // with the handler. Consequently, a local copy of the handler is required
  119. // to ensure that any owning sub-object remains valid until after we have
  120. // deallocated the memory here.
  121. detail::binder2<Handler, asio::error_code, std::size_t>
  122. handler(o->handler_, o->ec_, o->bytes_transferred_);
  123. p.h = asio::detail::addressof(handler.handler_);
  124. p.reset();
  125. // Make the upcall if required.
  126. if (owner)
  127. {
  128. fenced_block b(fenced_block::half);
  129. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  130. w.complete(handler, handler.handler_);
  131. ASIO_HANDLER_INVOCATION_END;
  132. }
  133. }
  134. static void do_immediate(operation* base, bool, const void* io_ex)
  135. {
  136. // Take ownership of the handler object.
  137. ASIO_ASSUME(base != 0);
  138. reactive_socket_send_op* o(static_cast<reactive_socket_send_op*>(base));
  139. ptr p = { asio::detail::addressof(o->handler_), o, o };
  140. ASIO_HANDLER_COMPLETION((*o));
  141. // Take ownership of the operation's outstanding work.
  142. immediate_handler_work<Handler, IoExecutor> w(
  143. static_cast<handler_work<Handler, IoExecutor>&&>(
  144. o->work_));
  145. ASIO_ERROR_LOCATION(o->ec_);
  146. // Make a copy of the handler so that the memory can be deallocated before
  147. // the upcall is made. Even if we're not about to make an upcall, a
  148. // sub-object of the handler may be the true owner of the memory associated
  149. // with the handler. Consequently, a local copy of the handler is required
  150. // to ensure that any owning sub-object remains valid until after we have
  151. // deallocated the memory here.
  152. detail::binder2<Handler, asio::error_code, std::size_t>
  153. handler(o->handler_, o->ec_, o->bytes_transferred_);
  154. p.h = asio::detail::addressof(handler.handler_);
  155. p.reset();
  156. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  157. w.complete(handler, handler.handler_, io_ex);
  158. ASIO_HANDLER_INVOCATION_END;
  159. }
  160. private:
  161. Handler handler_;
  162. handler_work<Handler, IoExecutor> work_;
  163. };
  164. } // namespace detail
  165. } // namespace asio
  166. #include "asio/detail/pop_options.hpp"
  167. #endif // ASIO_DETAIL_REACTIVE_SOCKET_SEND_OP_HPP