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.

129 lines
3.0KB

  1. //
  2. // local/detail/impl/endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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. void endpoint::resize(std::size_t new_size)
  41. {
  42. if (new_size > sizeof(asio::detail::sockaddr_un_type))
  43. {
  44. asio::error_code ec(asio::error::invalid_argument);
  45. asio::detail::throw_error(ec);
  46. }
  47. else if (new_size == 0)
  48. {
  49. path_length_ = 0;
  50. }
  51. else
  52. {
  53. path_length_ = new_size
  54. - offsetof(asio::detail::sockaddr_un_type, sun_path);
  55. // The path returned by the operating system may be NUL-terminated.
  56. if (path_length_ > 0 && data_.local.sun_path[path_length_ - 1] == 0)
  57. --path_length_;
  58. }
  59. }
  60. std::string endpoint::path() const
  61. {
  62. return std::string(data_.local.sun_path, path_length_);
  63. }
  64. void endpoint::path(const char* p)
  65. {
  66. using namespace std; // For strlen.
  67. init(p, strlen(p));
  68. }
  69. void endpoint::path(const std::string& p)
  70. {
  71. init(p.data(), p.length());
  72. }
  73. bool operator==(const endpoint& e1, const endpoint& e2)
  74. {
  75. return e1.path() == e2.path();
  76. }
  77. bool operator<(const endpoint& e1, const endpoint& e2)
  78. {
  79. return e1.path() < e2.path();
  80. }
  81. void endpoint::init(const char* path_name, std::size_t path_length)
  82. {
  83. if (path_length > sizeof(data_.local.sun_path) - 1)
  84. {
  85. // The buffer is not large enough to store this address.
  86. asio::error_code ec(asio::error::name_too_long);
  87. asio::detail::throw_error(ec);
  88. }
  89. using namespace std; // For memcpy.
  90. data_.local = asio::detail::sockaddr_un_type();
  91. data_.local.sun_family = AF_UNIX;
  92. memcpy(data_.local.sun_path, path_name, path_length);
  93. path_length_ = path_length;
  94. // NUL-terminate normal path names. Names that start with a NUL are in the
  95. // UNIX domain protocol's "abstract namespace" and are not NUL-terminated.
  96. if (path_length > 0 && data_.local.sun_path[0] == 0)
  97. data_.local.sun_path[path_length] = 0;
  98. }
  99. } // namespace detail
  100. } // namespace local
  101. } // namespace asio
  102. #include "asio/detail/pop_options.hpp"
  103. #endif // defined(ASIO_HAS_LOCAL_SOCKETS)
  104. #endif // ASIO_LOCAL_DETAIL_IMPL_ENDPOINT_IPP