basic_endpoint.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // ip/basic_endpoint.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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/ip/address.hpp>
  17. #include <boost/asio/ip/detail/endpoint.hpp>
  18. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  19. # include <iosfwd>
  20. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace ip {
  25. /// Describes an endpoint for a version-independent IP socket.
  26. /**
  27. * The boost::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. /// The type of the endpoint structure. This type is dependent on the
  44. /// underlying implementation of the socket layer.
  45. #if defined(GENERATING_DOCUMENTATION)
  46. typedef implementation_defined data_type;
  47. #else
  48. typedef boost::asio::detail::socket_addr_type data_type;
  49. #endif
  50. /// Default constructor.
  51. basic_endpoint() BOOST_ASIO_NOEXCEPT
  52. : impl_()
  53. {
  54. }
  55. /// Construct an endpoint using a port number, specified in the host's byte
  56. /// order. The IP address will be the any address (i.e. INADDR_ANY or
  57. /// in6addr_any). This constructor would typically be used for accepting new
  58. /// connections.
  59. /**
  60. * @par Examples
  61. * To initialise an IPv4 TCP endpoint for port 1234, use:
  62. * @code
  63. * boost::asio::ip::tcp::endpoint ep(boost::asio::ip::tcp::v4(), 1234);
  64. * @endcode
  65. *
  66. * To specify an IPv6 UDP endpoint for port 9876, use:
  67. * @code
  68. * boost::asio::ip::udp::endpoint ep(boost::asio::ip::udp::v6(), 9876);
  69. * @endcode
  70. */
  71. basic_endpoint(const InternetProtocol& internet_protocol,
  72. unsigned short port_num) BOOST_ASIO_NOEXCEPT
  73. : impl_(internet_protocol.family(), port_num)
  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.
  79. basic_endpoint(const boost::asio::ip::address& addr,
  80. unsigned short port_num) BOOST_ASIO_NOEXCEPT
  81. : impl_(addr, port_num)
  82. {
  83. }
  84. /// Copy constructor.
  85. basic_endpoint(const basic_endpoint& other) BOOST_ASIO_NOEXCEPT
  86. : impl_(other.impl_)
  87. {
  88. }
  89. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  90. /// Move constructor.
  91. basic_endpoint(basic_endpoint&& other) BOOST_ASIO_NOEXCEPT
  92. : impl_(other.impl_)
  93. {
  94. }
  95. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  96. /// Assign from another endpoint.
  97. basic_endpoint& operator=(const basic_endpoint& other) BOOST_ASIO_NOEXCEPT
  98. {
  99. impl_ = other.impl_;
  100. return *this;
  101. }
  102. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  103. /// Move-assign from another endpoint.
  104. basic_endpoint& operator=(basic_endpoint&& other) BOOST_ASIO_NOEXCEPT
  105. {
  106. impl_ = other.impl_;
  107. return *this;
  108. }
  109. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  110. /// The protocol associated with the endpoint.
  111. protocol_type protocol() const BOOST_ASIO_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() BOOST_ASIO_NOEXCEPT
  119. {
  120. return impl_.data();
  121. }
  122. /// Get the underlying endpoint in the native type.
  123. const data_type* data() const BOOST_ASIO_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 BOOST_ASIO_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 BOOST_ASIO_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. unsigned short port() const BOOST_ASIO_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(unsigned short port_num) BOOST_ASIO_NOEXCEPT
  151. {
  152. impl_.port(port_num);
  153. }
  154. /// Get the IP address associated with the endpoint.
  155. boost::asio::ip::address address() const BOOST_ASIO_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) BOOST_ASIO_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) BOOST_ASIO_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) BOOST_ASIO_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) BOOST_ASIO_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) BOOST_ASIO_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) BOOST_ASIO_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) BOOST_ASIO_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. #include <boost/asio/detail/pop_options.hpp>
  226. #include <boost/asio/ip/impl/basic_endpoint.hpp>
  227. #endif // BOOST_ASIO_IP_BASIC_ENDPOINT_HPP