|
NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
|
TCP Acceptor类 更多...
#include <tcp_acceptor.hpp>
Public 成员函数 | |
| tcp_acceptor ()=default | |
| 默认构造函数 | |
| void | open (const ip_address &endpoint, int backlog=SOMAXCONN) |
| 打开并开始监听 | |
| tcp_socket | accept () |
| 接受客户端连接(阻塞模式) | |
| optional< tcp_socket > | accept_nonblock () |
| 接受客户端连接(非阻塞模式) | |
| void | async_accept (io_context &ctx, function< void(error_code, tcp_socket)> handler) |
| 异步接受客户端连接 | |
| void | async_accept (io_context &ctx, cancellation_slot &slot, function< void(error_code, tcp_socket)> handler) |
| 异步接受连接(带取消槽) | |
| template<typename Token, enable_if_t<!is_same_v< decay_t< Token >, function< void(error_code, tcp_socket)> >, int > = 0> | |
| void | async_accept (io_context &ctx, Token &&token) |
| 异步接受—任意可调用对象 | |
| decltype(auto) | async_accept (io_context &ctx, use_future_t) |
| 异步接受—use_future | |
| void | async_accept (io_context &ctx, detached_t) |
| 异步接受—detached(即发即忘) | |
| decltype(auto) | async_accept (io_context &ctx, use_awaitable_t) |
| 异步接受—use_awaitable | |
| void | open (family f, type t=type::STREAM, protocol p=protocol::AUTO) |
| 打开socket | |
| Public 成员函数 继承自 neforce::ip_socket | |
| ip_socket ()=default | |
| 默认构造函数 | |
| ip_socket (const native_handle_type fd) noexcept | |
| 从原生句柄构造 | |
| ~ip_socket () override=default | |
| 析构函数 | |
| family | address_family () const noexcept |
| 获取地址族 | |
| bool | is_ipv4 () const noexcept |
| 检查是否为IPv4 socket | |
| bool | is_ipv6 () const noexcept |
| 检查是否为IPv6 socket | |
| bool | close () noexcept override |
| 关闭socket | |
| Public 成员函数 继承自 neforce::socket_base | |
| socket_base () | |
| 默认构造函数 | |
| socket_base (const native_handle_type fd) noexcept | |
| 从原生句柄构造 | |
| socket_base (socket_base &&other) noexcept | |
| 移动构造函数 | |
| socket_base & | operator= (socket_base &&other) noexcept |
| 移动赋值运算符 | |
| virtual | ~socket_base () |
| 析构函数 | |
| native_handle_type | native_handle () const noexcept |
| 获取原生句柄 | |
| bool | is_open () const noexcept |
| 检查socket是否已打开 | |
| operator bool () const noexcept | |
| 布尔转换运算符 | |
| void | open (family f, type t=type::STREAM, protocol p=protocol::AUTO) |
| 打开socket | |
| bool | try_open (family f, type t=type::STREAM, protocol p=protocol::AUTO) noexcept |
| 尝试打开socket | |
| bool | is_nonblocking () const noexcept |
| 查询是否为非阻塞模式 | |
| bool | set_nonblocking (bool enable) noexcept |
| 设置非阻塞模式 | |
| bool | shutdown_send () noexcept |
| 关闭发送方向 | |
| bool | shutdown_receive () noexcept |
| 关闭接收方向 | |
| bool | shutdown_both () noexcept |
| 关闭双向通信 | |
| bool | set_option (int level, int optname, const void *value, ::socklen_t len) noexcept |
| 设置socket选项 | |
| bool | get_option (int level, int optname, void *optval, ::socklen_t *optlen) const noexcept |
| 获取socket选项 | |
| bool | get_reuse_address () const noexcept |
| 查询地址重用 | |
| bool | set_reuse_address (bool enable=true) noexcept |
| 设置地址重用 | |
| bool | get_reuse_port () const noexcept |
| 查询端口重用 | |
| bool | set_reuse_port (bool enable=true) noexcept |
| 设置端口重用 | |
| bool | get_keep_alive () const noexcept |
| 查询TCP KeepAlive | |
| bool | set_keep_alive (bool enable=true) noexcept |
| 设置TCP KeepAlive | |
| bool | get_tcp_nodelay () const noexcept |
| 查询Nagle算法 | |
| bool | set_tcp_nodelay (bool enable=true) noexcept |
| 设置禁用Nagle算法 | |
| int | get_receive_buffer_size () const noexcept |
| 查询接收缓冲区大小 | |
| bool | set_receive_buffer_size (int size) noexcept |
| 设置接收缓冲区大小 | |
| int | get_send_buffer_size () const noexcept |
| 查询发送缓冲区大小 | |
| bool | set_send_buffer_size (int size) noexcept |
| 设置发送缓冲区大小 | |
| optional< milliseconds > | get_send_timeout () const noexcept |
| 获取发送超时时间 | |
| bool | set_send_timeout (milliseconds timeout) noexcept |
| 设置发送超时时间 | |
| optional< milliseconds > | get_receive_timeout () const noexcept |
| 获取接收超时时间 | |
| bool | set_receive_timeout (milliseconds timeout) noexcept |
| 设置接收超时时间 | |
| optional< ip_address > | local_endpoint () const |
| 获取本地端点地址 | |
| optional< ip_address > | remote_endpoint () const |
| 获取远程端点地址 | |
| void | bind (const ip_address &endpoint) |
| 绑定socket到本地地址 | |
| void | listen (int backlog) |
| 开始监听连接(TCP) | |
| native_handle_type | release () noexcept |
| 释放socket所有权 | |
额外继承的成员函数 | |
| Public 类型 继承自 neforce::socket_base | |
| enum class | type : int32_t { STREAM = SOCK_STREAM , DGRAM = SOCK_DGRAM , RAW = SOCK_RAW , RDM = SOCK_RDM , SEQPACKET = SOCK_SEQPACKET , CLOEXEC = SOCK_CLOEXEC , NONBLOCK = SOCK_NONBLOCK , DCCP = SOCK_DCCP } |
| socket 类型枚举 更多... | |
| enum class | protocol : int32_t { AUTO = 0 , ICMP = IPPROTO_ICMP , TCP = IPPROTO_TCP , UDP = IPPROTO_UDP , SCTP = IPPROTO_SCTP , RAW = IPPROTO_RAW , ESP = IPPROTO_ESP , AH = IPPROTO_AH , DCCP = IPPROTO_DCCP , IP = ETH_P_IP , ARP = ETH_P_ARP , IPV6 = ETH_P_IPV6 , LAT = ETH_P_LAT , VLAN = ETH_P_8021Q , LLDP = ETH_P_LLDP , MPLS = ETH_P_MPLS_UC } |
| socket 协议枚举 更多... | |
| using | native_handle_type |
| 平台原生句柄类型 | |
| using | family = ip_address::family |
| 网络地址族类型 | |
| 静态 Public 属性 继承自 neforce::socket_base | |
| static constexpr native_handle_type | invalid_handle |
| 无效句柄常量 | |
| Protected 属性 继承自 neforce::ip_socket | |
| family | family_ = family::UNDEF |
| 地址族 | |
| Protected 属性 继承自 neforce::socket_base | |
| native_handle_type | fd_ = invalid_handle |
| Socket句柄 | |
| bool | nonblocking_ {false} |
| 非阻塞模式状态跟踪 | |
TCP Acceptor类
实现TCP服务器的连接接受功能,负责监听端口并接受客户端连接。 继承自ip_socket,提供专门用于服务器端的操作。
主要功能:
使用示例:
在文件 tcp_acceptor.hpp 第 70 行定义.
|
nodiscard |
接受客户端连接(阻塞模式)
| socket_exception | 接受失败时抛出 |
| value_exception | acceptor未打开时抛出 |
阻塞等待直到有新连接到达,然后返回代表该连接的socket。 返回的socket可用于与客户端通信。
|
nodiscard |
接受客户端连接(非阻塞模式)
| socket_exception | 发生错误时抛出 |
非阻塞地尝试接受新连接。 如果没有待处理的连接,立即返回none。 需要先设置socket为非阻塞模式。
| void neforce::tcp_acceptor::async_accept | ( | io_context & | ctx, |
| cancellation_slot & | slot, | ||
| function< void(error_code, tcp_socket)> | handler ) |
异步接受连接(带取消槽)
| ctx | 异步 I/O 执行上下文 |
| slot | 取消槽 |
| handler | 完成回调 void(error_code, tcp_socket) |
|
inline |
| void neforce::tcp_acceptor::async_accept | ( | io_context & | ctx, |
| function< void(error_code, tcp_socket)> | handler ) |
异步接受客户端连接
| ctx | 异步 I/O 执行上下文 |
| handler | 完成回调 void(error_code, tcp_socket) |
异步等待新连接。新连接到达时 handler 在 io_context::run() 线程中执行。 acceptor 必须是已打开且处于监听状态。
被这些函数引用 async_accept(), async_accept(), async_accept() , 以及 async_accept().
|
inline |
异步接受—任意可调用对象
| Token | 可调用对象类型,需满足 void(error_code, tcp_socket) 签名 |
| ctx | 异步 I/O 执行上下文 |
| token | 完成令牌 |
在文件 tcp_acceptor.hpp 第 142 行定义.
引用了 async_accept() , 以及 neforce::forward().
|
inline |
异步接受—use_awaitable
| ctx | 异步 I/O 执行上下文 |
在文件 tcp_acceptor.hpp 第 171 行定义.
引用了 async_accept() , 以及 neforce::use_awaitable.
|
inline |
异步接受—use_future
| ctx | 异步 I/O 执行上下文 |
在文件 tcp_acceptor.hpp 第 151 行定义.
引用了 async_accept() , 以及 neforce::use_future.
| void neforce::tcp_acceptor::open | ( | const ip_address & | endpoint, |
| int | backlog = SOMAXCONN ) |
打开并开始监听
| endpoint | 要绑定的本地端点地址 |
| backlog | 连接队列大小(默认SOMAXCONN) |
| socket_exception | 绑定或监听失败时抛出 |
| value_exception | 端点无效时抛出 |
| void neforce::socket_base::open | ( | family | f, |
| type | t = type::STREAM, | ||
| protocol | p = protocol::AUTO ) |