NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
ip_address.hpp
浏览该文件的文档.
1#ifndef NEFORCE_NETWORK_UTIL_IP_ADDRESS_HPP__
2#define NEFORCE_NETWORK_UTIL_IP_ADDRESS_HPP__
3
10
16#ifdef NEFORCE_PLATFORM_WINDOWS
17# include <ws2tcpip.h>
18# ifdef max
19# undef max
20# endif
21# ifdef min
22# undef min
23# endif
24#endif
25#ifdef NEFORCE_PLATFORM_LINUX
26# include <netinet/in.h>
27#endif
28NEFORCE_BEGIN_NAMESPACE__
29
34
49class NEFORCE_API ip_address : public istringify<ip_address> {
50public:
52
56 enum class family : int32_t {
57 UNDEF = AF_UNSPEC,
58 UNIX = AF_UNIX,
59 INET4 = AF_INET,
60 INET6 = AF_INET6,
61#ifdef NEFORCE_PLATFORM_LINUX
62 PACKET = AF_PACKET,
63 NETLINK = AF_NETLINK,
64 BLUETOOTH = AF_BLUETOOTH,
65 CAN = AF_CAN,
66 ALG = AF_ALG,
67 VSOCK = AF_VSOCK,
68 NFC = AF_NFC,
69 XDP = AF_XDP,
70#endif
71 };
72
73private:
74 address_type addr_;
75
76public:
82 ip_address() noexcept = default;
83
88 explicit ip_address(const ::sockaddr_in& addr4) noexcept :
89 addr_(addr4) {}
90
95 explicit ip_address(const ::sockaddr_in6& addr6) noexcept :
96 addr_(addr6) {}
97
98 ip_address(const ip_address& other) noexcept = default;
99 ip_address& operator=(const ip_address& other) noexcept = default;
100
101 ip_address(ip_address&& other) noexcept = default;
102 ip_address& operator=(ip_address&& other) noexcept = default;
103
108 NEFORCE_NODISCARD bool is_valid() const noexcept { return !addr_.holds_alternative<none_t>(); }
109
114 NEFORCE_NODISCARD bool is_ipv4() const noexcept { return addr_.holds_alternative<::sockaddr_in>(); }
115
120 NEFORCE_NODISCARD bool is_ipv6() const noexcept { return addr_.holds_alternative<::sockaddr_in6>(); }
121
130 NEFORCE_NODISCARD static ip_address any(ports port = ports::UNDEF, family f = family::INET4) noexcept;
131
138 NEFORCE_NODISCARD static ip_address loopback(ports port = ports::UNDEF, family f = family::INET4) noexcept;
139
144 NEFORCE_NODISCARD const ::sockaddr* data() const noexcept;
145
150 NEFORCE_NODISCARD ::sockaddr* data() noexcept;
151
156 NEFORCE_NODISCARD int size() const noexcept;
157
162 NEFORCE_NODISCARD const address_type& address() const noexcept { return addr_; }
163
168 NEFORCE_NODISCARD family address_family() const noexcept;
169
174 NEFORCE_NODISCARD ports port() const noexcept;
175
183 NEFORCE_NODISCARD string to_string() const;
184
194 NEFORCE_NODISCARD static optional<ip_address> parse(const string& host, ports port = ports{}) noexcept;
195
203 NEFORCE_NODISCARD bool operator==(const ip_address& other) const;
204
210 NEFORCE_NODISCARD bool operator!=(const ip_address& other) const { return !(*this == other); }
211};
212 // NetworkUtil
214
215NEFORCE_BEGIN_INNER__
216
217template <>
218struct future_handler<error_code, size_t, ip_address> {
219 shared_ptr<promise<pair<size_t, ip_address>>> promise_;
220
221 void operator()(error_code ec, size_t n, ip_address addr) {
222 if (ec) {
223 promise_->set_exception(_NEFORCE make_exception_ptr(system_exception(ec)));
224 } else {
225 promise_->set_value(make_pair(n, move(addr)));
226 }
227 }
228};
229
230NEFORCE_END_INNER__
231
232template <>
233struct async_result<use_future_t, void(error_code, size_t, ip_address)> {
234 using handler_type = inner::future_handler<error_code, size_t, ip_address>;
235 using return_type = future<pair<size_t, ip_address>>;
236 handler_type handler_;
237 explicit async_result(use_future_t /*unused*/) {
238 handler_.promise_ = make_shared<promise<pair<size_t, ip_address>>>();
239 }
240 handler_type get_handler() { return handler_; }
241 return_type get() { return handler_.promise_->get_future(); }
242};
243
244NEFORCE_END_NAMESPACE__
245#endif // NEFORCE_NETWORK_UTIL_IP_ADDRESS_HPP__
完成令牌模型
IP地址封装类
bool is_valid() const noexcept
检查地址是否有效
ip_address(const ::sockaddr_in6 &addr6) noexcept
从IPv6地址构造
ports port() const noexcept
获取端口号
family
网络地址族类型枚举
@ INET4
IPv4 互联网协议族
family address_family() const noexcept
获取地址族
bool operator!=(const ip_address &other) const
不等比较运算符
static optional< ip_address > parse(const string &host, ports port=ports{}) noexcept
从字符串解析IP地址
const ::sockaddr * data() const noexcept
获取底层指针
static ip_address loopback(ports port=ports::UNDEF, family f=family::INET4) noexcept
获取回环地址
bool operator==(const ip_address &other) const
相等比较运算符
bool is_ipv4() const noexcept
检查是否为IPv4地址
bool is_ipv6() const noexcept
检查是否为IPv6地址
int size() const noexcept
获取地址结构大小
variant< none_t, ::sockaddr_in, ::sockaddr_in6 > address_type
地址存储类型
static ip_address any(ports port=ports::UNDEF, family f=family::INET4) noexcept
获取通配地址
string to_string() const
转换为字符串表示
ip_address() noexcept=default
默认构造函数
const address_type & address() const noexcept
获取内部地址存储的常量引用
enable_if_t< is_void_v< T >, future_result_t< T > > get(future< T > &f)
通用future结果获取函数
int int32_t
32位有符号整数类型
constexpr pair< unwrap_ref_decay_t< T1 >, unwrap_ref_decay_t< T2 > > make_pair(T1 &&x, T2 &&y) noexcept(conjunction< is_nothrow_constructible< unwrap_ref_decay_t< T1 >, T1 >, is_nothrow_constructible< unwrap_ref_decay_t< T2 >, T2 > >::value)
创建pair的辅助函数
uint64_t size_t
无符号大小类型
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
可字符串化接口
可选值类型
网络端口定义和转换工具
可字符串化接口
空状态类型
网络端口封装类
@ UNDEF
未定义/无效端口
变体类型主模板
变体类型实现