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