#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(PH1), std::forward(PH2)); } #define CC_WSFunClass(Fun) [this](auto && PH1) { Fun(std::forward(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; using RecvDataFunc = std::function; std::map 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 PathSix; CORS CORSConfig; std::mutex m_Mutex; std::function 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 GetIPS(); private: void MessagePrompt(const CCString& message); void ProcessRequest(); void ResponseData(CCSocket & socket); CCString GetFileHeaders(CCString& str,CCSocket &socket,std::map &map); int unPackingWSFrameData(char *msg,int msgLen,std::vector &outBuf,CCWebSocketInfo& wsInfo); void constructCloseFrame(unsigned char *frame, int status_code, const char *reason); }; #endif