endpoint.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // ip/detail/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_DETAIL_ENDPOINT_HPP
  11. #define BOOST_ASIO_IP_DETAIL_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 <string>
  17. #include <boost/asio/detail/socket_types.hpp>
  18. #include <boost/asio/detail/winsock_init.hpp>
  19. #include <boost/system/error_code.hpp>
  20. #include <boost/asio/ip/address.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace ip {
  25. namespace detail {
  26. // Helper class for implementating an IP endpoint.
  27. class endpoint
  28. {
  29. public:
  30. // Default constructor.
  31. BOOST_ASIO_DECL endpoint() BOOST_ASIO_NOEXCEPT;
  32. // Construct an endpoint using a family and port number.
  33. BOOST_ASIO_DECL endpoint(int family,
  34. unsigned short port_num) BOOST_ASIO_NOEXCEPT;
  35. // Construct an endpoint using an address and port number.
  36. BOOST_ASIO_DECL endpoint(const boost::asio::ip::address& addr,
  37. unsigned short port_num) BOOST_ASIO_NOEXCEPT;
  38. // Copy constructor.
  39. endpoint(const endpoint& other) BOOST_ASIO_NOEXCEPT
  40. : data_(other.data_)
  41. {
  42. }
  43. // Assign from another endpoint.
  44. endpoint& operator=(const endpoint& other) BOOST_ASIO_NOEXCEPT
  45. {
  46. data_ = other.data_;
  47. return *this;
  48. }
  49. // Get the underlying endpoint in the native type.
  50. boost::asio::detail::socket_addr_type* data() BOOST_ASIO_NOEXCEPT
  51. {
  52. return &data_.base;
  53. }
  54. // Get the underlying endpoint in the native type.
  55. const boost::asio::detail::socket_addr_type* data() const BOOST_ASIO_NOEXCEPT
  56. {
  57. return &data_.base;
  58. }
  59. // Get the underlying size of the endpoint in the native type.
  60. std::size_t size() const BOOST_ASIO_NOEXCEPT
  61. {
  62. if (is_v4())
  63. return sizeof(boost::asio::detail::sockaddr_in4_type);
  64. else
  65. return sizeof(boost::asio::detail::sockaddr_in6_type);
  66. }
  67. // Set the underlying size of the endpoint in the native type.
  68. BOOST_ASIO_DECL void resize(std::size_t new_size);
  69. // Get the capacity of the endpoint in the native type.
  70. std::size_t capacity() const BOOST_ASIO_NOEXCEPT
  71. {
  72. return sizeof(data_);
  73. }
  74. // Get the port associated with the endpoint.
  75. BOOST_ASIO_DECL unsigned short port() const BOOST_ASIO_NOEXCEPT;
  76. // Set the port associated with the endpoint.
  77. BOOST_ASIO_DECL void port(unsigned short port_num) BOOST_ASIO_NOEXCEPT;
  78. // Get the IP address associated with the endpoint.
  79. BOOST_ASIO_DECL boost::asio::ip::address address() const BOOST_ASIO_NOEXCEPT;
  80. // Set the IP address associated with the endpoint.
  81. BOOST_ASIO_DECL void address(
  82. const boost::asio::ip::address& addr) BOOST_ASIO_NOEXCEPT;
  83. // Compare two endpoints for equality.
  84. BOOST_ASIO_DECL friend bool operator==(const endpoint& e1,
  85. const endpoint& e2) BOOST_ASIO_NOEXCEPT;
  86. // Compare endpoints for ordering.
  87. BOOST_ASIO_DECL friend bool operator<(const endpoint& e1,
  88. const endpoint& e2) BOOST_ASIO_NOEXCEPT;
  89. // Determine whether the endpoint is IPv4.
  90. bool is_v4() const BOOST_ASIO_NOEXCEPT
  91. {
  92. return data_.base.sa_family == BOOST_ASIO_OS_DEF(AF_INET);
  93. }
  94. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  95. // Convert to a string.
  96. BOOST_ASIO_DECL std::string to_string() const;
  97. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  98. private:
  99. // The underlying IP socket address.
  100. union data_union
  101. {
  102. boost::asio::detail::socket_addr_type base;
  103. boost::asio::detail::sockaddr_in4_type v4;
  104. boost::asio::detail::sockaddr_in6_type v6;
  105. } data_;
  106. };
  107. } // namespace detail
  108. } // namespace ip
  109. } // namespace asio
  110. } // namespace boost
  111. #include <boost/asio/detail/pop_options.hpp>
  112. #if defined(BOOST_ASIO_HEADER_ONLY)
  113. # include <boost/asio/ip/detail/impl/endpoint.ipp>
  114. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  115. #endif // BOOST_ASIO_IP_DETAIL_ENDPOINT_HPP