|
NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
|
TCP服务器基类 更多...
#include <tcp_server.hpp>
Public 类型 | |
| using | client_handler_t = function<void(unique_ptr<tcp_socket>)> |
| 客户端处理器类型 | |
| using | exception_handler_t = function<void(const exception&)> |
| 异常处理器类型 | |
Public 成员函数 | |
| tcp_server_base (ports port, io_context &ctx, size_t worker_count=0) | |
| 构造函数 | |
| virtual | ~tcp_server_base () |
| 析构函数 | |
| bool | set_client_handler (client_handler_t handler) |
| 设置客户端处理器 | |
| bool | set_exception_handler (exception_handler_t handler) |
| 设置异常处理器 | |
| virtual bool | start (int backlog=SOMAXCONN) noexcept |
| 启动服务器 | |
| void | stop () |
| 停止服务器 | |
| bool | is_running () const noexcept |
| 检查服务器是否运行中 | |
| ports | port () const noexcept |
| 获取监听端口 | |
Protected 成员函数 | |
| void | accept_loop () |
| 接受连接的主循环 | |
| virtual void | handle_client (unique_ptr< tcp_socket > client) |
| 处理单个客户端连接 | |
| virtual optional< unique_ptr< tcp_socket > > | accept_one ()=0 |
| 创建一个客户端连接 | |
| virtual void | create_acceptor (const ip_address &endpoint, int backlog)=0 |
| 创建并配置acceptor | |
Protected 属性 | |
| unique_ptr< tcp_acceptor > | acceptor_ |
| TCP接受器 | |
| io_context & | ctx_ |
| 异步 I/O 执行上下文 | |
| unique_ptr< io_context::work > | ctx_work_ |
| io_context 工作守卫 | |
| ports | port_ |
| 监听端口 | |
| atomic< bool > | running_ {false} |
| 运行标志 | |
| vector< thread > | worker_threads_ |
| 工作线程列表(含 accept 线程) | |
| size_t | worker_count_ |
| 工作线程数 | |
| client_handler_t | client_handler_ |
| 客户端处理器 | |
| exception_handler_t | exception_handler_ |
| 异常处理器 | |
| mutex | acceptor_mutex_ |
| TCP接受器互斥锁 | |
|
explicit |
构造函数
| port | 监听端口 |
| ctx | 异步 I/O 执行上下文 |
| worker_count | 工作线程数(默认最大) |
| value_exception | worker_count为0时抛出 |
引用了 port().
|
inlinevirtual |
|
protected |
接受连接的主循环
使用 io_context::add_fd + run_one() 驱动事件等待, 替代 raw poll/WSAEventSelect。专用线程确保 SSL 握手等阻塞操作 不影响 io_context 池线程。
|
protectedpure virtual |
创建一个客户端连接
派生类实现具体的连接接受逻辑。 支持阻塞和非阻塞模式。
|
protectedpure virtual |
创建并配置acceptor
| endpoint | 监听地址 |
| backlog | 连接队列大小 |
派生类实现具体的acceptor创建和配置。
|
inlineprotectedvirtual |
处理单个客户端连接
| client | 已建立连接的 socket |
调用客户端处理器处理连接。 派生类可重写此方法以自定义处理逻辑。
在文件 tcp_server.hpp 第 66 行定义.
引用了 client_handler_ , 以及 neforce::move().
|
inlinenodiscardnoexcept |
|
inlinenodiscardnoexcept |
获取监听端口
在文件 tcp_server.hpp 第 156 行定义.
引用了 port_.
被这些函数引用 neforce::ssl_server::ssl_server() , 以及 tcp_server_base().
| bool neforce::tcp_server_base::set_client_handler | ( | client_handler_t | handler | ) |
设置客户端处理器
| handler | 处理器函数 |
必须在服务器启动前设置。
| bool neforce::tcp_server_base::set_exception_handler | ( | exception_handler_t | handler | ) |
设置异常处理器
| handler | 处理器函数 |
|
virtualnoexcept |
启动服务器
| backlog | 连接队列大小(默认SOMAXCONN) |
创建acceptor,开始接受连接。 需要先设置客户端处理器。
被 neforce::ssl_server 重载.
| void neforce::tcp_server_base::stop | ( | ) |