Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:59

0001 //
0002 // ip/detail/endpoint.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_IP_DETAIL_ENDPOINT_HPP
0012 #define BOOST_ASIO_IP_DETAIL_ENDPOINT_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <string>
0020 #include <boost/asio/detail/socket_types.hpp>
0021 #include <boost/asio/detail/winsock_init.hpp>
0022 #include <boost/system/error_code.hpp>
0023 #include <boost/asio/ip/address.hpp>
0024 
0025 #include <boost/asio/detail/push_options.hpp>
0026 
0027 namespace boost {
0028 namespace asio {
0029 namespace ip {
0030 namespace detail {
0031 
0032 // Helper class for implementating an IP endpoint.
0033 class endpoint
0034 {
0035 public:
0036   // Default constructor.
0037   BOOST_ASIO_DECL endpoint() noexcept;
0038 
0039   // Construct an endpoint using a family and port number.
0040   BOOST_ASIO_DECL endpoint(int family,
0041       unsigned short port_num) noexcept;
0042 
0043   // Construct an endpoint using an address and port number.
0044   BOOST_ASIO_DECL endpoint(const boost::asio::ip::address& addr,
0045       unsigned short port_num) noexcept;
0046 
0047   // Copy constructor.
0048   endpoint(const endpoint& other) noexcept
0049     : data_(other.data_)
0050   {
0051   }
0052 
0053   // Assign from another endpoint.
0054   endpoint& operator=(const endpoint& other) noexcept
0055   {
0056     data_ = other.data_;
0057     return *this;
0058   }
0059 
0060   // Get the underlying endpoint in the native type.
0061   boost::asio::detail::socket_addr_type* data() noexcept
0062   {
0063     return &data_.base;
0064   }
0065 
0066   // Get the underlying endpoint in the native type.
0067   const boost::asio::detail::socket_addr_type* data() const noexcept
0068   {
0069     return &data_.base;
0070   }
0071 
0072   // Get the underlying size of the endpoint in the native type.
0073   std::size_t size() const noexcept
0074   {
0075     if (is_v4())
0076       return sizeof(boost::asio::detail::sockaddr_in4_type);
0077     else
0078       return sizeof(boost::asio::detail::sockaddr_in6_type);
0079   }
0080 
0081   // Set the underlying size of the endpoint in the native type.
0082   BOOST_ASIO_DECL void resize(std::size_t new_size);
0083 
0084   // Get the capacity of the endpoint in the native type.
0085   std::size_t capacity() const noexcept
0086   {
0087     return sizeof(data_);
0088   }
0089 
0090   // Get the port associated with the endpoint.
0091   BOOST_ASIO_DECL unsigned short port() const noexcept;
0092 
0093   // Set the port associated with the endpoint.
0094   BOOST_ASIO_DECL void port(unsigned short port_num) noexcept;
0095 
0096   // Get the IP address associated with the endpoint.
0097   BOOST_ASIO_DECL boost::asio::ip::address address() const noexcept;
0098 
0099   // Set the IP address associated with the endpoint.
0100   BOOST_ASIO_DECL void address(
0101       const boost::asio::ip::address& addr) noexcept;
0102 
0103   // Compare two endpoints for equality.
0104   BOOST_ASIO_DECL friend bool operator==(const endpoint& e1,
0105       const endpoint& e2) noexcept;
0106 
0107   // Compare endpoints for ordering.
0108   BOOST_ASIO_DECL friend bool operator<(const endpoint& e1,
0109       const endpoint& e2) noexcept;
0110 
0111   // Determine whether the endpoint is IPv4.
0112   bool is_v4() const noexcept
0113   {
0114     return data_.base.sa_family == BOOST_ASIO_OS_DEF(AF_INET);
0115   }
0116 
0117 #if !defined(BOOST_ASIO_NO_IOSTREAM)
0118   // Convert to a string.
0119   BOOST_ASIO_DECL std::string to_string() const;
0120 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
0121 
0122 private:
0123   // The underlying IP socket address.
0124   union data_union
0125   {
0126     boost::asio::detail::socket_addr_type base;
0127     boost::asio::detail::sockaddr_in4_type v4;
0128     boost::asio::detail::sockaddr_in6_type v6;
0129   } data_;
0130 };
0131 
0132 } // namespace detail
0133 } // namespace ip
0134 } // namespace asio
0135 } // namespace boost
0136 
0137 #include <boost/asio/detail/pop_options.hpp>
0138 
0139 #if defined(BOOST_ASIO_HEADER_ONLY)
0140 # include <boost/asio/ip/detail/impl/endpoint.ipp>
0141 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0142 
0143 #endif // BOOST_ASIO_IP_DETAIL_ENDPOINT_HPP