NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
socket_base.hpp
浏览该文件的文档.
1#ifndef NEFORCE_NETWORK_SOCKET_BASE_HPP__
2#define NEFORCE_NETWORK_SOCKET_BASE_HPP__
3
10
11#include "NeForce/core/exception/system_exception.hpp"
14#ifdef NEFORCE_PLATFORM_LINUX
15# include <linux/if_ether.h>
16#endif
17NEFORCE_BEGIN_NAMESPACE__
18
23
30struct NEFORCE_API socket_exception final : network_exception {
35 static int last_error() noexcept;
36
42 static bool is_would_block(int error) noexcept;
43
44 explicit socket_exception(const char* info = "Socket Operation Failed.",
45 const error_code code = error_code(last_error(), system_category())) noexcept :
46 network_exception(info, code) {}
47
48 explicit socket_exception(const exception& e) :
50
51 ~socket_exception() override = default;
52
53 NEFORCE_NODISCARD const char* type() const noexcept override { return "socket_exception"; }
54};
55 // Exceptions
57
63
81class NEFORCE_API socket_base {
82public:
84#ifdef NEFORCE_PLATFORM_WINDOWS
85 uintptr_t;
86#else
87 int;
88#endif
89
94#ifdef NEFORCE_PLATFORM_WINDOWS
96#else
97 -1;
98#endif
99
101
105 enum class type : int32_t {
106 STREAM = SOCK_STREAM,
107 DGRAM = SOCK_DGRAM,
108 RAW = SOCK_RAW,
109 RDM = SOCK_RDM,
110 SEQPACKET = SOCK_SEQPACKET,
111#ifdef NEFORCE_PLATFORM_LINUX
112 CLOEXEC = SOCK_CLOEXEC,
113 NONBLOCK = SOCK_NONBLOCK,
114 DCCP = SOCK_DCCP,
115#endif
116 };
117
123 enum class protocol : int32_t {
124 AUTO = 0,
125 ICMP = IPPROTO_ICMP,
126 TCP = IPPROTO_TCP,
127 UDP = IPPROTO_UDP,
128 SCTP = IPPROTO_SCTP,
129 RAW = IPPROTO_RAW,
130 ESP = IPPROTO_ESP,
131 AH = IPPROTO_AH,
132#ifdef NEFORCE_PLATFORM_LINUX
133 DCCP = IPPROTO_DCCP,
134
135 IP = ETH_P_IP,
136 ARP = ETH_P_ARP,
137 IPV6 = ETH_P_IPV6,
138 LAT = ETH_P_LAT,
139 VLAN = ETH_P_8021Q,
140 LLDP = ETH_P_LLDP,
141 MPLS = ETH_P_MPLS_UC
142#endif
143 };
144
145protected:
147 bool nonblocking_{false};
148
149public:
154
159 explicit socket_base(const native_handle_type fd) noexcept :
160 fd_(fd) {}
161
162 socket_base(const socket_base&) = delete;
163 socket_base& operator=(const socket_base&) = delete;
164
169 socket_base(socket_base&& other) noexcept :
170 fd_(exchange(other.fd_, invalid_handle)),
171 nonblocking_(exchange(other.nonblocking_, false)) {}
172
179
184
189 NEFORCE_NODISCARD native_handle_type native_handle() const noexcept { return fd_; }
190
195 NEFORCE_NODISCARD bool is_open() const noexcept { return fd_ != invalid_handle; }
196
201 explicit operator bool() const noexcept { return is_open(); }
202
212
217 virtual bool close() noexcept;
218
226 bool try_open(family f, type t = type::STREAM, protocol p = protocol::AUTO) noexcept;
227
232 NEFORCE_NODISCARD bool is_nonblocking() const noexcept;
233
239 bool set_nonblocking(bool enable) noexcept;
240
245 bool shutdown_send() noexcept;
246
251 bool shutdown_receive() noexcept;
252
257 bool shutdown_both() noexcept;
258
267 bool set_option(int level, int optname, const void* value, ::socklen_t len) noexcept;
268
277 bool get_option(int level, int optname, void* optval, ::socklen_t* optlen) const noexcept;
278
283 NEFORCE_NODISCARD bool get_reuse_address() const noexcept;
284
290 bool set_reuse_address(bool enable = true) noexcept;
291
296 NEFORCE_NODISCARD bool get_reuse_port() const noexcept;
297
303 bool set_reuse_port(bool enable = true) noexcept;
304
309 NEFORCE_NODISCARD bool get_keep_alive() const noexcept;
310
316 bool set_keep_alive(bool enable = true) noexcept;
317
322 NEFORCE_NODISCARD bool get_tcp_nodelay() const noexcept;
323
329 bool set_tcp_nodelay(bool enable = true) noexcept;
330
335 NEFORCE_NODISCARD int get_receive_buffer_size() const noexcept;
336
342 bool set_receive_buffer_size(int size) noexcept;
343
348 NEFORCE_NODISCARD int get_send_buffer_size() const noexcept;
349
355 bool set_send_buffer_size(int size) noexcept;
356
361 NEFORCE_NODISCARD optional<milliseconds> get_send_timeout() const noexcept;
362
368 bool set_send_timeout(milliseconds timeout) noexcept;
369
374 NEFORCE_NODISCARD optional<milliseconds> get_receive_timeout() const noexcept;
375
381 bool set_receive_timeout(milliseconds timeout) noexcept;
382
387 NEFORCE_NODISCARD optional<ip_address> local_endpoint() const;
388
393 NEFORCE_NODISCARD optional<ip_address> remote_endpoint() const;
394
401 void bind(const ip_address& endpoint);
402
409 void listen(int backlog);
410
417 NEFORCE_NODISCARD native_handle_type release() noexcept { return exchange(fd_, invalid_handle); }
418};
419 // SocketBase
421
422NEFORCE_END_NAMESPACE__
423#endif // NEFORCE_NETWORK_SOCKET_BASE_HPP__
IP地址封装类
family
网络地址族类型枚举
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
设置接收缓冲区大小
type
socket 类型枚举
@ STREAM
流式套接字(TCP)
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
关闭接收方向
protocol
socket 协议枚举
virtual bool close() noexcept
关闭socket
ip_address::family family
网络地址族类型
int get_send_buffer_size() const noexcept
查询发送缓冲区大小
bool set_nonblocking(bool enable) noexcept
设置非阻塞模式
socket_base()
默认构造函数
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
持续时间类型
int int32_t
32位有符号整数类型
duration< int64_t, milli > milliseconds
毫秒持续时间
const error_category & system_category() noexcept
获取系统错误类别单例
uint64_t uintptr_t
可容纳指针的无符号整数类型
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()))
获取容器的大小
IP地址封装类
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错误码