basic_endpoint.hpp 7.3 KB

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