|
STTNet 0.7.0
|
Tcp服务端类 更多...
#include <sttnet.h>
Public 成员函数 | |
| void | putTask (const std::function< int(TcpFDHandler &k, TcpInformation &inf)> &fun, TcpFDHandler &k, TcpInformation &inf) |
| 把一个任务放入工作线程池由工作线程完成 | |
| TcpServer (const unsigned long long &maxFD=1000000, const int &buffer_size=256, const size_t &finishQueue_cap=65536, const bool &security_open=true, const int &connectionNumLimit=20, const int &connectionSecs=1, const int &connectionTimes=6, const int &requestSecs=1, const int &requestTimes=40, const int &checkFrequency=60, const int &connectionTimeout=60) | |
| 构造函数,默认是允许最大1000000个连接,每个连接接收缓冲区最大为256kb,启用安全模块。 | |
| bool | startListen (const int &port, const int &threads=8) |
| 打开Tcp服务器监听程序 | |
| bool | setTLS (const char *cert, const char *key, const char *passwd, const char *ca) |
| 以兼容模式启用双向 TLS(mTLS)。 | |
| bool | setTLS (const char *cert, const char *key, const char *passwd="") |
| 启用普通单向 TLS,适用于常见 HTTPS/WSS 服务。 | |
| bool | setTLS (const char *cert, const char *key, const char *passwd, const char *ca, TLSClientAuthMode clientAuth) |
| 启用 TLS 并显式选择客户端证书校验模式。 | |
| void | redrawTLS () |
| 撤销TLS加密,ca证书等 | |
| void | setSecuritySendBackFun (std::function< void(TcpFDHandler &k, TcpInformation &inf)> fc) |
| 设置违反信息安全策略时候的返回函数 | |
| void | setGlobalSolveFunction (std::function< bool(TcpFDHandler &k, TcpInformation &inf)> fc) |
| 设置全局备用函数 | |
| void | setFunction (const std::string &key, std::function< int(TcpFDHandler &k, TcpInformation &inf)> fc) |
| 设置key对应的收到客户端消息后的回调函数 | |
| void | setGetKeyFunction (std::function< int(TcpFDHandler &k, TcpInformation &inf)> parseKeyFun) |
| 设置解析出key的回调函数 | |
| bool | stopListen () |
| 停止监听 | |
| virtual bool | close () |
| 关闭监听和所有已连接的套接字 | |
| virtual bool | close (const int &fd) |
| 关闭某个套接字的连接 | |
| void | setConnectStrategy (const stt::security::RateLimitType &type) |
| 设置“连接速率限流”所使用的策略。 | |
| void | setRequestStrategy (const stt::security::RateLimitType &type) |
| 设置“IP 级请求限流”所使用的策略。 | |
| void | setPathStrategy (const stt::security::RateLimitType &type) |
| 设置“path 级请求限流”所使用的策略。 | |
| void | setPathLimit (const std::string &path, const int ×, const int &secs) |
| 设置某个路径的额外限流规则(path 级)。 | |
| void | setCloseFun (std::function< void(const int &fd)> closeFun) |
| 设置关闭tcp连接之后调用的函数 | |
| void | setMaxPendingWriteBytes (const size_t bytes) |
| 设置每连接待发送数据高水位。 | |
| void | setWriteBudgetPerEvent (const size_t bytes) |
| 设置 Reactor 单次处理一个连接的最大发送字节数。 | |
| void | setMaxHttpHeaderBytes (const size_t bytes) |
| 设置 HTTP 请求和 WebSocket 握手头上限。 | |
| void | setMaxPendingWorkerTasks (const size_t tasks) |
| 设置 WorkerPool 尚未开始执行的任务上限。 | |
| void | setReactorMessageBudgets (const size_t workerCompletions, const size_t sendReadyNotifications) |
| 设置单次 Reactor 唤醒处理的跨线程消息预算。 | |
| void | setSocketOptions (const ServerSocketOptions &options) |
| 设置新接收 TCP 连接的常用套接字参数。 | |
| void | setGracefulShutdownTimeout (const size_t milliseconds) |
| 设置 close()/stopListen() 等待在途请求和发送队列排空的最长时间。 | |
| ServerMetricsSnapshot | getMetrics () const noexcept |
| 获取服务器运行指标快照。 | |
| bool | isListen () |
| 返回对象的监听状态 | |
| int | getListenPort () const noexcept |
| 获取当前实际监听端口。 | |
| SSL * | getSSL (const int &fd) |
| 查询和服务端的连接,传入套接字,返回加密的SSL句柄 | |
| virtual | ~TcpServer () |
| TcpServer 类的析构函数 | |
Protected 类型 | |
| enum class | WriteFlushResult { Drained , WaitWrite , WaitRead , Reschedule , Error } |
Protected 成员函数 | |
| int | enqueueWrite (const std::shared_ptr< ConnectionWriteState > &state, std::string data) |
| void | requestQueuedClose (const std::shared_ptr< ConnectionWriteState > &state) |
| void | requestCloseAfterFlush (const int &fd, const uint64_t expectedConnection=0) |
| void | publishSendReady (const std::shared_ptr< ConnectionWriteState > &state) |
| void | notifyReactor () noexcept |
| bool | hasPendingReactorWork () |
| void | clearReactorQueues () |
| void | drainReactorWork (const int &epollFD) |
| size_t | drainWorkerResults (const size_t budget) |
| size_t | drainSendReady (const int &epollFD, const size_t budget) |
| void | applyAcceptedSocketOptions (const int &acceptedFD) const noexcept |
| void | reportReactorStartup (const bool success) |
| void | advanceGracefulDrain () |
| void | prepareHandler (TcpFDHandler &handler, const int &fd) |
| void | prepareQueuedHandler (TcpFDHandler &handler, const int &fd) |
| void | prepareQueuedHandler (TcpFDHandler &handler, const int &fd, const uint64_t expectedConnection) |
| WriteFlushResult | flushConnectionWrites (TcpFDInf &connection) |
| bool | updateConnectionEvents (const int &epollFD, TcpFDInf &connection, const bool &wantWrite) |
| void | handleSendReady (const int &epollFD, SendReadyMessage message) |
| void | publishWorkerResult (WorkerMessage message) |
| void | scheduleBufferedRead (const int &fd, const uint64_t connection) |
Tcp服务端类
|
strongprotected |
|
inline |
构造函数,默认是允许最大1000000个连接,每个连接接收缓冲区最大为256kb,启用安全模块。
| maxFD | 服务对象的最大接受连接数 默认为1000000 |
| buffer_size | 同一个连接允许传输的最大数据量(单位为kb) 默认为256kb |
| finishQueue_cap | Worker 完成队列(Worker → Reactor)的容量,必须为 2 的幂。 |
| security_open | true:开启安全模块 false:关闭安全模块 (默认为开启) |
| connectionNumLimit | 同一个ip连接数目的上限(默认20) |
| connectionSecs | 连接速率统计窗口长度(单位:秒)(默认1秒) |
| connectionTimes | 在 connectionSecs 秒内允许的最大连接次数 (默认6次) |
| requestSecs | 请求速率统计窗口长度(单位:秒)(默认1秒) |
| requestTimes | 在秒requestSecs内允许的最大请求数量(默认40次) |
| checkFrequency | 检查僵尸连接的频率(单位秒钟) -1为不做检查 (默认为60秒) |
| connectionTimeout | 连接多少秒内没有任何反应就视为僵尸连接 (单位为秒) -1为无限制 (默认60秒) |
|
inlinevirtual |
TcpServer 类的析构函数
|
protected |
|
protectednoexcept |
|
protected |
|
virtual |
关闭监听和所有已连接的套接字
被 stt::network::HttpServer , 以及 stt::network::WebSocketServer 重载.
|
virtual |
关闭某个套接字的连接
| fd | 需要关闭的套接字 |
被 stt::network::HttpServer , 以及 stt::network::WebSocketServer 重载.
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
inlinenoexcept |
获取当前实际监听端口。
|
inlinenoexcept |
| SSL * stt::network::TcpServer::getSSL | ( | const int & | fd | ) |
查询和服务端的连接,传入套接字,返回加密的SSL句柄
|
protected |
|
protected |
|
inline |
返回对象的监听状态
|
protectednoexcept |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
| void stt::network::TcpServer::putTask | ( | const std::function< int(TcpFDHandler &k, TcpInformation &inf)> & | fun, |
| TcpFDHandler & | k, | ||
| TcpInformation & | inf ) |
把一个任务放入工作线程池由工作线程完成
| fun | 放入工作线程池的可执行对象 -参数:TcpFDHandler &k - 和客户端连接的套接字的操作对象的引用 TcpFDInf &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:-2:处理失败并且需要关闭连接 -1:处理失败但不需要关闭连接 1:处理成功 |
| k | 和客户端连接的套接字的操作对象的引用 |
| inf | 客户端信息的引用,保存数据,处理进度,状态机信息等 |
| void stt::network::TcpServer::redrawTLS | ( | ) |
撤销TLS加密,ca证书等
|
protected |
|
protected |
|
protected |
|
protected |
Reactor 内延续解析已在用户态缓冲区中的 HTTP 流水线请求。
|
inline |
设置关闭tcp连接之后调用的函数
|
inline |
设置“连接速率限流”所使用的策略。
| type | 策略类型,见 stt::security::RateLimitType 说明。 |
|
inline |
设置key对应的收到客户端消息后的回调函数
| key | 找到对应回调函数的key |
| fc | 一个函数或函数对象,用于收到客户端消息后处理逻辑 -参数:TcpFDHandler &k - 和客户端连接的套接字的操作对象的引用 TcpFDInf &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:-2:处理失败并且需要关闭连接 -1:处理失败但不需要关闭连接 0:处理流程已经扔入工作线程池,需要等待处理完成 1:处理成功 |
|
inline |
设置解析出key的回调函数
| parseKeyFun | 解析key的回调函数 -参数:TcpFDHandler &k - 和客户端连接的套接字的操作对象的引用 TcpFDInf &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:-2:处理失败并且需要关闭连接 -1:处理失败但不需要关闭连接 0:处理流程已经扔入工作线程池,需要等待处理完成 1:处理成功 |
|
inline |
设置全局备用函数
| fc | 一个函数或函数对象,用于收到客户端消息后处理逻辑 -参数:TcpFDHandler &k - 和客户端连接的套接字的操作对象的引用 TcpInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:true:处理成功 false:处理失败 会关闭连接 |
|
inline |
设置 close()/stopListen() 等待在途请求和发送队列排空的最长时间。
| milliseconds | 超时毫秒数;默认 5000。设置为 0 表示立即强制停止。 |
|
inline |
设置 HTTP 请求和 WebSocket 握手头上限。
| bytes | 最大请求头字节数,最小接受值为 1024;默认 64 KiB。 |
|
inline |
设置 WorkerPool 尚未开始执行的任务上限。
| tasks | 最大排队任务数,必须大于 0;默认 65536。 |
|
inline |
设置每连接待发送数据高水位。
| bytes | 最大待发送字节数,必须大于 0;默认 4 MiB。 |
|
inline |
设置某个路径的额外限流规则(path 级)。
| path | 需要额外限流的路径,例如 "/login"、"/register"。 |
| times | 在 secs 秒内允许的最大请求次数。 |
| secs | 统计窗口长度(秒)。 |
|
inline |
设置“path 级请求限流”所使用的策略。
| type | 策略类型,见 stt::security::RateLimitType 说明。 |
|
inline |
设置单次 Reactor 唤醒处理的跨线程消息预算。
| workerCompletions | 单次最多处理的 Worker 完成消息数。 |
| sendReadyNotifications | 单次最多处理的待发送连接通知数。 |
|
inline |
设置“IP 级请求限流”所使用的策略。
| type | 策略类型,见 stt::security::RateLimitType 说明。 |
|
inline |
设置违反信息安全策略时候的返回函数
| fc | 一个函数或函数对象,用于收到客户端消息后处理逻辑 -参数:TcpFDHandler &k - 和客户端连接的套接字的操作对象的引用 TcpInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:true:处理成功 false:处理失败 会关闭连接 |
|
inline |
设置新接收 TCP 连接的常用套接字参数。
| options | TCP_NODELAY、keepalive、收发缓冲、deferred accept、Fast Open 及 backlog 配置。 |
| bool stt::network::TcpServer::setTLS | ( | const char * | cert, |
| const char * | key, | ||
| const char * | passwd, | ||
| const char * | ca ) |
以兼容模式启用双向 TLS(mTLS)。
| cert | 服务器证书链 PEM 文件。 |
| key | 与证书匹配的 PEM 私钥。 |
| passwd | 私钥密码;未加密时可传空字符串。 |
| ca | 用于验证客户端证书的 CA PEM 文件。 |
| bool stt::network::TcpServer::setTLS | ( | const char * | cert, |
| const char * | key, | ||
| const char * | passwd, | ||
| const char * | ca, | ||
| TLSClientAuthMode | clientAuth ) |
启用 TLS 并显式选择客户端证书校验模式。
| cert | 服务器证书链 PEM 文件。 |
| key | 与证书匹配的 PEM 私钥。 |
| passwd | 私钥密码;未加密时可为空。 |
| ca | 客户端证书 CA;Optional/Required 模式必须提供。 |
| clientAuth | 客户端证书校验模式。 |
| bool stt::network::TcpServer::setTLS | ( | const char * | cert, |
| const char * | key, | ||
| const char * | passwd = "" ) |
启用普通单向 TLS,适用于常见 HTTPS/WSS 服务。
| cert | 服务器证书链 PEM 文件。 |
| key | 与证书匹配的 PEM 私钥。 |
| passwd | 私钥密码;默认无密码。 |
|
inline |
设置 Reactor 单次处理一个连接的最大发送字节数。
| bytes | 单次预算,必须大于 0;默认 256 KiB。 |
| bool stt::network::TcpServer::startListen | ( | const int & | port, |
| const int & | threads = 8 ) |
打开Tcp服务器监听程序
| port | 监听的端口 |
| threads | 消费者线程的数量 (默认为8) |
| bool stt::network::TcpServer::stopListen | ( | ) |
停止监听
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
mutableprotected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |