From 6ec26c46d694a1d5291790a9c70bec25dce4b513 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Thu, 9 Sep 2021 15:14:08 +0200 Subject: Factored out http server related code into zenhttp module since it feels out of place in zencore --- zenhttp/httpsys.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 zenhttp/httpsys.h (limited to 'zenhttp/httpsys.h') diff --git a/zenhttp/httpsys.h b/zenhttp/httpsys.h new file mode 100644 index 000000000..ed4a6a182 --- /dev/null +++ b/zenhttp/httpsys.h @@ -0,0 +1,61 @@ +#pragma once + +#include + +#define _WINSOCKAPI_ +#include +#include "iothreadpool.h" + +#include +#include + +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(int ThreadCount); + ~HttpSysServer(); + + // HttpServer interface implementation + + virtual void Initialize(int BasePort) override; + virtual void Run(bool TestMode) override; + virtual void RequestExit() override; + virtual void RegisterService(HttpService& Service) override; + +private: + void Initialize(const wchar_t* UrlPath); + + void StartServer(); + void OnHandlingRequest(); + void IssueNewRequestMaybe(); + + inline bool IsOk() const { return m_IsOk; } + + void RegisterService(const char* Endpoint, HttpService& Service); + void RemoveEndpoint(const char* Endpoint, HttpService& Service); + +private: + bool m_IsOk = false; + bool m_IsHttpInitialized = false; + WinIoThreadPool m_ThreadPool; + + std::wstring m_BaseUri; // 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}; + int32_t m_MinPendingRequests = 16; + int32_t m_MaxPendingRequests = 128; + Event m_ShutdownEvent; +}; + +} // namespace zen -- cgit v1.2.3 From a73862b954209fd1adce0f07b8f80b8cf27f2486 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Thu, 9 Sep 2021 16:29:24 +0200 Subject: Added compile time logic to toggle http.sys / null http implementation on/off --- zenhttp/httpsys.h | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'zenhttp/httpsys.h') diff --git a/zenhttp/httpsys.h b/zenhttp/httpsys.h index ed4a6a182..ec8964f13 100644 --- a/zenhttp/httpsys.h +++ b/zenhttp/httpsys.h @@ -2,12 +2,21 @@ #include -#define _WINSOCKAPI_ -#include -#include "iothreadpool.h" +#ifndef ZEN_WITH_HTTPSYS +# if ZEN_PLATFORM_WINDOWS +# define ZEN_WITH_HTTPSYS 1 +#else +# define ZEN_WITH_HTTPSYS 0 +# endif +#endif -#include -#include +#if ZEN_WITH_HTTPSYS +# define _WINSOCKAPI_ +# include +# include "iothreadpool.h" + +# include +# include namespace zen { @@ -59,3 +68,4 @@ private: }; } // namespace zen +#endif -- cgit v1.2.3 From 46aa14744ba72873975d288b568fa3b15d196a78 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Thu, 9 Sep 2021 20:46:22 +0200 Subject: clang-format --- zenhttp/httpsys.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'zenhttp/httpsys.h') diff --git a/zenhttp/httpsys.h b/zenhttp/httpsys.h index ec8964f13..7c4e303ce 100644 --- a/zenhttp/httpsys.h +++ b/zenhttp/httpsys.h @@ -1,3 +1,5 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + #pragma once #include @@ -5,7 +7,7 @@ #ifndef ZEN_WITH_HTTPSYS # if ZEN_PLATFORM_WINDOWS # define ZEN_WITH_HTTPSYS 1 -#else +# else # define ZEN_WITH_HTTPSYS 0 # endif #endif -- cgit v1.2.3 From 49d09922716b216896fe60a92b1a126c9ba8c302 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Fri, 10 Sep 2021 18:55:30 +0200 Subject: Refactored HTTP request handling to scale better The new logic simply reads the whole payload up front before dispatching to the endpoint handler. This increases concurrency as fewer threads will be blocked waiting for payloads Similar logic will be added for compact binary package negotiation and ultimately we want to support streaming payloads to a staging directory on disk rather than keeping them all in memory --- zenhttp/httpsys.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zenhttp/httpsys.h') diff --git a/zenhttp/httpsys.h b/zenhttp/httpsys.h index 7c4e303ce..02c96d9b5 100644 --- a/zenhttp/httpsys.h +++ b/zenhttp/httpsys.h @@ -52,7 +52,7 @@ private: inline bool IsOk() const { return m_IsOk; } void RegisterService(const char* Endpoint, HttpService& Service); - void RemoveEndpoint(const char* Endpoint, HttpService& Service); + void UnregisterService(const char* Endpoint, HttpService& Service); private: bool m_IsOk = false; -- cgit v1.2.3 From f181ee026f90d37abe536779a7c0fe9f24abe925 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Fri, 10 Sep 2021 22:05:32 +0200 Subject: Improved error reporting, tweaked request buffer size and added explicit cleanup of http API resources --- zenhttp/httpsys.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'zenhttp/httpsys.h') diff --git a/zenhttp/httpsys.h b/zenhttp/httpsys.h index 02c96d9b5..9149b54d6 100644 --- a/zenhttp/httpsys.h +++ b/zenhttp/httpsys.h @@ -44,6 +44,7 @@ public: private: void Initialize(const wchar_t* UrlPath); + void Cleanup(); void StartServer(); void OnHandlingRequest(); @@ -64,6 +65,7 @@ private: HTTP_URL_GROUP_ID m_HttpUrlGroupId = 0; HANDLE m_RequestQueueHandle = 0; std::atomic_int32_t m_PendingRequests{0}; + std::atomic m_IsShuttingDown{0}; int32_t m_MinPendingRequests = 16; int32_t m_MaxPendingRequests = 128; Event m_ShutdownEvent; -- cgit v1.2.3 From 62ac72cad393f1685a3ea64ddf9d4399dc430452 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Sat, 11 Sep 2021 17:29:47 +0200 Subject: Comment fixes, changed thread count args to unsigned --- zenhttp/httpsys.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'zenhttp/httpsys.h') diff --git a/zenhttp/httpsys.h b/zenhttp/httpsys.h index 9149b54d6..6616817ec 100644 --- a/zenhttp/httpsys.h +++ b/zenhttp/httpsys.h @@ -32,7 +32,7 @@ class HttpSysServer : public HttpServer friend class HttpSysTransaction; public: - explicit HttpSysServer(int ThreadCount); + explicit HttpSysServer(unsigned int ThreadCount); ~HttpSysServer(); // HttpServer interface implementation -- cgit v1.2.3 From 835a7d00a9da37b07cdf450899ac7fd0125b3320 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Sun, 12 Sep 2021 13:42:23 +0200 Subject: Some error handling improvements in zenhttp Primarily replaces some exception usage with std::error_code --- zenhttp/httpsys.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'zenhttp/httpsys.h') diff --git a/zenhttp/httpsys.h b/zenhttp/httpsys.h index 6616817ec..62a3699cb 100644 --- a/zenhttp/httpsys.h +++ b/zenhttp/httpsys.h @@ -71,5 +71,12 @@ private: Event m_ShutdownEvent; }; +class HttpSysException : public HttpServerException +{ +public: + HttpSysException(const char* Message, uint32_t Error); + ~HttpSysException(); +}; + } // namespace zen #endif -- cgit v1.2.3 From fa1f144a4eb7d201ef84b8d369d40f26fc73dff6 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Sun, 12 Sep 2021 13:46:57 +0200 Subject: Eliminated HttpServerException and related classes --- zenhttp/httpsys.h | 7 ------- 1 file changed, 7 deletions(-) (limited to 'zenhttp/httpsys.h') diff --git a/zenhttp/httpsys.h b/zenhttp/httpsys.h index 62a3699cb..6616817ec 100644 --- a/zenhttp/httpsys.h +++ b/zenhttp/httpsys.h @@ -71,12 +71,5 @@ private: Event m_ShutdownEvent; }; -class HttpSysException : public HttpServerException -{ -public: - HttpSysException(const char* Message, uint32_t Error); - ~HttpSysException(); -}; - } // namespace zen #endif -- cgit v1.2.3