basic_endpoint.hpp 8.0 KB

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