STTNet
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 宏定义 
Public 成员函数 | 所有成员列表
stt::network::HttpServer类 参考

Http/HttpServer 服务端操作类 更多...

#include <sttnet.h>

类 stt::network::HttpServer 继承关系图:
stt::network::TcpServer

Public 成员函数

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 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服务器监听程序 更多...
 
 ~HttpServer ()
 析构函数 更多...
 
- Public 成员函数 继承自 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 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 加密并配置服务器端证书与密钥 更多...
 
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 ()
 停止监听 更多...
 
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 &times, const int &secs)
 设置某个路径的额外限流规则(path 级)。 更多...
 
void setCloseFun (std::function< void(const int &fd)> closeFun)
 设置关闭tcp连接之后调用的函数 更多...
 
bool isListen ()
 返回对象的监听状态 更多...
 
SSL * getSSL (const int &fd)
 查询和服务端的连接,传入套接字,返回加密的SSL句柄 更多...
 
 ~TcpServer ()
 TcpServer 类的析构函数 更多...
 

额外继承的成员函数

- Protected 属性 继承自 stt::network::TcpServer
std::queue< WorkerMessagefinishQueue
 
stt::system::WorkerPoolworkpool
 
unsigned long buffer_size
 
unsigned long long maxFD
 
security::ConnectionLimiter connectionLimiter
 
TcpFDInfclientfd
 
int flag1 =true
 
bool unblock
 
SSL_CTX * ctx =nullptr
 
bool TLS =false
 
bool security_open
 
int workerEventFD
 
int serverType
 
int connectionSecs
 
int connectionTimes
 
int requestSecs
 
int requestTimes
 
int checkFrequency
 

详细描述

Http/HttpServer 服务端操作类

注解
支持http/1.0 1.1

构造及析构函数说明

stt::network::HttpServer::HttpServer ( const unsigned long long &  maxFD = 1000000,
const int &  buffer_size = 256,
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 
)
inline

构造函数,默认是允许最大1000000个连接,每个连接接收缓冲区最大为256kb,启用安全模块。

注解
打开安全模块会对性能有影响
参数
maxFD服务对象的最大接受连接数 默认为1000000
buffer_size同一个连接允许传输的最大数据量(单位为kb) 默认为256kb
security_opentrue:开启安全模块 false:关闭安全模块 (默认为开启)
connectionNumLimit同一个ip连接数目的上限(默认10)
connectionSecs连接速率统计窗口长度(单位:秒)(默认1秒)
connectionTimes在 connectionSecs 秒内允许的最大连接次数 (默认3次)
requestSecs请求速率统计窗口长度(单位:秒)(默认1秒)
requestTimes在秒requestSecs内允许的最大请求数量(默认20次)
checkFrequency检查僵尸连接的频率(单位:秒) -1为不做检查 (默认为30秒)
connectionTimeout连接多少秒内没有任何反应就视为僵尸连接 (单位为秒) -1为无限制 (默认30秒)
stt::network::HttpServer::~HttpServer ( )
inline

析构函数

成员函数说明

void stt::network::HttpServer::putTask ( const std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)> &  fun,
HttpServerFDHandler k,
HttpRequestInformation inf 
)

把一个任务放入工作线程池由工作线程完成

注解
应当把速度慢、会阻塞的io任务放入工作线程池
警告
可执行对象必须严格按照规定的返回值返回
参数
fun放入工作线程池的可执行对象 -参数:HttpServerFDHandler &k - 和客户端连接的套接字的操作对象的引用 HttpRequestInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:-2:处理失败并且需要关闭连接 -1:处理失败但不需要关闭连接 1:处理成功
k和客户端连接的套接字的操作对象的引用
inf客户端信息的引用,保存数据,处理进度,状态机信息等
void stt::network::HttpServer::setFunction ( const std::string &  key,
std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)>  fc 
)
inline

设置key对应的收到客户端消息后的回调函数

注解
可以设置多个 ,框架会根据设置顺序依次执行回调函数;也可以设置扔入工作线程池处理的流程,注意设置不同的返回值即可。
警告
可执行对象必须严格按照规定的返回值返回
参数
key找到对应回调函数的key
fc一个函数或函数对象,用于收到客户端消息后处理逻辑 -参数:HttpServerFDHandler &k - 和客户端连接的套接字的操作对象的引用 HttpRequestInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:-2:处理失败并且需要关闭连接 -1:处理失败但不需要关闭连接 0:处理流程已经扔入工作线程池,需要等待处理完成 1:处理成功
httpserver->setFunction("/ping",[](HttpServerFDHandler &k,HttpRequestInformation &inf)->int
{
k.sendBack("pong");
return 1;
});
httpserver->setFunction("/ping",[](HttpServerFDHandler &k,HttpRequestInformation &inf)->int
{
httpserver->putTask([](HttpServerFDHandler &k,HttpRequestInformation &inf)->int
{
k.sendBack("pong");
return 1;
},k,inf);
return 0;
});
void stt::network::HttpServer::setGetKeyFunction ( std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)>  parseKeyFun)
inline

设置解析出key的回调函数

注解
根据传入的参数把key存入TcpInformation信息中的ctx哈希表。框架会根据key的值找到你注册的处理函数。
警告
可执行对象必须严格按照规定的返回值返回
参数
parseKeyFun解析key的回调函数 -参数:HttpServerFDHandler &k - 和客户端连接的套接字的操作对象的引用 HttpRequestInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:-2:处理失败并且需要关闭连接 -1:处理失败但不需要关闭连接 0:处理流程已经扔入工作线程池,需要等待处理完成 1:处理成功
httpserver->setGetKeyFunction([](HttpServerFDHandler &k,HttpRequestInformation &inf)->int
{
inf.ctx["key"]=inf.loc;
return 1;
});
void stt::network::HttpServer::setGlobalSolveFunction ( std::function< int(HttpServerFDHandler &k, HttpRequestInformation &inf)>  fc)
inline

设置全局备用函数

注解
找不到对应回调函数的时候会调用全局备用函数.可以设置多个 ,框架会根据设置顺序依次执行回调函数;也可以设置扔入工作线程池处理的流程,注意设置不同的返回值即可。
参数
fc一个函数或函数对象,用于收到客户端消息后处理逻辑 -参数:HttpServerFDHandler &k - 和客户端连接的套接字的操作对象的引用 HttpRequestInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等
  • -返回值:-2:处理失败并且需要关闭连接 -1:处理失败但不需要关闭连接 0:处理流程已经扔入工作线程池,需要等待处理完成 1:处理成功
void stt::network::HttpServer::setSecuritySendBackFun ( std::function< void(HttpServerFDHandler &k, HttpRequestInformation &inf)>  fc)
inline

设置违反信息安全策略时候的返回函数

注解
违反信息安全策略时候的返回函数,调用完就关闭连接
参数
key找到对应回调函数的key
fc一个函数或函数对象,用于收到客户端消息后处理逻辑 -参数:HttpServerFDHandler &k - 和客户端连接的套接字的操作对象的引用 HttpRequestInformation &inf - 客户端信息,保存数据,处理进度,状态机信息等 -返回值:true:处理成功 false:处理失败 会关闭连接
bool stt::network::HttpServer::startListen ( const int &  port,
const int &  threads = 8 
)
inline

打开Http服务器监听程序

参数
port监听的端口
threads消费者线程的数量 (默认为8)
返回
true:打开监听程序成功 false:打开监听程序失败

该类的文档由以下文件生成: