1#ifndef NEFORCE_NETWORK_UTIL_IP_ADDRESS_HPP__
2#define NEFORCE_NETWORK_UTIL_IP_ADDRESS_HPP__
16#ifdef NEFORCE_PLATFORM_WINDOWS
25#ifdef NEFORCE_PLATFORM_LINUX
26# include <netinet/in.h>
28NEFORCE_BEGIN_NAMESPACE__
61#ifdef NEFORCE_PLATFORM_LINUX
64 BLUETOOTH = AF_BLUETOOTH,
88 explicit
ip_address(const ::sockaddr_in& addr4) noexcept :
95 explicit ip_address(const ::sockaddr_in6& addr6) noexcept :
108 NEFORCE_NODISCARD
bool is_valid() const noexcept {
return !addr_.holds_alternative<
none_t>(); }
114 NEFORCE_NODISCARD
bool is_ipv4() const noexcept {
return addr_.holds_alternative<::sockaddr_in>(); }
120 NEFORCE_NODISCARD
bool is_ipv6() const noexcept {
return addr_.holds_alternative<::sockaddr_in6>(); }
144 NEFORCE_NODISCARD const ::sockaddr*
data() const noexcept;
150 NEFORCE_NODISCARD ::sockaddr*
data() noexcept;
156 NEFORCE_NODISCARD
int size() const noexcept;
218struct future_handler<error_code, size_t, ip_address> {
219 shared_ptr<promise<pair<size_t, ip_address>>> promise_;
221 void operator()(error_code ec,
size_t n, ip_address addr) {
223 promise_->set_exception(_NEFORCE make_exception_ptr(system_exception(ec)));
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 ) {
238 handler_.promise_ = make_shared<promise<pair<size_t, ip_address>>>();
240 handler_type get_handler() {
return handler_; }
241 return_type
get() {
return handler_.promise_->get_future(); }
244NEFORCE_END_NAMESPACE__
bool is_valid() const noexcept
检查地址是否有效
ip_address(const ::sockaddr_in6 &addr6) noexcept
从IPv6地址构造
ports port() const noexcept
获取端口号
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结果获取函数
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的辅助函数
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素