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.

resolver_service.hpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // detail/resolver_service.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_SERVICE_HPP
  11. #define ASIO_DETAIL_RESOLVER_SERVICE_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. #if !defined(ASIO_WINDOWS_RUNTIME)
  17. #include "asio/ip/basic_resolver_query.hpp"
  18. #include "asio/ip/basic_resolver_results.hpp"
  19. #include "asio/detail/concurrency_hint.hpp"
  20. #include "asio/detail/memory.hpp"
  21. #include "asio/detail/resolve_endpoint_op.hpp"
  22. #include "asio/detail/resolve_query_op.hpp"
  23. #include "asio/detail/resolver_service_base.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace detail {
  27. template <typename Protocol>
  28. class resolver_service :
  29. public execution_context_service_base<resolver_service<Protocol> >,
  30. public resolver_service_base
  31. {
  32. public:
  33. // The implementation type of the resolver. A cancellation token is used to
  34. // indicate to the background thread that the operation has been cancelled.
  35. typedef socket_ops::shared_cancel_token_type implementation_type;
  36. // The endpoint type.
  37. typedef typename Protocol::endpoint endpoint_type;
  38. // The query type.
  39. typedef asio::ip::basic_resolver_query<Protocol> query_type;
  40. // The results type.
  41. typedef asio::ip::basic_resolver_results<Protocol> results_type;
  42. // Constructor.
  43. resolver_service(execution_context& context)
  44. : execution_context_service_base<resolver_service<Protocol> >(context),
  45. resolver_service_base(context)
  46. {
  47. }
  48. // Destroy all user-defined handler objects owned by the service.
  49. void shutdown()
  50. {
  51. this->base_shutdown();
  52. }
  53. // Perform any fork-related housekeeping.
  54. void notify_fork(execution_context::fork_event fork_ev)
  55. {
  56. this->base_notify_fork(fork_ev);
  57. }
  58. // Resolve a query to a list of entries.
  59. results_type resolve(implementation_type&, const query_type& query,
  60. asio::error_code& ec)
  61. {
  62. asio::detail::addrinfo_type* address_info = 0;
  63. socket_ops::getaddrinfo(query.host_name().c_str(),
  64. query.service_name().c_str(), query.hints(), &address_info, ec);
  65. auto_addrinfo auto_address_info(address_info);
  66. return ec ? results_type() : results_type::create(
  67. address_info, query.host_name(), query.service_name());
  68. }
  69. // Asynchronously resolve a query to a list of entries.
  70. template <typename Handler, typename IoExecutor>
  71. void async_resolve(implementation_type& impl, const query_type& query,
  72. Handler& handler, const IoExecutor& io_ex)
  73. {
  74. // Allocate and construct an operation to wrap the handler.
  75. typedef resolve_query_op<Protocol, Handler, IoExecutor> op;
  76. typename op::ptr p = { asio::detail::addressof(handler),
  77. op::ptr::allocate(handler), 0 };
  78. p.p = new (p.v) op(impl, query, scheduler_, handler, io_ex);
  79. ASIO_HANDLER_CREATION((scheduler_.context(),
  80. *p.p, "resolver", &impl, 0, "async_resolve"));
  81. start_resolve_op(p.p);
  82. p.v = p.p = 0;
  83. }
  84. // Resolve an endpoint to a list of entries.
  85. results_type resolve(implementation_type&,
  86. const endpoint_type& endpoint, asio::error_code& ec)
  87. {
  88. char host_name[NI_MAXHOST];
  89. char service_name[NI_MAXSERV];
  90. socket_ops::sync_getnameinfo(endpoint.data(), endpoint.size(),
  91. host_name, NI_MAXHOST, service_name, NI_MAXSERV,
  92. endpoint.protocol().type(), ec);
  93. return ec ? results_type() : results_type::create(
  94. endpoint, host_name, service_name);
  95. }
  96. // Asynchronously resolve an endpoint to a list of entries.
  97. template <typename Handler, typename IoExecutor>
  98. void async_resolve(implementation_type& impl, const endpoint_type& endpoint,
  99. Handler& handler, const IoExecutor& io_ex)
  100. {
  101. // Allocate and construct an operation to wrap the handler.
  102. typedef resolve_endpoint_op<Protocol, Handler, IoExecutor> op;
  103. typename op::ptr p = { asio::detail::addressof(handler),
  104. op::ptr::allocate(handler), 0 };
  105. p.p = new (p.v) op(impl, endpoint, scheduler_, handler, io_ex);
  106. ASIO_HANDLER_CREATION((scheduler_.context(),
  107. *p.p, "resolver", &impl, 0, "async_resolve"));
  108. start_resolve_op(p.p);
  109. p.v = p.p = 0;
  110. }
  111. };
  112. } // namespace detail
  113. } // namespace asio
  114. #include "asio/detail/pop_options.hpp"
  115. #endif // !defined(ASIO_WINDOWS_RUNTIME)
  116. #endif // ASIO_DETAIL_RESOLVER_SERVICE_HPP