Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.6KB

  1. //
  2. // detail/resolve_query_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_RESOLVE_QUERY_OP_HPP
  11. #define ASIO_DETAIL_RESOLVE_QUERY_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/error.hpp"
  17. #include "asio/ip/basic_resolver_query.hpp"
  18. #include "asio/ip/basic_resolver_results.hpp"
  19. #include "asio/detail/bind_handler.hpp"
  20. #include "asio/detail/fenced_block.hpp"
  21. #include "asio/detail/handler_alloc_helpers.hpp"
  22. #include "asio/detail/handler_invoke_helpers.hpp"
  23. #include "asio/detail/memory.hpp"
  24. #include "asio/detail/resolve_op.hpp"
  25. #include "asio/detail/socket_ops.hpp"
  26. #if defined(ASIO_HAS_IOCP)
  27. # include "asio/detail/win_iocp_io_context.hpp"
  28. #else // defined(ASIO_HAS_IOCP)
  29. # include "asio/detail/scheduler.hpp"
  30. #endif // defined(ASIO_HAS_IOCP)
  31. #include "asio/detail/push_options.hpp"
  32. namespace asio {
  33. namespace detail {
  34. template <typename Protocol, typename Handler, typename IoExecutor>
  35. class resolve_query_op : public resolve_op
  36. {
  37. public:
  38. ASIO_DEFINE_HANDLER_PTR(resolve_query_op);
  39. typedef asio::ip::basic_resolver_query<Protocol> query_type;
  40. typedef asio::ip::basic_resolver_results<Protocol> results_type;
  41. #if defined(ASIO_HAS_IOCP)
  42. typedef class win_iocp_io_context scheduler_impl;
  43. #else
  44. typedef class scheduler scheduler_impl;
  45. #endif
  46. resolve_query_op(socket_ops::weak_cancel_token_type cancel_token,
  47. const query_type& query, scheduler_impl& sched,
  48. Handler& handler, const IoExecutor& io_ex)
  49. : resolve_op(&resolve_query_op::do_complete),
  50. cancel_token_(cancel_token),
  51. query_(query),
  52. scheduler_(sched),
  53. handler_(ASIO_MOVE_CAST(Handler)(handler)),
  54. io_executor_(io_ex),
  55. addrinfo_(0)
  56. {
  57. handler_work<Handler, IoExecutor>::start(handler_, io_executor_);
  58. }
  59. ~resolve_query_op()
  60. {
  61. if (addrinfo_)
  62. socket_ops::freeaddrinfo(addrinfo_);
  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 operation object.
  69. resolve_query_op* o(static_cast<resolve_query_op*>(base));
  70. ptr p = { asio::detail::addressof(o->handler_), o, o };
  71. if (owner && owner != &o->scheduler_)
  72. {
  73. // The operation is being run on the worker io_context. Time to perform
  74. // the resolver operation.
  75. // Perform the blocking host resolution operation.
  76. socket_ops::background_getaddrinfo(o->cancel_token_,
  77. o->query_.host_name().c_str(), o->query_.service_name().c_str(),
  78. o->query_.hints(), &o->addrinfo_, o->ec_);
  79. // Pass operation back to main io_context for completion.
  80. o->scheduler_.post_deferred_completion(o);
  81. p.v = p.p = 0;
  82. }
  83. else
  84. {
  85. // The operation has been returned to the main io_context. The completion
  86. // handler is ready to be delivered.
  87. // Take ownership of the operation's outstanding work.
  88. handler_work<Handler, IoExecutor> w(o->handler_, o->io_executor_);
  89. ASIO_HANDLER_COMPLETION((*o));
  90. // Make a copy of the handler so that the memory can be deallocated
  91. // before the upcall is made. Even if we're not about to make an upcall,
  92. // a sub-object of the handler may be the true owner of the memory
  93. // associated with the handler. Consequently, a local copy of the handler
  94. // is required to ensure that any owning sub-object remains valid until
  95. // after we have deallocated the memory here.
  96. detail::binder2<Handler, asio::error_code, results_type>
  97. handler(o->handler_, o->ec_, results_type());
  98. p.h = asio::detail::addressof(handler.handler_);
  99. if (o->addrinfo_)
  100. {
  101. handler.arg2_ = results_type::create(o->addrinfo_,
  102. o->query_.host_name(), o->query_.service_name());
  103. }
  104. p.reset();
  105. if (owner)
  106. {
  107. fenced_block b(fenced_block::half);
  108. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  109. w.complete(handler, handler.handler_);
  110. ASIO_HANDLER_INVOCATION_END;
  111. }
  112. }
  113. }
  114. private:
  115. socket_ops::weak_cancel_token_type cancel_token_;
  116. query_type query_;
  117. scheduler_impl& scheduler_;
  118. Handler handler_;
  119. IoExecutor io_executor_;
  120. asio::detail::addrinfo_type* addrinfo_;
  121. };
  122. } // namespace detail
  123. } // namespace asio
  124. #include "asio/detail/pop_options.hpp"
  125. #endif // ASIO_DETAIL_RESOLVE_QUERY_OP_HPP