1#ifndef NEFORCE_NETWORK_SOCKET_BASE_HPP__
2#define NEFORCE_NETWORK_SOCKET_BASE_HPP__
13NEFORCE_BEGIN_NAMESPACE__
26struct NEFORCE_API socket_exception final : network_exception {
40 explicit socket_exception(const
char* info =
"Socket Operation Failed.", const
char*
type = static_type,
42 network_exception(info,
type,
code) {}
47 ~socket_exception()
override =
default;
49 static constexpr auto static_type =
"socket_exception";
102#ifdef NEFORCE_PLATFORM_WINDOWS
112#ifdef NEFORCE_PLATFORM_WINDOWS
172 explicit operator bool() const noexcept {
return is_open(); }
182 void open(
int family,
int type,
int protocol);
197 bool try_open(
int family,
int type,
int protocol) noexcept;
232 bool set_option(
int level,
int optname, const
void* value, ::socklen_t len) noexcept;
242 bool get_option(
int level,
int optname,
void* optval, ::socklen_t* optlen) const noexcept;
339NEFORCE_END_NAMESPACE__
static NEFORCE_NODISCARD constexpr T max() noexcept
获取类型的最大值
bool set_tcp_nodelay(bool enable=true) noexcept
设置TCP_NODELAY(禁用Nagle算法)
bool try_open(int family, int type, int protocol) noexcept
尝试打开socket(不抛出异常)
bool set_option(int level, int optname, const void *value, ::socklen_t len) noexcept
设置socket选项
bool set_reuse_address(bool enable=true) noexcept
设置地址重用(SO_REUSEADDR)
socket_base(const native_handle_type fd) noexcept
从原生句柄构造
NEFORCE_NODISCARD optional< ip_address > remote_endpoint() const
获取远程端点地址
int native_handle_type
平台原生句柄类型
bool shutdown_both() noexcept
关闭双向通信
bool shutdown_send() noexcept
关闭发送方向
void bind(const ip_address &endpoint)
绑定socket到本地地址
virtual bool close() noexcept
关闭socket
bool set_keep_alive(bool enable=true) noexcept
设置TCP KeepAlive
NEFORCE_NODISCARD bool is_open() const noexcept
检查socket是否已打开
bool shutdown_receive() noexcept
关闭接收方向
NEFORCE_NODISCARD optional< ip_address > local_endpoint() const
获取本地端点地址
bool set_send_buffer_size(int size) noexcept
设置发送缓冲区大小
bool set_nonblocking(bool enable) noexcept
设置非阻塞模式
NEFORCE_NODISCARD native_handle_type native_handle() const noexcept
获取原生句柄
void listen(int backlog)
开始监听连接(TCP)
native_handle_type fd_
Socket句柄
bool set_reuse_port(bool enable=true) noexcept
设置端口重用(SO_REUSEPORT)
void open(int family, int type, int protocol)
打开socket
socket_base(socket_base &&other) noexcept
移动构造函数
virtual ~socket_base()
析构函数
bool set_receive_timeout(milliseconds timeout) noexcept
设置接收超时时间
NEFORCE_NODISCARD native_handle_type release() noexcept
释放socket所有权
static constexpr native_handle_type invalid_handle
无效句柄常量
socket_base & operator=(socket_base &&other) noexcept
移动赋值运算符
bool set_send_timeout(milliseconds timeout) noexcept
设置发送超时时间
bool get_option(int level, int optname, void *optval, ::socklen_t *optlen) const noexcept
获取socket选项
bool set_receive_buffer_size(int size) noexcept
设置接收缓冲区大小
duration< int64_t, milli > milliseconds
毫秒持续时间
NEFORCE_CONSTEXPR14 T exchange(T &val, U &&new_val) noexcept(is_nothrow_move_constructible_v< T > &&is_nothrow_assignable_v< T &, U >)
将新值赋给对象并返回旧值
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
exception & operator=(const exception &other) noexcept
复制赋值运算符
NEFORCE_NODISCARD int code() const noexcept
获取异常码
NEFORCE_NODISCARD const char * type() const noexcept
获取异常类型
static int last_error() noexcept
获取最后系统Socket错误码
static bool is_would_block(int error) noexcept
检查错误码是否表示操作会阻塞