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.

200 lines
5.3KB

  1. //
  2. // ip/detail/impl/endpoint.ipp
  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_IP_DETAIL_IMPL_ENDPOINT_IPP
  11. #define ASIO_IP_DETAIL_IMPL_ENDPOINT_IPP
  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 <cstring>
  17. #if !defined(ASIO_NO_IOSTREAM)
  18. # include <sstream>
  19. #endif // !defined(ASIO_NO_IOSTREAM)
  20. #include "asio/detail/socket_ops.hpp"
  21. #include "asio/detail/throw_error.hpp"
  22. #include "asio/error.hpp"
  23. #include "asio/ip/detail/endpoint.hpp"
  24. #include "asio/detail/push_options.hpp"
  25. namespace asio {
  26. namespace ip {
  27. namespace detail {
  28. endpoint::endpoint() ASIO_NOEXCEPT
  29. : data_()
  30. {
  31. data_.v4.sin_family = ASIO_OS_DEF(AF_INET);
  32. data_.v4.sin_port = 0;
  33. data_.v4.sin_addr.s_addr = ASIO_OS_DEF(INADDR_ANY);
  34. }
  35. endpoint::endpoint(int family, unsigned short port_num) ASIO_NOEXCEPT
  36. : data_()
  37. {
  38. using namespace std; // For memcpy.
  39. if (family == ASIO_OS_DEF(AF_INET))
  40. {
  41. data_.v4.sin_family = ASIO_OS_DEF(AF_INET);
  42. data_.v4.sin_port =
  43. asio::detail::socket_ops::host_to_network_short(port_num);
  44. data_.v4.sin_addr.s_addr = ASIO_OS_DEF(INADDR_ANY);
  45. }
  46. else
  47. {
  48. data_.v6.sin6_family = ASIO_OS_DEF(AF_INET6);
  49. data_.v6.sin6_port =
  50. asio::detail::socket_ops::host_to_network_short(port_num);
  51. data_.v6.sin6_flowinfo = 0;
  52. data_.v6.sin6_addr.s6_addr[0] = 0; data_.v6.sin6_addr.s6_addr[1] = 0;
  53. data_.v6.sin6_addr.s6_addr[2] = 0; data_.v6.sin6_addr.s6_addr[3] = 0;
  54. data_.v6.sin6_addr.s6_addr[4] = 0; data_.v6.sin6_addr.s6_addr[5] = 0;
  55. data_.v6.sin6_addr.s6_addr[6] = 0; data_.v6.sin6_addr.s6_addr[7] = 0;
  56. data_.v6.sin6_addr.s6_addr[8] = 0; data_.v6.sin6_addr.s6_addr[9] = 0;
  57. data_.v6.sin6_addr.s6_addr[10] = 0; data_.v6.sin6_addr.s6_addr[11] = 0;
  58. data_.v6.sin6_addr.s6_addr[12] = 0; data_.v6.sin6_addr.s6_addr[13] = 0;
  59. data_.v6.sin6_addr.s6_addr[14] = 0; data_.v6.sin6_addr.s6_addr[15] = 0;
  60. data_.v6.sin6_scope_id = 0;
  61. }
  62. }
  63. endpoint::endpoint(const asio::ip::address& addr,
  64. unsigned short port_num) ASIO_NOEXCEPT
  65. : data_()
  66. {
  67. using namespace std; // For memcpy.
  68. if (addr.is_v4())
  69. {
  70. data_.v4.sin_family = ASIO_OS_DEF(AF_INET);
  71. data_.v4.sin_port =
  72. asio::detail::socket_ops::host_to_network_short(port_num);
  73. data_.v4.sin_addr.s_addr =
  74. asio::detail::socket_ops::host_to_network_long(
  75. addr.to_v4().to_uint());
  76. }
  77. else
  78. {
  79. data_.v6.sin6_family = ASIO_OS_DEF(AF_INET6);
  80. data_.v6.sin6_port =
  81. asio::detail::socket_ops::host_to_network_short(port_num);
  82. data_.v6.sin6_flowinfo = 0;
  83. asio::ip::address_v6 v6_addr = addr.to_v6();
  84. asio::ip::address_v6::bytes_type bytes = v6_addr.to_bytes();
  85. memcpy(data_.v6.sin6_addr.s6_addr, bytes.data(), 16);
  86. data_.v6.sin6_scope_id =
  87. static_cast<asio::detail::u_long_type>(
  88. v6_addr.scope_id());
  89. }
  90. }
  91. void endpoint::resize(std::size_t new_size)
  92. {
  93. if (new_size > sizeof(asio::detail::sockaddr_storage_type))
  94. {
  95. asio::error_code ec(asio::error::invalid_argument);
  96. asio::detail::throw_error(ec);
  97. }
  98. }
  99. unsigned short endpoint::port() const ASIO_NOEXCEPT
  100. {
  101. if (is_v4())
  102. {
  103. return asio::detail::socket_ops::network_to_host_short(
  104. data_.v4.sin_port);
  105. }
  106. else
  107. {
  108. return asio::detail::socket_ops::network_to_host_short(
  109. data_.v6.sin6_port);
  110. }
  111. }
  112. void endpoint::port(unsigned short port_num) ASIO_NOEXCEPT
  113. {
  114. if (is_v4())
  115. {
  116. data_.v4.sin_port
  117. = asio::detail::socket_ops::host_to_network_short(port_num);
  118. }
  119. else
  120. {
  121. data_.v6.sin6_port
  122. = asio::detail::socket_ops::host_to_network_short(port_num);
  123. }
  124. }
  125. asio::ip::address endpoint::address() const ASIO_NOEXCEPT
  126. {
  127. using namespace std; // For memcpy.
  128. if (is_v4())
  129. {
  130. return asio::ip::address_v4(
  131. asio::detail::socket_ops::network_to_host_long(
  132. data_.v4.sin_addr.s_addr));
  133. }
  134. else
  135. {
  136. asio::ip::address_v6::bytes_type bytes;
  137. #if defined(ASIO_HAS_STD_ARRAY)
  138. memcpy(bytes.data(), data_.v6.sin6_addr.s6_addr, 16);
  139. #else // defined(ASIO_HAS_STD_ARRAY)
  140. memcpy(bytes.elems, data_.v6.sin6_addr.s6_addr, 16);
  141. #endif // defined(ASIO_HAS_STD_ARRAY)
  142. return asio::ip::address_v6(bytes, data_.v6.sin6_scope_id);
  143. }
  144. }
  145. void endpoint::address(const asio::ip::address& addr) ASIO_NOEXCEPT
  146. {
  147. endpoint tmp_endpoint(addr, port());
  148. data_ = tmp_endpoint.data_;
  149. }
  150. bool operator==(const endpoint& e1, const endpoint& e2) ASIO_NOEXCEPT
  151. {
  152. return e1.address() == e2.address() && e1.port() == e2.port();
  153. }
  154. bool operator<(const endpoint& e1, const endpoint& e2) ASIO_NOEXCEPT
  155. {
  156. if (e1.address() < e2.address())
  157. return true;
  158. if (e1.address() != e2.address())
  159. return false;
  160. return e1.port() < e2.port();
  161. }
  162. #if !defined(ASIO_NO_IOSTREAM)
  163. std::string endpoint::to_string() const
  164. {
  165. std::ostringstream tmp_os;
  166. tmp_os.imbue(std::locale::classic());
  167. if (is_v4())
  168. tmp_os << address();
  169. else
  170. tmp_os << '[' << address() << ']';
  171. tmp_os << ':' << port();
  172. return tmp_os.str();
  173. }
  174. #endif // !defined(ASIO_NO_IOSTREAM)
  175. } // namespace detail
  176. } // namespace ip
  177. } // namespace asio
  178. #include "asio/detail/pop_options.hpp"
  179. #endif // ASIO_IP_DETAIL_IMPL_ENDPOINT_IPP