IPBS_Station/SDK/CCServlet/include/CCWebServlet.h

76 lines
2.3 KiB
C
Raw Permalink Normal View History

2024-08-13 17:07:34 +08:00
#ifndef CCWeb_Servlet_H
#define CCWeb_Servlet_H
#pragma once
#include "CCResponse.h"
2024-11-01 16:09:31 +08:00
#include "CCTimeData.h"
#include "CCWebSocketInfo.h"
2024-08-13 17:07:34 +08:00
2024-11-01 16:09:31 +08:00
#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
};
2024-08-13 17:07:34 +08:00
class CCWebServlet:public CCResponse
{
private:
using RequestFunc = std::function<void(CCRequest& ,CCResponse&)>;
2024-11-01 16:09:31 +08:00
using RecvDataFunc = std::function<void(CCWebSocketInfo&)>;
2024-08-13 17:07:34 +08:00
std::map<std::string,RequestFunc> RequestFun;
CCString ServerIP;
CCSocket m_Socket;
2024-11-01 16:09:31 +08:00
int numThreads = 5,ListenMax = 50,RootLen = 0,ServerPort;
2024-08-13 17:07:34 +08:00
bool FlagRun = true;
2024-11-01 16:09:31 +08:00
PromptLevel Print = DEBUG;
2024-08-13 17:07:34 +08:00
CCThread m_Thread;
std::vector<std::string> PathSix;
CORS CORSConfig;
std::mutex m_Mutex;
2024-11-01 16:09:31 +08:00
std::function<void()> WebSocketSignal;
RequestFunc WebSignalFun;
CCThreadPool m_ThreadPool;
RecvDataFunc MFunc,CFunc,IFunc,EFunc;
2024-08-13 17:07:34 +08:00
public:
public:
CCWebServlet() = default;
void SetThreadNumber(int headcount);
2024-11-01 16:09:31 +08:00
void SetWebServlet(CCString UrlPath,RequestFunc RFunc);
2024-08-13 17:07:34 +08:00
void SetAddRootPath(const CCString& rootPath);
2024-11-01 16:09:31 +08:00
void SetWebServletFun(RequestFunc RFunc);
2024-08-13 17:07:34 +08:00
bool Sign() const;
bool Start(CCString IP,int port);
2024-11-01 16:09:31 +08:00
bool Init(CCString IP,int port,bool ChokeUpSock = true);
int Running();
2024-08-13 17:07:34 +08:00
void Close();
void SetCorsConfig(CORS& cors);
2024-11-01 16:09:31 +08:00
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();
2024-08-13 17:07:34 +08:00
private:
2024-11-01 16:09:31 +08:00
void MessagePrompt(const CCString& message);
2024-08-13 17:07:34 +08:00
void ProcessRequest();
void ResponseData(CCSocket & socket);
2024-11-01 16:09:31 +08:00
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);
2024-08-13 17:07:34 +08:00
};
#endif