5 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 // Derived from a public domain implementation written by Daniel Casimiro.
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)
12 #ifndef BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP
13 #define BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
17 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/asio/detail/push_options.hpp>
21 #include <boost/asio/detail/push_options.hpp>
22 #include <boost/throw_exception.hpp>
26 #include <boost/system/system_error.hpp>
27 #include <boost/asio/detail/pop_options.hpp>
29 #include <boost/asio/error.hpp>
30 #include <boost/asio/detail/socket_ops.hpp>
31 #include <boost/asio/detail/socket_types.hpp>
32 #include <boost/asio/detail/throw_error.hpp>
34 #if !defined(BOOST_ASIO_DISABLE_LOCAL_SOCKETS)
35 # if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
36 # define BOOST_ASIO_HAS_LOCAL_SOCKETS 1
37 # endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
38 #endif // !defined(BOOST_ASIO_DISABLE_LOCAL_SOCKETS)
40 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) \
41 || defined(GENERATING_DOCUMENTATION)
48 /// Describes an endpoint for a UNIX socket.
50 * The boost::asio::local::basic_endpoint class template describes an endpoint
51 * that may be associated with a particular UNIX socket.
54 * @e Distinct @e objects: Safe.@n
55 * @e Shared @e objects: Unsafe.
60 template <typename Protocol
>
64 /// The protocol type associated with the endpoint.
65 typedef Protocol protocol_type
;
67 /// The type of the endpoint structure. This type is dependent on the
68 /// underlying implementation of the socket layer.
69 #if defined(GENERATING_DOCUMENTATION)
70 typedef implementation_defined data_type
;
72 typedef boost::asio::detail::socket_addr_type data_type
;
75 /// Default constructor.
81 /// Construct an endpoint using the specified path name.
82 basic_endpoint(const char* path
)
84 using namespace std
; // For strlen.
85 init(path
, strlen(path
));
88 /// Construct an endpoint using the specified path name.
89 basic_endpoint(const std::string
& path
)
91 init(path
.data(), path
.length());
95 basic_endpoint(const basic_endpoint
& other
)
97 path_length_(other
.path_length_
)
101 /// Assign from another endpoint.
102 basic_endpoint
& operator=(const basic_endpoint
& other
)
105 path_length_
= other
.path_length_
;
109 /// The protocol associated with the endpoint.
110 protocol_type
protocol() const
112 return protocol_type();
115 /// Get the underlying endpoint in the native type.
121 /// Get the underlying endpoint in the native type.
122 const data_type
* data() const
127 /// Get the underlying size of the endpoint in the native type.
128 std::size_t size() const
131 + offsetof(boost::asio::detail::sockaddr_un_type
, sun_path
);
134 /// Set the underlying size of the endpoint in the native type.
135 void resize(std::size_t size
)
137 if (size
> sizeof(boost::asio::detail::sockaddr_un_type
))
139 boost::system::system_error
e(boost::asio::error::invalid_argument
);
140 boost::throw_exception(e
);
149 - offsetof(boost::asio::detail::sockaddr_un_type
, sun_path
);
151 // The path returned by the operating system may be NUL-terminated.
152 if (path_length_
> 0 && data_
.local
.sun_path
[path_length_
- 1] == 0)
157 /// Get the capacity of the endpoint in the native type.
158 std::size_t capacity() const
160 return sizeof(boost::asio::detail::sockaddr_un_type
);
163 /// Get the path associated with the endpoint.
164 std::string
path() const
166 return std::string(data_
.local
.sun_path
, path_length_
);
169 /// Set the path associated with the endpoint.
170 void path(const char* p
)
172 using namespace std
; // For strlen.
176 /// Set the path associated with the endpoint.
177 void path(const std::string
& p
)
179 init(p
.data(), p
.length());
182 /// Compare two endpoints for equality.
183 friend bool operator==(const basic_endpoint
<Protocol
>& e1
,
184 const basic_endpoint
<Protocol
>& e2
)
186 return e1
.path() == e2
.path();
189 /// Compare two endpoints for inequality.
190 friend bool operator!=(const basic_endpoint
<Protocol
>& e1
,
191 const basic_endpoint
<Protocol
>& e2
)
193 return e1
.path() != e2
.path();
196 /// Compare endpoints for ordering.
197 friend bool operator<(const basic_endpoint
<Protocol
>& e1
,
198 const basic_endpoint
<Protocol
>& e2
)
200 return e1
.path() < e2
.path();
204 // The underlying UNIX socket address.
207 boost::asio::detail::socket_addr_type base
;
208 boost::asio::detail::sockaddr_un_type local
;
211 // The length of the path associated with the endpoint.
212 std::size_t path_length_
;
214 // Initialise with a specified path.
215 void init(const char* path
, std::size_t path_length
)
217 if (path_length
> sizeof(data_
.local
.sun_path
) - 1)
219 // The buffer is not large enough to store this address.
220 boost::system::error_code
ec(boost::asio::error::name_too_long
);
221 boost::asio::detail::throw_error(ec
);
224 using namespace std
; // For memcpy.
225 data_
.local
= boost::asio::detail::sockaddr_un_type();
226 data_
.local
.sun_family
= AF_UNIX
;
227 memcpy(data_
.local
.sun_path
, path
, path_length
);
228 path_length_
= path_length
;
230 // NUL-terminate normal path names. Names that start with a NUL are in the
231 // UNIX domain protocol's "abstract namespace" and are not NUL-terminated.
232 if (path_length
> 0 && data_
.local
.sun_path
[0] == 0)
233 data_
.local
.sun_path
[path_length
] = 0;
237 /// Output an endpoint as a string.
239 * Used to output a human-readable string for a specified endpoint.
241 * @param os The output stream to which the string will be written.
243 * @param endpoint The endpoint to be written.
245 * @return The output stream.
247 * @relates boost::asio::local::basic_endpoint
249 template <typename Elem
, typename Traits
, typename Protocol
>
250 std::basic_ostream
<Elem
, Traits
>& operator<<(
251 std::basic_ostream
<Elem
, Traits
>& os
,
252 const basic_endpoint
<Protocol
>& endpoint
)
254 os
<< endpoint
.path();
262 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
263 // || defined(GENERATING_DOCUMENTATION)
265 #include <boost/asio/detail/pop_options.hpp>
267 #endif // BOOST_ASIO_LOCAL_BASIC_ENDPOINT_HPP