basic_endpoint.hpp 7.6 KB

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