endpoint.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // generic/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_GENERIC_DETAIL_ENDPOINT_HPP
  11. #define BOOST_ASIO_GENERIC_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 <cstddef>
  17. #include <boost/asio/detail/socket_types.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. namespace generic {
  22. namespace detail {
  23. // Helper class for implementing a generic socket endpoint.
  24. class endpoint
  25. {
  26. public:
  27. // Default constructor.
  28. BOOST_ASIO_DECL endpoint();
  29. // Construct an endpoint from the specified raw bytes.
  30. BOOST_ASIO_DECL endpoint(const void* sock_addr,
  31. std::size_t sock_addr_size, int sock_protocol);
  32. // Copy constructor.
  33. endpoint(const endpoint& other)
  34. : data_(other.data_),
  35. size_(other.size_),
  36. protocol_(other.protocol_)
  37. {
  38. }
  39. // Assign from another endpoint.
  40. endpoint& operator=(const endpoint& other)
  41. {
  42. data_ = other.data_;
  43. size_ = other.size_;
  44. protocol_ = other.protocol_;
  45. return *this;
  46. }
  47. // Get the address family associated with the endpoint.
  48. int family() const
  49. {
  50. return data_.base.sa_family;
  51. }
  52. // Get the socket protocol associated with the endpoint.
  53. int protocol() const
  54. {
  55. return protocol_;
  56. }
  57. // Get the underlying endpoint in the native type.
  58. boost::asio::detail::socket_addr_type* data()
  59. {
  60. return &data_.base;
  61. }
  62. // Get the underlying endpoint in the native type.
  63. const boost::asio::detail::socket_addr_type* data() const
  64. {
  65. return &data_.base;
  66. }
  67. // Get the underlying size of the endpoint in the native type.
  68. std::size_t size() const
  69. {
  70. return size_;
  71. }
  72. // Set the underlying size of the endpoint in the native type.
  73. BOOST_ASIO_DECL void resize(std::size_t size);
  74. // Get the capacity of the endpoint in the native type.
  75. std::size_t capacity() const
  76. {
  77. return sizeof(boost::asio::detail::sockaddr_storage_type);
  78. }
  79. // Compare two endpoints for equality.
  80. BOOST_ASIO_DECL friend bool operator==(
  81. const endpoint& e1, const endpoint& e2);
  82. // Compare endpoints for ordering.
  83. BOOST_ASIO_DECL friend bool operator<(
  84. const endpoint& e1, const endpoint& e2);
  85. private:
  86. // The underlying socket address.
  87. union data_union
  88. {
  89. boost::asio::detail::socket_addr_type base;
  90. boost::asio::detail::sockaddr_storage_type generic;
  91. } data_;
  92. // The length of the socket address stored in the endpoint.
  93. std::size_t size_;
  94. // The socket protocol associated with the endpoint.
  95. int protocol_;
  96. // Initialise with a specified memory.
  97. BOOST_ASIO_DECL void init(const void* sock_addr,
  98. std::size_t sock_addr_size, int sock_protocol);
  99. };
  100. } // namespace detail
  101. } // namespace generic
  102. } // namespace asio
  103. } // namespace boost
  104. #include <boost/asio/detail/pop_options.hpp>
  105. #if defined(BOOST_ASIO_HEADER_ONLY)
  106. # include <boost/asio/generic/detail/impl/endpoint.ipp>
  107. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  108. #endif // BOOST_ASIO_GENERIC_DETAIL_ENDPOINT_HPP