|
STTNet 0.7.0
|
Http/HttpServer 服务端操作类 More...
#include <sttnet.h>
Public Member Functions | |
| void | putTask (const std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> &fun, HttpServerFDHandler &k, HttpRequestInformation &inf) |
| 把一个任务放入工作线程池由工作线程完成 | |
| HttpServer (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=10, const int &connectionSecs=1, const int &connectionTimes=3, const int &requestSecs=1, const int &requestTimes=20, const int &checkFrequency=30, const int &connectionTimeout=30) | |
| 构造函数,默认是允许最大1000000个连接,每个连接接收缓冲区最大为256kb,启用安全模块。 | |
| void | setSecuritySendBackFun (std::function< void(HttpServerFDHandler &k, HttpRequestInformation &inf)> fc) |
| 设置违反信息安全策略时候的返回函数 | |
| void | setGlobalSolveFunction (std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> fc) |
| 设置全局备用函数 | |
| void | setFunction (const std::string &key, std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> fc) |
| 设置key对应的收到客户端消息后的回调函数 | |
| void | setGetKeyFunction (std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> parseKeyFun) |
| 设置解析出key的回调函数 | |
| bool | startListen (const int &port, const int &threads=8) |
| 打开Http服务器监听程序 | |
| bool | close () override |
| 关闭监听和所有已连接的套接字 | |
| bool | close (const int &fd) override |
| 关闭某个套接字的连接 | |
| ~HttpServer () | |
| 析构函数 | |
Public Member Functions inherited from stt::network::TcpServer | |
| 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 () |
| 停止监听 | |
| 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 类的析构函数 | |
Additional Inherited Members | |
Protected Types inherited from stt::network::TcpServer | |
| enum class | WriteFlushResult { Drained , WaitWrite , WaitRead , Reschedule , Error } |
Protected Member Functions inherited from stt::network::TcpServer | |
| 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) |
Protected Attributes inherited from stt::network::TcpServer | |
| system::MPSCQueue< WorkerMessage > | finishQueue |
| system::MPSCQueue< SendReadyMessage > | sendReadyQueue |
| stt::system::WorkerPool * | workpool =nullptr |
| unsigned long | buffer_size |
| unsigned long long | maxFD |
| security::ConnectionLimiter | connectionLimiter |
| std::unordered_map< int, TcpFDInf > | clientfd |
| std::atomic< bool > | flag1 {false} |
| bool | unblock |
| SSL_CTX * | ctx =nullptr |
| bool | TLS =false |
| std::mutex | tlsContextMutex |
| bool | security_open |
| std::atomic< int > | workerEventFD {-1} |
| std::atomic< bool > | workerWakePending {false} |
| std::recursive_mutex | lifecycleMutex |
| std::thread | reactorThread |
| std::mutex | reactorStartupMutex |
| std::condition_variable | reactorStartupCV |
| bool | reactorStartupComplete =false |
| bool | reactorStartupSuccess =false |
| std::atomic< bool > | gracefulDrainRequested {false} |
| std::mutex | gracefulShutdownMutex |
| std::condition_variable | gracefulShutdownCV |
| std::mutex | overflowFinishMutex |
| std::deque< WorkerMessage > | overflowFinishQueue |
| std::mutex | overflowSendReadyMutex |
| std::deque< SendReadyMessage > | overflowSendReadyQueue |
| std::mutex | writeRegistryMutex |
| std::unordered_map< int, std::weak_ptr< ConnectionWriteState > > | writeRegistry |
| std::deque< SendReadyMessage > | bufferedReadQueue |
| std::deque< SendReadyMessage > | timeoutCandidates |
| int | serverType |
| int | connectionSecs |
| int | connectionTimes |
| int | requestSecs |
| int | requestTimes |
| int | checkFrequency |
| uint64_t | connection_obj_fd |
| size_t | maxPendingWriteBytes =4UL*1024UL*1024UL |
| size_t | writeBudgetPerEvent =256UL*1024UL |
| size_t | maxHttpHeaderBytes =64UL*1024UL |
| size_t | maxPendingWorkerTasks =65536 |
| size_t | workerCompletionBudgetPerWake =2048 |
| size_t | sendReadyBudgetPerWake =256 |
| size_t | gracefulShutdownTimeoutMs =5000 |
| ServerSocketOptions | socketOptions |
| std::atomic< uint64_t > | metricAcceptedConnections {0} |
| std::atomic< uint64_t > | metricActiveConnections {0} |
| std::atomic< uint64_t > | metricClosedConnections {0} |
| std::atomic< uint64_t > | metricRejectedConnections {0} |
| std::atomic< uint64_t > | metricAcceptErrors {0} |
| std::atomic< uint64_t > | metricTLSHandshakeFailures {0} |
| std::atomic< uint64_t > | metricParsedHttpRequests {0} |
| std::atomic< uint64_t > | metricQueuedWriteBytes {0} |
| std::atomic< uint64_t > | metricSentBytes {0} |
| std::atomic< uint64_t > | metricPendingWriteBytes {0} |
| std::atomic< uint64_t > | metricPeakPendingWriteBytes {0} |
| std::atomic< uint64_t > | metricPeakPendingWorkerTasks {0} |
| std::atomic< uint64_t > | metricWriteOverflows {0} |
| std::atomic< uint64_t > | metricWorkerQueueOverflows {0} |
| std::atomic< uint64_t > | metricSendReadyQueueOverflows {0} |
| std::atomic< uint64_t > | metricWorkerTaskRejections {0} |
| std::atomic< uint64_t > | metricReactorWakeups {0} |
| std::atomic< uint64_t > | metricReactorWakeupsCoalesced {0} |
| std::atomic< uint64_t > | metricWriteSyscalls {0} |
| std::atomic< uint64_t > | metricBatchedWriteSyscalls {0} |
| std::atomic< uint64_t > | metricBatchedWriteBuffers {0} |
| std::atomic< uint64_t > | metricIdleTimeoutChecks {0} |
| std::atomic< uint64_t > | metricIdleTimeoutCloses {0} |
| std::atomic< uint64_t > | metricGracefulShutdownTimeouts {0} |
|
inline |
构造函数,默认是允许最大1000000个连接,每个连接接收缓冲区最大为256kb,启用安全模块。
| maxFD | 服务对象的最大接受连接数 默认为1000000 |
| buffer_size | 同一个连接允许传输的最大数据量(单位为kb) 默认为256kb |
| finishQueue_cap | Worker 完成队列(Worker → Reactor)的容量,必须为 2 的幂。 |
| security_open | true:开启安全模块 false:关闭安全模块 (默认为开启) |
| connectionNumLimit | 同一个ip连接数目的上限(默认10) |
| connectionSecs | 连接速率统计窗口长度(单位:秒)(默认1秒) |
| connectionTimes | 在 connectionSecs 秒内允许的最大连接次数 (默认3次) |
| requestSecs | 请求速率统计窗口长度(单位:秒)(默认1秒) |
| requestTimes | 在秒requestSecs内允许的最大请求数量(默认20次) |
| checkFrequency | 检查僵尸连接的频率(单位:秒) -1为不做检查 (默认为30秒) |
| connectionTimeout | 连接多少秒内没有任何反应就视为僵尸连接 (单位为秒) -1为无限制 (默认30秒) |
|
inline |
析构函数
|
overridevirtual |
关闭监听和所有已连接的套接字
Reimplemented from stt::network::TcpServer.
|
overridevirtual |
关闭某个套接字的连接
| fd | 需要关闭的套接字 |
Reimplemented from stt::network::TcpServer.
| void stt::network::HttpServer::putTask | ( | const std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> & | fun, |
| HttpServerFDHandler & | k, | ||
| HttpRequestInformation & | inf ) |
把一个任务放入工作线程池由工作线程完成
| fun | 放入工作线程池的可执行对象 -参数:HttpServerFDHandler &k - 和客户端连接的套接字的操作对象的引用 HttpRequestInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:-2:处理失败并且需要关闭连接 -1:处理失败但不需要关闭连接 1:处理成功 |
| k | 和客户端连接的套接字的操作对象的引用 |
| inf | 客户端信息的引用,保存数据,处理进度,状态机信息等 |
|
inline |
设置key对应的收到客户端消息后的回调函数
| key | 找到对应回调函数的 key。 |
| fc | 一个函数或函数对象,用于收到客户端消息后处理逻辑 -参数:HttpServerFDHandler &k - 和客户端连接的套接字的操作对象的引用 HttpRequestInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:-2:处理失败并且需要关闭连接 -1:处理失败但不需要关闭连接 0:处理流程已经扔入工作线程池,需要等待处理完成 1:处理成功 {
k.sendBack("pong");
return 1;
});
解析,响应Http/https请求的操作类 仅传入套接字,然后使用这个类进行Http的操作 Definition sttnet.h:3222 bool sendBack(const std::string &data, const std::string &header="", const std::string &code="200 OK", const std::string &header1="") 发送Http/Https响应 |
|
inline |
设置解析出key的回调函数
| parseKeyFun | 解析key的回调函数 -参数:HttpServerFDHandler &k - 和客户端连接的套接字的操作对象的引用 HttpRequestInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:-2:处理失败并且需要关闭连接 -1:处理失败但不需要关闭连接 0:处理流程已经扔入工作线程池,需要等待处理完成 1:处理成功 {
return 1;
});
std::unordered_map< std::string, std::any > ctx 所需的数据仓库 Definition sttnet.h:3199 |
|
inline |
设置全局备用函数
| fc | 一个函数或函数对象,用于收到客户端消息后处理逻辑 -参数:HttpServerFDHandler &k - 和客户端连接的套接字的操作对象的引用 HttpRequestInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等
|
|
inline |
设置违反信息安全策略时候的返回函数
| fc | 一个函数或函数对象,用于收到客户端消息后处理逻辑 -参数:HttpServerFDHandler &k - 和客户端连接的套接字的操作对象的引用 HttpRequestInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:true:处理成功 false:处理失败 会关闭连接 |
|
inline |
打开Http服务器监听程序
| port | 监听的端口 |
| threads | 消费者线程的数量 (默认为8) |