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
3.2KB

  1. //
  2. // local/detail/impl/endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Derived from a public domain implementation written by Daniel Casimiro.
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
  12. #define ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include "asio/detail/config.hpp"
  17. #if defined(ASIO_HAS_LOCAL_SOCKETS)
  18. #include <cstring>
  19. #include "asio/detail/socket_ops.hpp"
  20. #include "asio/detail/throw_error.hpp"
  21. #include "asio/error.hpp"
  22. #include "asio/local/detail/endpoint.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. namespace local {
  26. namespace detail {
  27. endpoint::endpoint()
  28. {
  29. init("", 0);
  30. }
  31. endpoint::endpoint(const char* path_name)
  32. {
  33. using namespace std; // For strlen.
  34. init(path_name, strlen(path_name));
  35. }
  36. endpoint::endpoint(const std::string& path_name)
  37. {
  38. init(path_name.data(), path_name.length());
  39. }
  40. #if defined(ASIO_HAS_STRING_VIEW)
  41. endpoint::endpoint(string_view path_name)
  42. {
  43. init(path_name.data(), path_name.length());
  44. }
  45. #endif // defined(ASIO_HAS_STRING_VIEW)
  46. void endpoint::resize(std::size_t new_size)
  47. {
  48. if (new_size > sizeof(asio::detail::sockaddr_un_type))
  49. {
  50. asio::error_code ec(asio::error::invalid_argument);
  51. asio::detail::throw_error(ec);
  52. }
  53. else if (new_size == 0)
  54. {
  55. path_length_ = 0;
  56. }
  57. else
  58. {
  59. path_length_ = new_size
  60. - offsetof(asio::detail::sockaddr_un_type, sun_path);
  61. // The path returned by the operating system may be NUL-terminated.
  62. if (path_length_ > 0 && data_.local.sun_path[path_length_ - 1] == 0)
  63. --path_length_;
  64. }
  65. }
  66. std::string endpoint::path() const
  67. {
  68. return std::string(data_.local.sun_path, path_length_);
  69. }
  70. void endpoint::path(const char* p)
  71. {
  72. using namespace std; // For strlen.
  73. init(p, strlen(p));
  74. }
  75. void endpoint::path(const std::string& p)
  76. {
  77. init(p.data(), p.length());
  78. }
  79. bool operator==(const endpoint& e1, const endpoint& e2)
  80. {
  81. return e1.path() == e2.path();
  82. }
  83. bool operator<(const endpoint& e1, const endpoint& e2)
  84. {
  85. return e1.path() < e2.path();
  86. }
  87. void endpoint::init(const char* path_name, std::size_t path_length)
  88. {
  89. if (path_length > sizeof(data_.local.sun_path) - 1)
  90. {
  91. // The buffer is not large enough to store this address.
  92. asio::error_code ec(asio::error::name_too_long);
  93. asio::detail::throw_error(ec);
  94. }
  95. using namespace std; // For memcpy.
  96. data_.local = asio::detail::sockaddr_un_type();
  97. data_.local.sun_family = AF_UNIX;
  98. if (path_length > 0)
  99. memcpy(data_.local.sun_path, path_name, path_length);
  100. path_length_ = path_length;
  101. // NUL-terminate normal path names. Names that start with a NUL are in the
  102. // UNIX domain protocol's "abstract namespace" and are not NUL-terminated.
  103. if (path_length > 0 && data_.local.sun_path[0] == 0)
  104. data_.local.sun_path[path_length] = 0;
  105. }
  106. } // namespace detail
  107. } // namespace local
  108. } // namespace asio
  109. #include "asio/detail/pop_options.hpp"
  110. #endif // defined(ASIO_HAS_LOCAL_SOCKETS)
  111. #endif // ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP