IPBS_Station/SDK/CCServlet/include/CCWebServlet.h
2025-09-05 08:44:30 +08:00

76 lines
2.3 KiB
C++

#ifndef CCWeb_Servlet_H
#define CCWeb_Servlet_H
#pragma once
#include "CCResponse.h"
#include "CCTimeData.h"
#include "CCWebSocketInfo.h"
#define CC_ServletFunClass(Fun) [this](auto && PH1, auto && PH2) { Fun(std::forward<decltype(PH1)>(PH1), std::forward<decltype(PH2)>(PH2)); }
#define CC_WSFunClass(Fun) [this](auto && PH1) { Fun(std::forward<decltype(PH1)>(PH1)); }
#define CC_ServletFun(Fun) Fun
#define CC_WSFun(Fun) Fun
enum PromptLevel
{
ERROR_t = 0,
WARNING,
INFO,
DEBUG
};
class CCWebServlet:public CCResponse
{
private:
using RequestFunc = std::function<void(CCRequest& ,CCResponse&)>;
using RecvDataFunc = std::function<void(CCWebSocketInfo&)>;
std::map<std::string,RequestFunc> RequestFun;
CCString ServerIP;
CCSocket m_Socket;
int numThreads = 5,ListenMax = 50,RootLen = 0,ServerPort;
bool FlagRun = true;
PromptLevel Print = DEBUG;
CCThread m_Thread;
std::vector<std::string> PathSix;
CORS CORSConfig;
std::mutex m_Mutex;
std::function<void()> WebSocketSignal;
RequestFunc WebSignalFun;
CCThreadPool m_ThreadPool;
RecvDataFunc MFunc,CFunc,IFunc,EFunc;
public:
public:
CCWebServlet() = default;
void SetThreadNumber(int headcount);
void SetWebServlet(CCString UrlPath,RequestFunc RFunc);
void SetAddRootPath(const CCString& rootPath);
void SetWebServletFun(RequestFunc RFunc);
bool Sign() const;
bool Start(CCString IP,int port);
bool Init(CCString IP,int port,bool ChokeUpSock = true);
int Running();
void Close();
void SetCorsConfig(CORS& cors);
void SetWSOnMessage(RecvDataFunc RFunc);
void SetWSOnClose(RecvDataFunc CFunc);
void SetWSOnOpen(RecvDataFunc IFunc);
void SetWSOnError(RecvDataFunc EFunc);
void SetPrint(PromptLevel level);
CCThreadPool GetServletThreadPool();
std::vector<CCString> GetIPS();
private:
void MessagePrompt(const CCString& message);
void ProcessRequest();
void ResponseData(CCSocket & socket);
CCString GetFileHeaders(CCString& str,CCSocket &socket,std::map<CCString, CCString> &map);
int unPackingWSFrameData(char *msg,int msgLen,std::vector<char> &outBuf,CCWebSocketInfo& wsInfo);
void constructCloseFrame(unsigned char *frame, int status_code, const char *reason);
};
#endif