basic_endpoint.hpp 7.1 KB

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