endpoint.hpp 3.7 KB

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