1 | // |
2 | // ip/basic_endpoint.hpp |
3 | // ~~~~~~~~~~~~~~~~~~~~~ |
4 | // |
5 | // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
9 | // |
10 | |
11 | #ifndef BOOST_ASIO_IP_BASIC_ENDPOINT_HPP |
12 | #define BOOST_ASIO_IP_BASIC_ENDPOINT_HPP |
13 | |
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
15 | # pragma once |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) |
17 | |
18 | #include <boost/asio/detail/config.hpp> |
19 | #include <boost/asio/ip/address.hpp> |
20 | #include <boost/asio/ip/detail/endpoint.hpp> |
21 | |
22 | #if !defined(BOOST_ASIO_NO_IOSTREAM) |
23 | # include <iosfwd> |
24 | #endif // !defined(BOOST_ASIO_NO_IOSTREAM) |
25 | |
26 | #include <boost/asio/detail/push_options.hpp> |
27 | |
28 | namespace boost { |
29 | namespace asio { |
30 | namespace ip { |
31 | |
32 | /// Describes an endpoint for a version-independent IP socket. |
33 | /** |
34 | * The boost::asio::ip::basic_endpoint class template describes an endpoint that |
35 | * may be associated with a particular socket. |
36 | * |
37 | * @par Thread Safety |
38 | * @e Distinct @e objects: Safe.@n |
39 | * @e Shared @e objects: Unsafe. |
40 | * |
41 | * @par Concepts: |
42 | * Endpoint. |
43 | */ |
44 | template <typename InternetProtocol> |
45 | class basic_endpoint |
46 | { |
47 | public: |
48 | /// The protocol type associated with the endpoint. |
49 | typedef InternetProtocol protocol_type; |
50 | |
51 | /// The type of the endpoint structure. This type is dependent on the |
52 | /// underlying implementation of the socket layer. |
53 | #if defined(GENERATING_DOCUMENTATION) |
54 | typedef implementation_defined data_type; |
55 | #else |
56 | typedef boost::asio::detail::socket_addr_type data_type; |
57 | #endif |
58 | |
59 | /// Default constructor. |
60 | basic_endpoint() |
61 | : impl_() |
62 | { |
63 | } |
64 | |
65 | /// Construct an endpoint using a port number, specified in the host's byte |
66 | /// order. The IP address will be the any address (i.e. INADDR_ANY or |
67 | /// in6addr_any). This constructor would typically be used for accepting new |
68 | /// connections. |
69 | /** |
70 | * @par Examples |
71 | * To initialise an IPv4 TCP endpoint for port 1234, use: |
72 | * @code |
73 | * boost::asio::ip::tcp::endpoint ep(boost::asio::ip::tcp::v4(), 1234); |
74 | * @endcode |
75 | * |
76 | * To specify an IPv6 UDP endpoint for port 9876, use: |
77 | * @code |
78 | * boost::asio::ip::udp::endpoint ep(boost::asio::ip::udp::v6(), 9876); |
79 | * @endcode |
80 | */ |
81 | basic_endpoint(const InternetProtocol& internet_protocol, |
82 | unsigned short port_num) |
83 | : impl_(internet_protocol.family(), port_num) |
84 | { |
85 | } |
86 | |
87 | /// Construct an endpoint using a port number and an IP address. This |
88 | /// constructor may be used for accepting connections on a specific interface |
89 | /// or for making a connection to a remote endpoint. |
90 | basic_endpoint(const boost::asio::ip::address& addr, unsigned short port_num) |
91 | : impl_(addr, port_num) |
92 | { |
93 | } |
94 | |
95 | /// Copy constructor. |
96 | basic_endpoint(const basic_endpoint& other) |
97 | : impl_(other.impl_) |
98 | { |
99 | } |
100 | |
101 | #if defined(BOOST_ASIO_HAS_MOVE) |
102 | /// Move constructor. |
103 | basic_endpoint(basic_endpoint&& other) |
104 | : impl_(other.impl_) |
105 | { |
106 | } |
107 | #endif // defined(BOOST_ASIO_HAS_MOVE) |
108 | |
109 | /// Assign from another endpoint. |
110 | basic_endpoint& operator=(const basic_endpoint& other) |
111 | { |
112 | impl_ = other.impl_; |
113 | return *this; |
114 | } |
115 | |
116 | #if defined(BOOST_ASIO_HAS_MOVE) |
117 | /// Move-assign from another endpoint. |
118 | basic_endpoint& operator=(basic_endpoint&& other) |
119 | { |
120 | impl_ = other.impl_; |
121 | return *this; |
122 | } |
123 | #endif // defined(BOOST_ASIO_HAS_MOVE) |
124 | |
125 | /// The protocol associated with the endpoint. |
126 | protocol_type protocol() const |
127 | { |
128 | if (impl_.is_v4()) |
129 | return InternetProtocol::v4(); |
130 | return InternetProtocol::v6(); |
131 | } |
132 | |
133 | /// Get the underlying endpoint in the native type. |
134 | data_type* data() |
135 | { |
136 | return impl_.data(); |
137 | } |
138 | |
139 | /// Get the underlying endpoint in the native type. |
140 | const data_type* data() const |
141 | { |
142 | return impl_.data(); |
143 | } |
144 | |
145 | /// Get the underlying size of the endpoint in the native type. |
146 | std::size_t size() const |
147 | { |
148 | return impl_.size(); |
149 | } |
150 | |
151 | /// Set the underlying size of the endpoint in the native type. |
152 | void resize(std::size_t new_size) |
153 | { |
154 | impl_.resize(new_size); |
155 | } |
156 | |
157 | /// Get the capacity of the endpoint in the native type. |
158 | std::size_t capacity() const |
159 | { |
160 | return impl_.capacity(); |
161 | } |
162 | |
163 | /// Get the port associated with the endpoint. The port number is always in |
164 | /// the host's byte order. |
165 | unsigned short port() const |
166 | { |
167 | return impl_.port(); |
168 | } |
169 | |
170 | /// Set the port associated with the endpoint. The port number is always in |
171 | /// the host's byte order. |
172 | void port(unsigned short port_num) |
173 | { |
174 | impl_.port(port_num); |
175 | } |
176 | |
177 | /// Get the IP address associated with the endpoint. |
178 | boost::asio::ip::address address() const |
179 | { |
180 | return impl_.address(); |
181 | } |
182 | |
183 | /// Set the IP address associated with the endpoint. |
184 | void address(const boost::asio::ip::address& addr) |
185 | { |
186 | impl_.address(addr); |
187 | } |
188 | |
189 | /// Compare two endpoints for equality. |
190 | friend bool operator==(const basic_endpoint<InternetProtocol>& e1, |
191 | const basic_endpoint<InternetProtocol>& e2) |
192 | { |
193 | return e1.impl_ == e2.impl_; |
194 | } |
195 | |
196 | /// Compare two endpoints for inequality. |
197 | friend bool operator!=(const basic_endpoint<InternetProtocol>& e1, |
198 | const basic_endpoint<InternetProtocol>& e2) |
199 | { |
200 | return !(e1 == e2); |
201 | } |
202 | |
203 | /// Compare endpoints for ordering. |
204 | friend bool operator<(const basic_endpoint<InternetProtocol>& e1, |
205 | const basic_endpoint<InternetProtocol>& e2) |
206 | { |
207 | return e1.impl_ < e2.impl_; |
208 | } |
209 | |
210 | /// Compare endpoints for ordering. |
211 | friend bool operator>(const basic_endpoint<InternetProtocol>& e1, |
212 | const basic_endpoint<InternetProtocol>& e2) |
213 | { |
214 | return e2.impl_ < e1.impl_; |
215 | } |
216 | |
217 | /// Compare endpoints for ordering. |
218 | friend bool operator<=(const basic_endpoint<InternetProtocol>& e1, |
219 | const basic_endpoint<InternetProtocol>& e2) |
220 | { |
221 | return !(e2 < e1); |
222 | } |
223 | |
224 | /// Compare endpoints for ordering. |
225 | friend bool operator>=(const basic_endpoint<InternetProtocol>& e1, |
226 | const basic_endpoint<InternetProtocol>& e2) |
227 | { |
228 | return !(e1 < e2); |
229 | } |
230 | |
231 | private: |
232 | // The underlying IP endpoint. |
233 | boost::asio::ip::detail::endpoint impl_; |
234 | }; |
235 | |
236 | #if !defined(BOOST_ASIO_NO_IOSTREAM) |
237 | |
238 | /// Output an endpoint as a string. |
239 | /** |
240 | * Used to output a human-readable string for a specified endpoint. |
241 | * |
242 | * @param os The output stream to which the string will be written. |
243 | * |
244 | * @param endpoint The endpoint to be written. |
245 | * |
246 | * @return The output stream. |
247 | * |
248 | * @relates boost::asio::ip::basic_endpoint |
249 | */ |
250 | template <typename Elem, typename Traits, typename InternetProtocol> |
251 | std::basic_ostream<Elem, Traits>& operator<<( |
252 | std::basic_ostream<Elem, Traits>& os, |
253 | const basic_endpoint<InternetProtocol>& endpoint); |
254 | |
255 | #endif // !defined(BOOST_ASIO_NO_IOSTREAM) |
256 | |
257 | } // namespace ip |
258 | } // namespace asio |
259 | } // namespace boost |
260 | |
261 | #include <boost/asio/detail/pop_options.hpp> |
262 | |
263 | #include <boost/asio/ip/impl/basic_endpoint.hpp> |
264 | |
265 | #endif // BOOST_ASIO_IP_BASIC_ENDPOINT_HPP |
266 | |