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.

124 lines
4.0KB

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