1 // 2 // generic/detail/endpoint.hpp 3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2021 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 11 #ifndef BOOST_ASIO_GENERIC_DETAIL_ENDPOINT_HPP 12 #define BOOST_ASIO_GENERIC_DETAIL_ENDPOINT_HPP 13 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 15 # pragma once 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 17 18 #include <boost/asio/detail/config.hpp> 19 20 #include <cstddef> 21 #include <boost/asio/detail/socket_types.hpp> 22 23 #include <boost/asio/detail/push_options.hpp> 24 25 namespace boost { 26 namespace asio { 27 namespace generic { 28 namespace detail { 29 30 // Helper class for implementing a generic socket endpoint. 31 class endpoint 32 { 33 public: 34 // Default constructor. 35 BOOST_ASIO_DECL endpoint(); 36 37 // Construct an endpoint from the specified raw bytes. 38 BOOST_ASIO_DECL endpoint(const void* sock_addr, 39 std::size_t sock_addr_size, int sock_protocol); 40 41 // Copy constructor. endpoint(const endpoint & other)42 endpoint(const endpoint& other) 43 : data_(other.data_), 44 size_(other.size_), 45 protocol_(other.protocol_) 46 { 47 } 48 49 // Assign from another endpoint. operator =(const endpoint & other)50 endpoint& operator=(const endpoint& other) 51 { 52 data_ = other.data_; 53 size_ = other.size_; 54 protocol_ = other.protocol_; 55 return *this; 56 } 57 58 // Get the address family associated with the endpoint. family() const59 int family() const 60 { 61 return data_.base.sa_family; 62 } 63 64 // Get the socket protocol associated with the endpoint. protocol() const65 int protocol() const 66 { 67 return protocol_; 68 } 69 70 // Get the underlying endpoint in the native type. data()71 boost::asio::detail::socket_addr_type* data() 72 { 73 return &data_.base; 74 } 75 76 // Get the underlying endpoint in the native type. data() const77 const boost::asio::detail::socket_addr_type* data() const 78 { 79 return &data_.base; 80 } 81 82 // Get the underlying size of the endpoint in the native type. size() const83 std::size_t size() const 84 { 85 return size_; 86 } 87 88 // Set the underlying size of the endpoint in the native type. 89 BOOST_ASIO_DECL void resize(std::size_t size); 90 91 // Get the capacity of the endpoint in the native type. capacity() const92 std::size_t capacity() const 93 { 94 return sizeof(boost::asio::detail::sockaddr_storage_type); 95 } 96 97 // Compare two endpoints for equality. 98 BOOST_ASIO_DECL friend bool operator==( 99 const endpoint& e1, const endpoint& e2); 100 101 // Compare endpoints for ordering. 102 BOOST_ASIO_DECL friend bool operator<( 103 const endpoint& e1, const endpoint& e2); 104 105 private: 106 // The underlying socket address. 107 union data_union 108 { 109 boost::asio::detail::socket_addr_type base; 110 boost::asio::detail::sockaddr_storage_type generic; 111 } data_; 112 113 // The length of the socket address stored in the endpoint. 114 std::size_t size_; 115 116 // The socket protocol associated with the endpoint. 117 int protocol_; 118 119 // Initialise with a specified memory. 120 BOOST_ASIO_DECL void init(const void* sock_addr, 121 std::size_t sock_addr_size, int sock_protocol); 122 }; 123 124 } // namespace detail 125 } // namespace generic 126 } // namespace asio 127 } // namespace boost 128 129 #include <boost/asio/detail/pop_options.hpp> 130 131 #if defined(BOOST_ASIO_HEADER_ONLY) 132 # include <boost/asio/generic/detail/impl/endpoint.ipp> 133 #endif // defined(BOOST_ASIO_HEADER_ONLY) 134 135 #endif // BOOST_ASIO_GENERIC_DETAIL_ENDPOINT_HPP 136