1#ifndef NEFORCE_NETWORK_SOCKET_BASE_HPP__
2#define NEFORCE_NETWORK_SOCKET_BASE_HPP__
11#include "NeForce/core/exception/system_exception.hpp"
14#ifdef NEFORCE_PLATFORM_LINUX
15# include <linux/if_ether.h>
17NEFORCE_BEGIN_NAMESPACE__
30struct NEFORCE_API socket_exception final : network_exception {
44 explicit socket_exception(const
char* info =
"Socket Operation Failed.",
46 network_exception(info, code) {}
51 ~socket_exception()
override =
default;
53 NEFORCE_NODISCARD
const char*
type() const noexcept
override {
return "socket_exception"; }
84#ifdef NEFORCE_PLATFORM_WINDOWS
94#ifdef NEFORCE_PLATFORM_WINDOWS
106 STREAM = SOCK_STREAM,
110 SEQPACKET = SOCK_SEQPACKET,
111#ifdef NEFORCE_PLATFORM_LINUX
112 CLOEXEC = SOCK_CLOEXEC,
113 NONBLOCK = SOCK_NONBLOCK,
132#ifdef NEFORCE_PLATFORM_LINUX
201 explicit operator bool() const noexcept {
return is_open(); }
267 bool set_option(
int level,
int optname, const
void* value, ::socklen_t len) noexcept;
277 bool get_option(
int level,
int optname,
void* optval, ::socklen_t* optlen) const noexcept;
422NEFORCE_END_NAMESPACE__
static constexpr T max() noexcept
获取类型的最大值
socket_base(socket_base &&other) noexcept
移动构造函数
native_handle_type fd_
Socket句柄
bool get_option(int level, int optname, void *optval, ::socklen_t *optlen) const noexcept
获取socket选项
bool get_tcp_nodelay() const noexcept
查询Nagle算法
bool set_send_timeout(milliseconds timeout) noexcept
设置发送超时时间
bool set_receive_buffer_size(int size) noexcept
设置接收缓冲区大小
native_handle_type release() noexcept
释放socket所有权
bool set_receive_timeout(milliseconds timeout) noexcept
设置接收超时时间
bool get_reuse_address() const noexcept
查询地址重用
uintptr_t native_handle_type
平台原生句柄类型
int get_receive_buffer_size() const noexcept
查询接收缓冲区大小
bool shutdown_both() noexcept
关闭双向通信
native_handle_type native_handle() const noexcept
获取原生句柄
bool set_send_buffer_size(int size) noexcept
设置发送缓冲区大小
optional< milliseconds > get_send_timeout() const noexcept
获取发送超时时间
bool is_open() const noexcept
检查socket是否已打开
bool try_open(family f, type t=type::STREAM, protocol p=protocol::AUTO) noexcept
尝试打开socket
bool set_tcp_nodelay(bool enable=true) noexcept
设置禁用Nagle算法
bool shutdown_receive() noexcept
关闭接收方向
virtual bool close() noexcept
关闭socket
ip_address::family family
网络地址族类型
int get_send_buffer_size() const noexcept
查询发送缓冲区大小
bool set_nonblocking(bool enable) noexcept
设置非阻塞模式
socket_base & operator=(socket_base &&other) noexcept
移动赋值运算符
void open(family f, type t=type::STREAM, protocol p=protocol::AUTO)
打开socket
bool get_reuse_port() const noexcept
查询端口重用
void listen(int backlog)
开始监听连接(TCP)
bool set_keep_alive(bool enable=true) noexcept
设置TCP KeepAlive
optional< ip_address > local_endpoint() const
获取本地端点地址
bool shutdown_send() noexcept
关闭发送方向
optional< milliseconds > get_receive_timeout() const noexcept
获取接收超时时间
socket_base(const native_handle_type fd) noexcept
从原生句柄构造
bool is_nonblocking() const noexcept
查询是否为非阻塞模式
bool set_reuse_port(bool enable=true) noexcept
设置端口重用
bool nonblocking_
非阻塞模式状态跟踪
optional< ip_address > remote_endpoint() const
获取远程端点地址
void bind(const ip_address &endpoint)
绑定socket到本地地址
bool set_option(int level, int optname, const void *value, ::socklen_t len) noexcept
设置socket选项
virtual ~socket_base()
析构函数
bool set_reuse_address(bool enable=true) noexcept
设置地址重用
static constexpr native_handle_type invalid_handle
无效句柄常量
bool get_keep_alive() const noexcept
查询TCP KeepAlive
duration< int64_t, milli > milliseconds
毫秒持续时间
const error_category & system_category() noexcept
获取系统错误类别单例
constexpr T exchange(T &val, U &&new_val) noexcept(is_nothrow_move_constructible_v< T > &&is_nothrow_assignable_v< T &, U >)
将新值赋给对象并返回旧值
constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
exception & operator=(const exception &other) noexcept
复制赋值运算符
const char * type() const noexcept override
获取异常类型
static bool is_would_block(int error) noexcept
检查错误码是否表示操作会阻塞
static int last_error() noexcept
获取最后系统Socket错误码