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