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 ASIO_IP_BASIC_ENDPOINT_HPP 12 #define ASIO_IP_BASIC_ENDPOINT_HPP 13 14 15 #include "asio/detail/config.hpp" 16 #include "asio/ip/address.hpp" 17 #include "asio/ip/detail/endpoint.hpp" 18 19 20 #include "asio/detail/push_options.hpp" 21 22 namespace asio { 23 namespace ip { 24 25 /// Describes an endpoint for a version-independent IP socket. 26 /** 27 * The asio::ip::basic_endpoint class template describes an endpoint that 28 * may be associated with a particular socket. 29 * 30 * @par Thread Safety 31 * @e Distinct @e objects: Safe.@n 32 * @e Shared @e objects: Unsafe. 33 * 34 * @par Concepts: 35 * Endpoint. 36 */ 37 template <typename InternetProtocol> 38 class basic_endpoint 39 { 40 public: 41 /// The protocol type associated with the endpoint. 42 typedef InternetProtocol protocol_type; 43 44 /// The type of the endpoint structure. This type is dependent on the 45 /// underlying implementation of the socket layer. 46 typedef asio::detail::socket_addr_type data_type; 47 48 /// Default constructor. basic_endpoint()49 basic_endpoint() 50 : impl_() 51 { 52 } 53 54 /// Construct an endpoint using a port number, specified in the host's byte 55 /// order. The IP address will be the any address (i.e. INADDR_ANY or 56 /// in6addr_any). This constructor would typically be used for accepting new 57 /// connections. 58 /** 59 * @par Examples 60 * To initialise an IPv4 TCP endpoint for port 1234, use: 61 * @code 62 * asio::ip::tcp::endpoint ep(asio::ip::tcp::v4(), 1234); 63 * @endcode 64 * 65 * To specify an IPv6 UDP endpoint for port 9876, use: 66 * @code 67 * asio::ip::udp::endpoint ep(asio::ip::udp::v6(), 9876); 68 * @endcode 69 */ basic_endpoint(const InternetProtocol & internet_protocol,unsigned short port_num)70 basic_endpoint(const InternetProtocol& internet_protocol, 71 unsigned short port_num) 72 : impl_(internet_protocol.family(), port_num) 73 { 74 } 75 76 /// Construct an endpoint using a port number and an IP address. This 77 /// constructor may be used for accepting connections on a specific interface 78 /// or for making a connection to a remote endpoint. basic_endpoint(const asio::ip::address & addr,unsigned short port_num)79 basic_endpoint(const asio::ip::address& addr, unsigned short port_num) 80 : impl_(addr, port_num) 81 { 82 } 83 84 /// Copy constructor. basic_endpoint(const basic_endpoint & other)85 basic_endpoint(const basic_endpoint& other) 86 : impl_(other.impl_) 87 { 88 } 89 90 /// Move constructor. basic_endpoint(basic_endpoint && other)91 basic_endpoint(basic_endpoint&& other) 92 : impl_(other.impl_) 93 { 94 } 95 96 /// Assign from another endpoint. operator =(const basic_endpoint & other)97 basic_endpoint& operator=(const basic_endpoint& other) 98 { 99 impl_ = other.impl_; 100 return *this; 101 } 102 103 /// Move-assign from another endpoint. operator =(basic_endpoint && other)104 basic_endpoint& operator=(basic_endpoint&& other) 105 { 106 impl_ = other.impl_; 107 return *this; 108 } 109 110 /// The protocol associated with the endpoint. protocol() const111 protocol_type protocol() const 112 { 113 if (impl_.is_v4()) 114 return InternetProtocol::v4(); 115 return InternetProtocol::v6(); 116 } 117 118 /// Get the underlying endpoint in the native type. data()119 data_type* data() 120 { 121 return impl_.data(); 122 } 123 124 /// Get the underlying endpoint in the native type. data() const125 const data_type* data() const 126 { 127 return impl_.data(); 128 } 129 130 /// Get the underlying size of the endpoint in the native type. size() const131 std::size_t size() const 132 { 133 return impl_.size(); 134 } 135 136 /// Set the underlying size of the endpoint in the native type. resize(std::size_t new_size)137 void resize(std::size_t new_size) 138 { 139 impl_.resize(new_size); 140 } 141 142 /// Get the capacity of the endpoint in the native type. capacity() const143 std::size_t capacity() const 144 { 145 return impl_.capacity(); 146 } 147 148 /// Get the port associated with the endpoint. The port number is always in 149 /// the host's byte order. port() const150 unsigned short port() const 151 { 152 return impl_.port(); 153 } 154 155 /// Set the port associated with the endpoint. The port number is always in 156 /// the host's byte order. port(unsigned short port_num)157 void port(unsigned short port_num) 158 { 159 impl_.port(port_num); 160 } 161 162 /// Get the IP address associated with the endpoint. address() const163 asio::ip::address address() const 164 { 165 return impl_.address(); 166 } 167 168 /// Set the IP address associated with the endpoint. address(const asio::ip::address & addr)169 void address(const asio::ip::address& addr) 170 { 171 impl_.address(addr); 172 } 173 174 /// Compare two endpoints for equality. operator ==(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)175 friend bool operator==(const basic_endpoint<InternetProtocol>& e1, 176 const basic_endpoint<InternetProtocol>& e2) 177 { 178 return e1.impl_ == e2.impl_; 179 } 180 181 /// Compare two endpoints for inequality. operator !=(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)182 friend bool operator!=(const basic_endpoint<InternetProtocol>& e1, 183 const basic_endpoint<InternetProtocol>& e2) 184 { 185 return !(e1 == e2); 186 } 187 188 /// Compare endpoints for ordering. operator <(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)189 friend bool operator<(const basic_endpoint<InternetProtocol>& e1, 190 const basic_endpoint<InternetProtocol>& e2) 191 { 192 return e1.impl_ < e2.impl_; 193 } 194 195 /// Compare endpoints for ordering. operator >(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)196 friend bool operator>(const basic_endpoint<InternetProtocol>& e1, 197 const basic_endpoint<InternetProtocol>& e2) 198 { 199 return e2.impl_ < e1.impl_; 200 } 201 202 /// Compare endpoints for ordering. operator <=(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)203 friend bool operator<=(const basic_endpoint<InternetProtocol>& e1, 204 const basic_endpoint<InternetProtocol>& e2) 205 { 206 return !(e2 < e1); 207 } 208 209 /// Compare endpoints for ordering. operator >=(const basic_endpoint<InternetProtocol> & e1,const basic_endpoint<InternetProtocol> & e2)210 friend bool operator>=(const basic_endpoint<InternetProtocol>& e1, 211 const basic_endpoint<InternetProtocol>& e2) 212 { 213 return !(e1 < e2); 214 } 215 216 private: 217 // The underlying IP endpoint. 218 asio::ip::detail::endpoint impl_; 219 }; 220 221 222 } // namespace ip 223 } // namespace asio 224 225 #include "asio/detail/pop_options.hpp" 226 227 #include "asio/ip/impl/basic_endpoint.hpp" 228 229 #endif // ASIO_IP_BASIC_ENDPOINT_HPP 230