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.

137 lines
4.4KB

  1. //
  2. // detail/resolve_endpoint_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_RESOLVER_ENDPOINT_OP_HPP
  11. #define ASIO_DETAIL_RESOLVER_ENDPOINT_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_results.hpp"
  18. #include "asio/detail/bind_handler.hpp"
  19. #include "asio/detail/fenced_block.hpp"
  20. #include "asio/detail/handler_alloc_helpers.hpp"
  21. #include "asio/detail/handler_invoke_helpers.hpp"
  22. #include "asio/detail/memory.hpp"
  23. #include "asio/detail/resolve_op.hpp"
  24. #include "asio/detail/socket_ops.hpp"
  25. #if defined(ASIO_HAS_IOCP)
  26. # include "asio/detail/win_iocp_io_context.hpp"
  27. #else // defined(ASIO_HAS_IOCP)
  28. # include "asio/detail/scheduler.hpp"
  29. #endif // defined(ASIO_HAS_IOCP)
  30. #include "asio/detail/push_options.hpp"
  31. namespace asio {
  32. namespace detail {
  33. template <typename Protocol, typename Handler, typename IoExecutor>
  34. class resolve_endpoint_op : public resolve_op
  35. {
  36. public:
  37. ASIO_DEFINE_HANDLER_PTR(resolve_endpoint_op);
  38. typedef typename Protocol::endpoint endpoint_type;
  39. typedef asio::ip::basic_resolver_results<Protocol> results_type;
  40. #if defined(ASIO_HAS_IOCP)
  41. typedef class win_iocp_io_context scheduler_impl;
  42. #else
  43. typedef class scheduler scheduler_impl;
  44. #endif
  45. resolve_endpoint_op(socket_ops::weak_cancel_token_type cancel_token,
  46. const endpoint_type& endpoint, scheduler_impl& sched,
  47. Handler& handler, const IoExecutor& io_ex)
  48. : resolve_op(&resolve_endpoint_op::do_complete),
  49. cancel_token_(cancel_token),
  50. endpoint_(endpoint),
  51. scheduler_(sched),
  52. handler_(ASIO_MOVE_CAST(Handler)(handler)),
  53. io_executor_(io_ex)
  54. {
  55. handler_work<Handler, IoExecutor>::start(handler_, io_executor_);
  56. }
  57. static void do_complete(void* owner, operation* base,
  58. const asio::error_code& /*ec*/,
  59. std::size_t /*bytes_transferred*/)
  60. {
  61. // Take ownership of the operation object.
  62. resolve_endpoint_op* o(static_cast<resolve_endpoint_op*>(base));
  63. ptr p = { asio::detail::addressof(o->handler_), o, o };
  64. handler_work<Handler, IoExecutor> w(o->handler_, o->io_executor_);
  65. if (owner && owner != &o->scheduler_)
  66. {
  67. // The operation is being run on the worker io_context. Time to perform
  68. // the resolver operation.
  69. // Perform the blocking endpoint resolution operation.
  70. char host_name[NI_MAXHOST];
  71. char service_name[NI_MAXSERV];
  72. socket_ops::background_getnameinfo(o->cancel_token_, o->endpoint_.data(),
  73. o->endpoint_.size(), host_name, NI_MAXHOST, service_name, NI_MAXSERV,
  74. o->endpoint_.protocol().type(), o->ec_);
  75. o->results_ = results_type::create(o->endpoint_, host_name, service_name);
  76. // Pass operation back to main io_context for completion.
  77. o->scheduler_.post_deferred_completion(o);
  78. p.v = p.p = 0;
  79. }
  80. else
  81. {
  82. // The operation has been returned to the main io_context. The completion
  83. // handler is ready to be delivered.
  84. ASIO_HANDLER_COMPLETION((*o));
  85. // Make a copy of the handler so that the memory can be deallocated
  86. // before the upcall is made. Even if we're not about to make an upcall,
  87. // a sub-object of the handler may be the true owner of the memory
  88. // associated with the handler. Consequently, a local copy of the handler
  89. // is required to ensure that any owning sub-object remains valid until
  90. // after we have deallocated the memory here.
  91. detail::binder2<Handler, asio::error_code, results_type>
  92. handler(o->handler_, o->ec_, o->results_);
  93. p.h = asio::detail::addressof(handler.handler_);
  94. p.reset();
  95. if (owner)
  96. {
  97. fenced_block b(fenced_block::half);
  98. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  99. w.complete(handler, handler.handler_);
  100. ASIO_HANDLER_INVOCATION_END;
  101. }
  102. }
  103. }
  104. private:
  105. socket_ops::weak_cancel_token_type cancel_token_;
  106. endpoint_type endpoint_;
  107. scheduler_impl& scheduler_;
  108. Handler handler_;
  109. IoExecutor io_executor_;
  110. results_type results_;
  111. };
  112. } // namespace detail
  113. } // namespace asio
  114. #include "asio/detail/pop_options.hpp"
  115. #endif // ASIO_DETAIL_RESOLVER_ENDPOINT_OP_HPP