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