Tcp服务端类 更多...
#include <sttnet.h>
Public 成员函数 | |
| TcpServer (const int &maxFD=10000, const bool &security_open=true, const int &connectionNumLimit=20, const int &connectionRateLimit=6, const int &buffer_size=8, const int &requestRate=12, const int &checkFrequency=1, const int &connectionTimeout=1800) | |
| 构造函数,默认是启用安全模块。限制一个ip最大连接为20;同一个ip每秒最快连接速度为6 更多... | |
| 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 加密并配置服务器端证书与密钥 更多... | |
| void | redrawTLS () |
| 撤销TLS加密,ca证书等 更多... | |
| bool | setFunction (std::function< bool(TcpFDHandler &k, TcpFDInf &inf)> fc) |
| 设置收到客户端消息后的回调函数 注册一个回调函数 更多... | |
| bool | stopListen () |
| 停止监听 更多... | |
| bool | close () |
| 关闭监听和所有已连接的套接字 更多... | |
| bool | close (const int &fd) |
| 关闭某个套接字的连接 更多... | |
| bool | isListen () |
| 返回对象的监听状态 更多... | |
| SSL * | getSSL (const int &fd) |
| 查询和服务端的连接,传入套接字,返回加密的SSL句柄 更多... | |
| ~TcpServer () | |
| TcpServer 类的析构函数 更多... | |
Protected 成员函数 | |
| bool | allowRequest (const int &cclientfd) |
| void | connectionDetect () |
Protected 属性 | |
| unsigned long | buffer_size |
| int | maxFD |
| security::ConnectionLimiter | connectionLimiter |
| TcpFDInf * | clientfd |
| int | flag1 =true |
| std::queue< QueueFD > * | fdQueue |
| std::mutex * | lq1 |
| std::condition_variable * | cv |
| int | consumerNum |
| std::mutex | lco1 |
| bool | unblock |
| SSL_CTX * | ctx =nullptr |
| bool | TLS =false |
| int | requestRate |
| int | checkFrequency |
| int | connectionTimeout |
| bool | security_open |
Tcp服务端类
|
inline |
构造函数,默认是启用安全模块。限制一个ip最大连接为20;同一个ip每秒最快连接速度为6
| maxFD | 服务对象的最大接受连接数 默认为10000 |
| security_open | true:开启安全模块 false:关闭安全模块 (默认为开启) |
| connectionNumLimit | 同一个ip连接数目的上限 |
| connectionRateLimit | 同一个ip每秒钟连接数目的上限 |
| buffer_size | 同一个连接允许传输的最大数据量(单位为kb) 默认为8kb |
| requestRatte | 同一个连接一秒内允许的最大请求数量 (默认为12次) |
| checkFrequency | 检查僵尸连接的频率(单位分钟) 默认为1分钟 -1为不做检查 |
| connectionTimeout | 连接多少秒内没有任何反应就视为僵尸连接 (单位为秒) 默认60秒 -1为无限制 |
|
inline |
TcpServer 类的析构函数
|
protected |
| bool stt::network::TcpServer::close | ( | ) |
关闭监听和所有已连接的套接字
| bool stt::network::TcpServer::close | ( | const int & | fd | ) |
关闭某个套接字的连接
| fd | 需要关闭的套接字 |
|
protected |
| SSL* stt::network::TcpServer::getSSL | ( | const int & | fd | ) |
查询和服务端的连接,传入套接字,返回加密的SSL句柄
|
inline |
返回对象的监听状态
| void stt::network::TcpServer::redrawTLS | ( | ) |
撤销TLS加密,ca证书等
|
inline |
设置收到客户端消息后的回调函数 注册一个回调函数
| fc | 一个函数或函数对象,用于收到客户端消息后处理逻辑 -参数:TcpFDHandler &k - 和客户端连接的套接字的引用 TcpFDInf &inf - 客户端信息,还有处理进度,状态机信息等 -返回: bool - 返回true处理成功,返回false处理失败 |
| bool stt::network::TcpServer::setTLS | ( | const char * | cert, |
| const char * | key, | ||
| const char * | passwd, | ||
| const char * | ca | ||
| ) |
启用 TLS 加密并配置服务器端证书与密钥
本函数用于初始化 OpenSSL,并为 TCP 服务器启用 TLS(SSL/TLSv1 协议族)支持。 它加载服务器端证书、私钥和可选的 CA 根证书,用于实现对等验证。
若已启用 TLS,将自动重建(重载)上下文。
| cacert | 服务器端证书链文件路径(通常为 PEM 格式,包括中间证书) |
| key | 私钥文件路径(与证书匹配的 PEM 格式密钥) |
| passwd | 私钥文件的密码(若密钥加密,可为空字符串) |
| ca | CA 根证书路径,用于验证客户端证书(PEM 格式) |
SSLv23_method(),实际上支持 SSLv3/TLSv1/TLSv1.1/TLSv1.2 及更高版本(具体取决于 OpenSSL 版本与配置)SSL_VERIFY_FAIL_IF_NO_PEER_CERT,即:| 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 |
1.8.5