2023-09-16 16:29:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "MiddleWareHandler.h"
|
|
|
|
#include "Queue.h"
|
|
|
|
#include "Request.h"
|
|
|
|
#include "Router.h"
|
|
|
|
#include "SocketManager.h"
|
|
|
|
#include "ThreadPool.h"
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace VWeb {
|
|
|
|
class Server;
|
|
|
|
typedef std::function<const void(Request &, const std::string &body)>
|
|
|
|
ParseFunction;
|
|
|
|
class RequestHandler {
|
|
|
|
public:
|
|
|
|
explicit RequestHandler(const Ref<SocketManager> &manager);
|
|
|
|
void InitThreads(int count);
|
|
|
|
void AddRequest(Ref<Request> &request);
|
|
|
|
void Stop();
|
|
|
|
void SetRouter(Ref<Router> &router);
|
|
|
|
void AddSendResponse(SendData);
|
2024-05-13 18:23:27 +02:00
|
|
|
void CloseRequest(int id) const;
|
2023-09-16 16:29:03 +02:00
|
|
|
Ref<MiddleWareHandler> &Middleware() { return m_MiddleWare; }
|
|
|
|
bool HasBodyParser(const std::string &key) {
|
|
|
|
return m_ParseFunctions.contains(key);
|
|
|
|
}
|
|
|
|
ParseFunction &GetBodyParser(const std::string &key) {
|
|
|
|
return m_ParseFunctions.at(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
std::unordered_map<std::string, ParseFunction> m_ParseFunctions;
|
|
|
|
Ref<Router> m_Router{nullptr};
|
|
|
|
Ref<SocketManager> m_SocketManager{nullptr};
|
|
|
|
Server *m_Server{nullptr};
|
|
|
|
SafeQueue<Ref<Request>> m_Requests{};
|
|
|
|
ThreadPool m_Pool{"RequestHandler"};
|
|
|
|
Ref<MiddleWareHandler> m_MiddleWare{nullptr};
|
|
|
|
friend Server;
|
|
|
|
};
|
|
|
|
} // namespace VWeb
|