NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
socket_base.hpp
浏览该文件的文档.
1#ifndef NEFORCE_NETWORK_SOCKET_BASE_HPP__
2#define NEFORCE_NETWORK_SOCKET_BASE_HPP__
3
10
13NEFORCE_BEGIN_NAMESPACE__
14
19
26struct NEFORCE_API socket_exception final : network_exception {
31 static int last_error() noexcept;
32
38 static bool is_would_block(int error) noexcept;
39
40 explicit socket_exception(const char* info = "Socket Operation Failed.", const char* type = static_type,
41 const int code = last_error()) noexcept :
42 network_exception(info, type, code) {}
43
44 explicit socket_exception(const exception& e) :
46
47 ~socket_exception() override = default;
48
49 static constexpr auto static_type = "socket_exception";
50};
51 // Exceptions
53
59
100class NEFORCE_API socket_base {
101public:
103#ifdef NEFORCE_PLATFORM_WINDOWS
104 ::UINT_PTR;
105#else
106 int;
107#endif
108
113#ifdef NEFORCE_PLATFORM_WINDOWS
115#else
116 -1;
117#endif
118
119protected:
121
122public:
127
132 explicit socket_base(const native_handle_type fd) noexcept :
133 fd_(fd) {}
134
135 socket_base(const socket_base&) = delete;
136 socket_base& operator=(const socket_base&) = delete;
137
142 socket_base(socket_base&& other) noexcept :
143 fd_(exchange(other.fd_, invalid_handle)) {}
144
151
156
161 NEFORCE_NODISCARD native_handle_type native_handle() const noexcept { return fd_; }
162
167 NEFORCE_NODISCARD bool is_open() const noexcept { return fd_ != invalid_handle; }
168
173 explicit operator bool() const noexcept { return is_open(); }
174
183 void open(int family, int type, int protocol);
184
189 virtual bool close() noexcept;
190
198 bool try_open(int family, int type, int protocol) noexcept;
199
205 bool set_nonblocking(bool enable) noexcept;
206
211 bool shutdown_send() noexcept;
212
217 bool shutdown_receive() noexcept;
218
223 bool shutdown_both() noexcept;
224
233 bool set_option(int level, int optname, const void* value, ::socklen_t len) noexcept;
234
243 bool get_option(int level, int optname, void* optval, ::socklen_t* optlen) const noexcept;
244
250 bool set_reuse_address(bool enable = true) noexcept;
251
257 bool set_reuse_port(bool enable = true) noexcept;
258
264 bool set_keep_alive(bool enable = true) noexcept;
265
271 bool set_tcp_nodelay(bool enable = true) noexcept;
272
278 bool set_receive_buffer_size(int size) noexcept;
279
285 bool set_send_buffer_size(int size) noexcept;
286
293
300
305 NEFORCE_NODISCARD optional<ip_address> local_endpoint() const;
306
311 NEFORCE_NODISCARD optional<ip_address> remote_endpoint() const;
312
319 void bind(const ip_address& endpoint);
320
327 void listen(int backlog);
328
335 NEFORCE_NODISCARD native_handle_type release() noexcept { return exchange(fd_, invalid_handle); }
336};
337 // SocketBase
339
340NEFORCE_END_NAMESPACE__
341#endif // NEFORCE_NETWORK_SOCKET_BASE_HPP__
IP地址封装类
static constexpr T max() noexcept
获取类型的最大值
可选值类
Socket基础类
bool set_tcp_nodelay(bool enable=true) noexcept
设置TCP_NODELAY(禁用Nagle算法)
bool try_open(int family, int type, int protocol) noexcept
尝试打开socket(不抛出异常)
optional< ip_address > local_endpoint() const
获取本地端点地址
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
从原生句柄构造
optional< ip_address > remote_endpoint() const
获取远程端点地址
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
bool shutdown_receive() noexcept
关闭接收方向
bool set_send_buffer_size(int size) noexcept
设置发送缓冲区大小
bool set_nonblocking(bool enable) noexcept
设置非阻塞模式
::UINT_PTR native_handle_type
平台原生句柄类型
native_handle_type release() noexcept
释放socket所有权
socket_base()
默认构造函数
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
设置接收超时时间
native_handle_type native_handle() const noexcept
获取原生句柄
bool is_open() const 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
毫秒持续时间
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
获取异常类型
int code() const noexcept
获取异常码
网络操作或行为异常
Socket操作异常类
static int last_error() noexcept
获取最后系统Socket错误码
static bool is_would_block(int error) noexcept
检查错误码是否表示操作会阻塞