diff options
| author | Stefan Boberg <[email protected]> | 2023-10-11 14:59:25 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-10-11 14:59:25 +0200 |
| commit | 11f7f70b825c5b6784f5e2609463a1a9d1a0dabc (patch) | |
| tree | 98f65537f52327c354193afa98a29f9f838b42ff /src/zenhttp/httpsys.cpp | |
| parent | hide HttpAsioServer interface behind factory function (#463) (diff) | |
| download | zen-11f7f70b825c5b6784f5e2609463a1a9d1a0dabc.tar.xz zen-11f7f70b825c5b6784f5e2609463a1a9d1a0dabc.zip | |
pluggable asio transport (#460)
added pluggable transport based on asio. This is in an experimental state and is not yet a replacement for httpasio even though that is the ultimate goal
also moved plugin API header into dedicated part of the tree to clarify that it is meant to be usable in isolation, without any dependency on zencore et al
moved transport implementations into dedicated source directory in zenhttp
note that this adds code to the build but nothing should change at runtime since the instantiation of the new code is conditional and is inactive by default
Diffstat (limited to 'src/zenhttp/httpsys.cpp')
| -rw-r--r-- | src/zenhttp/httpsys.cpp | 88 |
1 files changed, 87 insertions, 1 deletions
diff --git a/src/zenhttp/httpsys.cpp b/src/zenhttp/httpsys.cpp index c7ed0bb2f..8401dcf83 100644 --- a/src/zenhttp/httpsys.cpp +++ b/src/zenhttp/httpsys.cpp @@ -16,6 +16,86 @@ #include <zenhttp/httpshared.h> #if ZEN_WITH_HTTPSYS +# define _WINSOCKAPI_ +# include <zencore/windows.h> +# include <zencore/workthreadpool.h> +# include "iothreadpool.h" + +# include <http.h> + +namespace spdlog { +class logger; +} + +namespace zen { + +/** + * @brief Windows implementation of HTTP server based on http.sys + * + * This requires elevation to function + */ +class HttpSysServer : public HttpServer +{ + friend class HttpSysTransaction; + +public: + explicit HttpSysServer(const HttpSysConfig& Config); + ~HttpSysServer(); + + // HttpServer interface implementation + + virtual int Initialize(int BasePort) override; + virtual void Run(bool TestMode) override; + virtual void RequestExit() override; + virtual void RegisterService(HttpService& Service) override; + virtual void Close() override; + + WorkerThreadPool& WorkPool(); + + inline bool IsOk() const { return m_IsOk; } + inline bool IsAsyncResponseEnabled() const { return m_IsAsyncResponseEnabled; } + +private: + int InitializeServer(int BasePort); + void Cleanup(); + + void StartServer(); + void OnHandlingNewRequest(); + void IssueNewRequestMaybe(); + + void RegisterService(const char* Endpoint, HttpService& Service); + void UnregisterService(const char* Endpoint, HttpService& Service); + +private: + spdlog::logger& m_Log; + spdlog::logger& m_RequestLog; + spdlog::logger& Log() { return m_Log; } + + bool m_IsOk = false; + bool m_IsHttpInitialized = false; + bool m_IsRequestLoggingEnabled = false; + bool m_IsAsyncResponseEnabled = true; + + WinIoThreadPool m_ThreadPool; + RwLock m_AsyncWorkPoolInitLock; + WorkerThreadPool* m_AsyncWorkPool = nullptr; + + std::vector<std::wstring> m_BaseUris; // eg: http://*:nnnn/ + HTTP_SERVER_SESSION_ID m_HttpSessionId = 0; + HTTP_URL_GROUP_ID m_HttpUrlGroupId = 0; + HANDLE m_RequestQueueHandle = 0; + std::atomic_int32_t m_PendingRequests{0}; + std::atomic_int32_t m_IsShuttingDown{0}; + int32_t m_MinPendingRequests = 16; + int32_t m_MaxPendingRequests = 128; + Event m_ShutdownEvent; + const HttpSysConfig m_InitialConfig; +}; + +} // namespace zen +#endif + +#if ZEN_WITH_HTTPSYS # include <conio.h> # include <mstcpip.h> @@ -809,7 +889,7 @@ HttpAsyncWorkRequest::AsyncWorkItem::Execute() \/ \/ \/ */ -HttpSysServer::HttpSysServer(const Config& Config) +HttpSysServer::HttpSysServer(const HttpSysConfig& Config) : m_Log(logging::Get("http")) , m_RequestLog(logging::Get("http_requests")) , m_IsRequestLoggingEnabled(Config.IsRequestLoggingEnabled) @@ -1868,5 +1948,11 @@ HttpSysServer::RegisterService(HttpService& Service) RegisterService(Service.BaseUri(), Service); } +Ref<HttpServer> +CreateHttpSysServer(HttpSysConfig Config) +{ + return Ref<HttpServer>(new HttpSysServer(Config)); +} + } // namespace zen #endif |