Clementine
endpoint.hpp
1 //
2 // ip/detail/endpoint.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 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_IP_DETAIL_ENDPOINT_HPP
12 #define ASIO_IP_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 "asio/detail/config.hpp"
19 #include <string>
20 #include "asio/detail/socket_types.hpp"
21 #include "asio/detail/winsock_init.hpp"
22 #include "asio/error_code.hpp"
23 #include "asio/ip/address.hpp"
24 
25 #include "asio/detail/push_options.hpp"
26 
27 namespace asio {
28 namespace ip {
29 namespace detail {
30 
31 // Helper class for implementating an IP endpoint.
32 class endpoint
33 {
34 public:
35  // Default constructor.
36  ASIO_DECL endpoint() ASIO_NOEXCEPT;
37 
38  // Construct an endpoint using a family and port number.
39  ASIO_DECL endpoint(int family,
40  unsigned short port_num) ASIO_NOEXCEPT;
41 
42  // Construct an endpoint using an address and port number.
43  ASIO_DECL endpoint(const asio::ip::address& addr,
44  unsigned short port_num) ASIO_NOEXCEPT;
45 
46  // Copy constructor.
47  endpoint(const endpoint& other) ASIO_NOEXCEPT
48  : data_(other.data_)
49  {
50  }
51 
52  // Assign from another endpoint.
53  endpoint& operator=(const endpoint& other) ASIO_NOEXCEPT
54  {
55  data_ = other.data_;
56  return *this;
57  }
58 
59  // Get the underlying endpoint in the native type.
60  asio::detail::socket_addr_type* data() ASIO_NOEXCEPT
61  {
62  return &data_.base;
63  }
64 
65  // Get the underlying endpoint in the native type.
66  const asio::detail::socket_addr_type* data() const ASIO_NOEXCEPT
67  {
68  return &data_.base;
69  }
70 
71  // Get the underlying size of the endpoint in the native type.
72  std::size_t size() const ASIO_NOEXCEPT
73  {
74  if (is_v4())
75  return sizeof(asio::detail::sockaddr_in4_type);
76  else
77  return sizeof(asio::detail::sockaddr_in6_type);
78  }
79 
80  // Set the underlying size of the endpoint in the native type.
81  ASIO_DECL void resize(std::size_t new_size);
82 
83  // Get the capacity of the endpoint in the native type.
84  std::size_t capacity() const ASIO_NOEXCEPT
85  {
86  return sizeof(data_);
87  }
88 
89  // Get the port associated with the endpoint.
90  ASIO_DECL unsigned short port() const ASIO_NOEXCEPT;
91 
92  // Set the port associated with the endpoint.
93  ASIO_DECL void port(unsigned short port_num) ASIO_NOEXCEPT;
94 
95  // Get the IP address associated with the endpoint.
96  ASIO_DECL asio::ip::address address() const ASIO_NOEXCEPT;
97 
98  // Set the IP address associated with the endpoint.
99  ASIO_DECL void address(
100  const asio::ip::address& addr) ASIO_NOEXCEPT;
101 
102  // Compare two endpoints for equality.
103  ASIO_DECL friend bool operator==(const endpoint& e1,
104  const endpoint& e2) ASIO_NOEXCEPT;
105 
106  // Compare endpoints for ordering.
107  ASIO_DECL friend bool operator<(const endpoint& e1,
108  const endpoint& e2) ASIO_NOEXCEPT;
109 
110  // Determine whether the endpoint is IPv4.
111  bool is_v4() const ASIO_NOEXCEPT
112  {
113  return data_.base.sa_family == ASIO_OS_DEF(AF_INET);
114  }
115 
116 #if !defined(ASIO_NO_IOSTREAM)
117  // Convert to a string.
118  ASIO_DECL std::string to_string() const;
119 #endif // !defined(ASIO_NO_IOSTREAM)
120 
121 private:
122  // The underlying IP socket address.
123  union data_union
124  {
125  asio::detail::socket_addr_type base;
126  asio::detail::sockaddr_in4_type v4;
127  asio::detail::sockaddr_in6_type v6;
128  } data_;
129 };
130 
131 } // namespace detail
132 } // namespace ip
133 } // namespace asio
134 
135 #include "asio/detail/pop_options.hpp"
136 
137 #if defined(ASIO_HEADER_ONLY)
138 # include "asio/ip/detail/impl/endpoint.ipp"
139 #endif // defined(ASIO_HEADER_ONLY)
140 
141 #endif // ASIO_IP_DETAIL_ENDPOINT_HPP
Definition: chrono.h:284
Implements version-independent IP addresses.
Definition: address.hpp:46
Definition: endpoint.hpp:32
Definition: any_io_executor.hpp:28