endpoint.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // local/detail/endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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 BOOST_ASIO_LOCAL_DETAIL_ENDPOINT_HPP
  12. #define BOOST_ASIO_LOCAL_DETAIL_ENDPOINT_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
  18. #include <cstddef>
  19. #include <string>
  20. #include <boost/asio/detail/socket_types.hpp>
  21. #include <boost/asio/detail/string_view.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. namespace local {
  26. namespace detail {
  27. // Helper class for implementing a UNIX domain endpoint.
  28. class endpoint
  29. {
  30. public:
  31. // Default constructor.
  32. BOOST_ASIO_DECL endpoint();
  33. // Construct an endpoint using the specified path name.
  34. BOOST_ASIO_DECL endpoint(const char* path_name);
  35. // Construct an endpoint using the specified path name.
  36. BOOST_ASIO_DECL endpoint(const std::string& path_name);
  37. #if defined(BOOST_ASIO_HAS_STRING_VIEW)
  38. // Construct an endpoint using the specified path name.
  39. BOOST_ASIO_DECL endpoint(string_view path_name);
  40. #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
  41. // Copy constructor.
  42. endpoint(const endpoint& other)
  43. : data_(other.data_),
  44. path_length_(other.path_length_)
  45. {
  46. }
  47. // Assign from another endpoint.
  48. endpoint& operator=(const endpoint& other)
  49. {
  50. data_ = other.data_;
  51. path_length_ = other.path_length_;
  52. return *this;
  53. }
  54. // Get the underlying endpoint in the native type.
  55. boost::asio::detail::socket_addr_type* data()
  56. {
  57. return &data_.base;
  58. }
  59. // Get the underlying endpoint in the native type.
  60. const boost::asio::detail::socket_addr_type* data() const
  61. {
  62. return &data_.base;
  63. }
  64. // Get the underlying size of the endpoint in the native type.
  65. std::size_t size() const
  66. {
  67. return path_length_
  68. + offsetof(boost::asio::detail::sockaddr_un_type, sun_path);
  69. }
  70. // Set the underlying size of the endpoint in the native type.
  71. BOOST_ASIO_DECL void resize(std::size_t size);
  72. // Get the capacity of the endpoint in the native type.
  73. std::size_t capacity() const
  74. {
  75. return sizeof(boost::asio::detail::sockaddr_un_type);
  76. }
  77. // Get the path associated with the endpoint.
  78. BOOST_ASIO_DECL std::string path() const;
  79. // Set the path associated with the endpoint.
  80. BOOST_ASIO_DECL void path(const char* p);
  81. // Set the path associated with the endpoint.
  82. BOOST_ASIO_DECL void path(const std::string& p);
  83. // Compare two endpoints for equality.
  84. BOOST_ASIO_DECL friend bool operator==(
  85. const endpoint& e1, const endpoint& e2);
  86. // Compare endpoints for ordering.
  87. BOOST_ASIO_DECL friend bool operator<(
  88. const endpoint& e1, const endpoint& e2);
  89. private:
  90. // The underlying UNIX socket address.
  91. union data_union
  92. {
  93. boost::asio::detail::socket_addr_type base;
  94. boost::asio::detail::sockaddr_un_type local;
  95. } data_;
  96. // The length of the path associated with the endpoint.
  97. std::size_t path_length_;
  98. // Initialise with a specified path.
  99. BOOST_ASIO_DECL void init(const char* path, std::size_t path_length);
  100. };
  101. } // namespace detail
  102. } // namespace local
  103. } // namespace asio
  104. } // namespace boost
  105. #include <boost/asio/detail/pop_options.hpp>
  106. #if defined(BOOST_ASIO_HEADER_ONLY)
  107. # include <boost/asio/local/detail/impl/endpoint.ipp>
  108. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  109. #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
  110. #endif // BOOST_ASIO_LOCAL_DETAIL_ENDPOINT_HPP