reactive_socket_connect_op.hpp 5.3 KB

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