|
NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
|
TCP服务器基类 更多...
#include <tcp_server.hpp>
Public 类型 | |
| using | client_handler_t = function<void(tcp_socket)> |
| 客户端处理器类型 | |
| using | exception_handler_t = function<void(const exception&)> |
| 异常处理器类型 | |
Public 成员函数 | |
| tcp_server_base (ports port, size_t worker_count=thread_pool::max_thread_threshhold()) | |
| 构造函数 | |
| 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 () |
| 停止服务器 | |
| NEFORCE_NODISCARD bool | is_running () const noexcept |
| 检查服务器是否运行中 | |
| NEFORCE_NODISCARD ports | port () const noexcept |
| 获取监听端口 | |
Protected 成员函数 | |
| void | accept_loop () |
| 接受连接的主循环 | |
| virtual void | handle_client (tcp_socket client) |
| 处理单个客户端连接 | |
| virtual optional< tcp_socket > | accept_one ()=0 |
| 创建一个客户端连接 | |
| virtual void | create_acceptor (const ip_address &endpoint, int backlog)=0 |
| 创建并配置acceptor | |
Protected 属性 | |
| unique_ptr< tcp_acceptor > | acceptor_ |
| TCP接受器 | |
| ports | port_ |
| 监听端口 | |
| atomic< bool > | running_ {false} |
| 运行标志 | |
| vector< thread > | worker_threads_ |
| 工作线程列表 | |
| thread_pool | client_pool_ |
| 客户端处理线程池 | |
| client_handler_t | client_handler_ |
| 客户端处理器 | |
| exception_handler_t | exception_handler_ |
| 异常处理器 | |
|
inlinevirtual |
|
protected |
接受连接的主循环
该循环调用派生类实现的 accept_one() 获取已就绪的客户端 socket。 获取成功后提交至线程池,由 handle_client() 处理。
|
protectedpure virtual |
创建一个客户端连接
派生类实现具体的连接接受逻辑。 支持阻塞和非阻塞模式。
|
protectedpure virtual |
创建并配置acceptor
| endpoint | 监听地址 |
| backlog | 连接队列大小 |
派生类实现具体的acceptor创建和配置。
|
inlineprotectedvirtual |
处理单个客户端连接
| client | 已建立连接的 socket |
调用客户端处理器处理连接。 派生类可重写此方法以自定义处理逻辑。
在文件 tcp_server.hpp 第 63 行定义.
引用了 client_handler_ , 以及 move().
|
inlinenoexcept |
|
inlinenoexcept |
获取监听端口
在文件 tcp_server.hpp 第 152 行定义.
引用了 port_.
被这些函数引用 ssl_server::ssl_server() , 以及 tcp_server_base().
| bool tcp_server_base::set_client_handler | ( | client_handler_t | handler | ) |
设置客户端处理器
| handler | 处理器函数 |
必须在服务器启动前设置。
| bool tcp_server_base::set_exception_handler | ( | exception_handler_t | handler | ) |
设置异常处理器
| handler | 处理器函数 |
|
virtualnoexcept |
| void tcp_server_base::stop | ( | ) |