reactive_socket_connect_op.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // detail/reactive_socket_connect_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_CONNECT_OP_HPP
  11. #define ASIO_DETAIL_REACTIVE_SOCKET_CONNECT_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/fenced_block.hpp"
  18. #include "asio/detail/handler_alloc_helpers.hpp"
  19. #include "asio/detail/handler_work.hpp"
  20. #include "asio/detail/memory.hpp"
  21. #include "asio/detail/reactor_op.hpp"
  22. #include "asio/detail/socket_ops.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace detail {
  26. class reactive_socket_connect_op_base : public reactor_op
  27. {
  28. public:
  29. reactive_socket_connect_op_base(const asio::error_code& success_ec,
  30. socket_type socket, func_type complete_func)
  31. : reactor_op(success_ec,
  32. &reactive_socket_connect_op_base::do_perform, complete_func),
  33. socket_(socket)
  34. {
  35. }
  36. static status do_perform(reactor_op* base)
  37. {
  38. ASIO_ASSUME(base != 0);
  39. reactive_socket_connect_op_base* o(
  40. static_cast<reactive_socket_connect_op_base*>(base));
  41. status result = socket_ops::non_blocking_connect(
  42. o->socket_, o->ec_) ? done : not_done;
  43. ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_connect", o->ec_));
  44. return result;
  45. }
  46. private:
  47. socket_type socket_;
  48. };
  49. template <typename Handler, typename IoExecutor>
  50. class reactive_socket_connect_op : public reactive_socket_connect_op_base
  51. {
  52. public:
  53. typedef Handler handler_type;
  54. typedef IoExecutor io_executor_type;
  55. ASIO_DEFINE_HANDLER_PTR(reactive_socket_connect_op);
  56. reactive_socket_connect_op(const asio::error_code& success_ec,
  57. socket_type socket, Handler& handler, const IoExecutor& io_ex)
  58. : reactive_socket_connect_op_base(success_ec, socket,
  59. &reactive_socket_connect_op::do_complete),
  60. handler_(static_cast<Handler&&>(handler)),
  61. work_(handler_, io_ex)
  62. {
  63. }
  64. static void do_complete(void* owner, operation* base,
  65. const asio::error_code& /*ec*/,
  66. std::size_t /*bytes_transferred*/)
  67. {
  68. // Take ownership of the handler object.
  69. ASIO_ASSUME(base != 0);
  70. reactive_socket_connect_op* o
  71. (static_cast<reactive_socket_connect_op*>(base));
  72. ptr p = { asio::detail::addressof(o->handler_), o, o };
  73. ASIO_HANDLER_COMPLETION((*o));
  74. // Take ownership of the operation's outstanding work.
  75. handler_work<Handler, IoExecutor> w(
  76. static_cast<handler_work<Handler, IoExecutor>&&>(
  77. o->work_));
  78. ASIO_ERROR_LOCATION(o->ec_);
  79. // Make a copy of the handler so that the memory can be deallocated before
  80. // the upcall is made. Even if we're not about to make an upcall, a
  81. // sub-object of the handler may be the true owner of the memory associated
  82. // with the handler. Consequently, a local copy of the handler is required
  83. // to ensure that any owning sub-object remains valid until after we have
  84. // deallocated the memory here.
  85. detail::binder1<Handler, asio::error_code>
  86. handler(o->handler_, o->ec_);
  87. p.h = asio::detail::addressof(handler.handler_);
  88. p.reset();
  89. // Make the upcall if required.
  90. if (owner)
  91. {
  92. fenced_block b(fenced_block::half);
  93. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  94. w.complete(handler, handler.handler_);
  95. ASIO_HANDLER_INVOCATION_END;
  96. }
  97. }
  98. static void do_immediate(operation* base, bool, const void* io_ex)
  99. {
  100. // Take ownership of the handler object.
  101. ASIO_ASSUME(base != 0);
  102. reactive_socket_connect_op* o
  103. (static_cast<reactive_socket_connect_op*>(base));
  104. ptr p = { asio::detail::addressof(o->handler_), o, o };
  105. ASIO_HANDLER_COMPLETION((*o));
  106. // Take ownership of the operation's outstanding work.
  107. immediate_handler_work<Handler, IoExecutor> w(
  108. static_cast<handler_work<Handler, IoExecutor>&&>(
  109. o->work_));
  110. ASIO_ERROR_LOCATION(o->ec_);
  111. // Make a copy of the handler so that the memory can be deallocated before
  112. // the upcall is made. Even if we're not about to make an upcall, a
  113. // sub-object of the handler may be the true owner of the memory associated
  114. // with the handler. Consequently, a local copy of the handler is required
  115. // to ensure that any owning sub-object remains valid until after we have
  116. // deallocated the memory here.
  117. detail::binder1<Handler, asio::error_code>
  118. handler(o->handler_, o->ec_);
  119. p.h = asio::detail::addressof(handler.handler_);
  120. p.reset();
  121. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  122. w.complete(handler, handler.handler_, io_ex);
  123. ASIO_HANDLER_INVOCATION_END;
  124. }
  125. private:
  126. Handler handler_;
  127. handler_work<Handler, IoExecutor> work_;
  128. };
  129. } // namespace detail
  130. } // namespace asio
  131. #include "asio/detail/pop_options.hpp"
  132. #endif // ASIO_DETAIL_REACTIVE_SOCKET_CONNECT_OP_HPP