aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-09-10 19:48:26 +0200
committerStefan Boberg <[email protected]>2021-09-10 19:48:26 +0200
commite04a36f90f9e326a2e77237ba4e18399a8f60172 (patch)
tree15c2126f15ad86bf3f9d1b00a5322deb1c6a125c
parentclang-format (diff)
downloadzen-e04a36f90f9e326a2e77237ba4e18399a8f60172.tar.xz
zen-e04a36f90f9e326a2e77237ba4e18399a8f60172.zip
Added beginnings of a uWS http front-end
-rw-r--r--vcpkg.json3
-rw-r--r--xmake.lua5
-rw-r--r--zenhttp/httpserver.cpp5
-rw-r--r--zenhttp/httpuws.cpp81
-rw-r--r--zenhttp/httpuws.h25
-rw-r--r--zenhttp/xmake.lua2
-rw-r--r--zenhttp/zenhttp.vcxproj2
-rw-r--r--zenhttp/zenhttp.vcxproj.filters2
-rw-r--r--zenserver/xmake.lua3
9 files changed, 123 insertions, 5 deletions
diff --git a/vcpkg.json b/vcpkg.json
index 4a0c41d3b..45910556a 100644
--- a/vcpkg.json
+++ b/vcpkg.json
@@ -21,6 +21,7 @@
"features": [ "lz4", "zstd" ]
},
"sol2",
- "sentry-native"
+ "sentry-native",
+ "uwebsockets"
]
}
diff --git a/xmake.lua b/xmake.lua
index bbfd0385e..ed1ebb395 100644
--- a/xmake.lua
+++ b/xmake.lua
@@ -17,7 +17,10 @@ add_requires(
"vcpkg::curl",
"vcpkg::zlib",
"vcpkg::zstd",
- "vcpkg::http-parser")
+ "vcpkg::http-parser",
+ "vcpkg::uwebsockets",
+ "vcpkg::usockets",
+ "vcpkg::libuv")
add_rules("mode.debug", "mode.release")
diff --git a/zenhttp/httpserver.cpp b/zenhttp/httpserver.cpp
index f281185b7..d1893dc6e 100644
--- a/zenhttp/httpserver.cpp
+++ b/zenhttp/httpserver.cpp
@@ -4,6 +4,7 @@
#include "httpnull.h"
#include "httpsys.h"
+#include "httpuws.h"
#include <zencore/compactbinary.h>
#include <zencore/compactbinarypackage.h>
@@ -354,7 +355,9 @@ HttpRequestRouter::HandleRequest(zen::HttpServerRequest& Request)
Ref<HttpServer>
CreateHttpServer()
{
-#if ZEN_WITH_HTTPSYS
+#if 1
+ return new HttpUwsServer;
+#elif ZEN_WITH_HTTPSYS
return new HttpSysServer{32};
#else
return new HttpNullServer;
diff --git a/zenhttp/httpuws.cpp b/zenhttp/httpuws.cpp
new file mode 100644
index 000000000..a35caa120
--- /dev/null
+++ b/zenhttp/httpuws.cpp
@@ -0,0 +1,81 @@
+#include "httpuws.h"
+
+#pragma warning(push)
+#pragma warning(disable : 4267 4706)
+#include <uwebsockets/App.h>
+#pragma warning(pop)
+
+#include <conio.h>
+#include <zencore/logging.h>
+
+#if ZEN_PLATFORM_WINDOWS
+# pragma comment(lib, "Iphlpapi.lib")
+#endif
+
+namespace zen {
+
+HttpUwsServer::HttpUwsServer()
+{
+}
+
+HttpUwsServer::~HttpUwsServer()
+{
+}
+
+void
+HttpUwsServer::RegisterService(HttpService& Service)
+{
+ ZEN_UNUSED(Service);
+}
+
+void
+HttpUwsServer::Initialize(int BasePort)
+{
+ m_BasePort = BasePort;
+}
+
+void
+HttpUwsServer::Run(bool TestMode)
+{
+ if (TestMode == false)
+ {
+ zen::logging::ConsoleLog().info("Zen Server running (null HTTP). Press ESC or Q to quit");
+ }
+
+ ::uWS::App().get("/*", [](uWS::HttpResponse<false>* res, uWS::HttpRequest* req) {
+ res->end("Hello world!");
+ ZEN_UNUSED(req);
+ }).listen(m_BasePort, [](auto* listen_socket) {
+ ZEN_UNUSED(listen_socket);
+ }).run();
+
+ do
+ {
+ int WaitTimeout = -1;
+
+ if (!TestMode)
+ {
+ WaitTimeout = 1000;
+ }
+
+ if (!TestMode && _kbhit() != 0)
+ {
+ char c = (char)_getch();
+
+ if (c == 27 || c == 'Q' || c == 'q')
+ {
+ RequestApplicationExit(0);
+ }
+ }
+
+ m_ShutdownEvent.Wait(WaitTimeout);
+ } while (!IsApplicationExitRequested());
+}
+
+void
+HttpUwsServer::RequestExit()
+{
+ m_ShutdownEvent.Set();
+}
+
+} // namespace zen
diff --git a/zenhttp/httpuws.h b/zenhttp/httpuws.h
new file mode 100644
index 000000000..2852764f7
--- /dev/null
+++ b/zenhttp/httpuws.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <zenhttp/httpserver.h>
+
+#include <zencore/thread.h>
+
+namespace zen {
+
+class HttpUwsServer : public HttpServer
+{
+ public:
+ HttpUwsServer();
+ ~HttpUwsServer();
+
+ virtual void RegisterService(HttpService& Service) override;
+ virtual void Initialize(int BasePort) override;
+ virtual void Run(bool TestMode) override;
+ virtual void RequestExit() override;
+
+private:
+ Event m_ShutdownEvent;
+ int m_BasePort = 0;
+};
+
+} \ No newline at end of file
diff --git a/zenhttp/xmake.lua b/zenhttp/xmake.lua
index fff4fb526..65d5f08ea 100644
--- a/zenhttp/xmake.lua
+++ b/zenhttp/xmake.lua
@@ -3,5 +3,5 @@ target('zenhttp')
add_files("**.cpp")
add_includedirs("include", {public=true})
add_deps("zencore")
- add_packages("vcpkg::gsl-lite")
+ add_packages("vcpkg::gsl-lite", "vcpkg::uwebsockets", "vcpkg::usockets", "vcpkg::libuv")
add_options("httpsys") \ No newline at end of file
diff --git a/zenhttp/zenhttp.vcxproj b/zenhttp/zenhttp.vcxproj
index d4a61b313..db9cd898a 100644
--- a/zenhttp/zenhttp.vcxproj
+++ b/zenhttp/zenhttp.vcxproj
@@ -99,11 +99,13 @@
<ClCompile Include="httpnull.cpp" />
<ClCompile Include="httpserver.cpp" />
<ClCompile Include="httpsys.cpp" />
+ <ClCompile Include="httpuws.cpp" />
<ClCompile Include="iothreadpool.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="httpnull.h" />
<ClInclude Include="httpsys.h" />
+ <ClInclude Include="httpuws.h" />
<ClInclude Include="include\zenhttp\httpclient.h" />
<ClInclude Include="include\zenhttp\httpserver.h" />
<ClInclude Include="include\zenhttp\zenhttp.h" />
diff --git a/zenhttp/zenhttp.vcxproj.filters b/zenhttp/zenhttp.vcxproj.filters
index 032c97386..9c2d05cd5 100644
--- a/zenhttp/zenhttp.vcxproj.filters
+++ b/zenhttp/zenhttp.vcxproj.filters
@@ -6,6 +6,7 @@
<ClCompile Include="httpsys.cpp" />
<ClCompile Include="iothreadpool.cpp" />
<ClCompile Include="httpnull.cpp" />
+ <ClCompile Include="httpuws.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\zenhttp\httpclient.h" />
@@ -14,6 +15,7 @@
<ClInclude Include="iothreadpool.h" />
<ClInclude Include="include\zenhttp\zenhttp.h" />
<ClInclude Include="httpnull.h" />
+ <ClInclude Include="httpuws.h" />
</ItemGroup>
<ItemGroup>
<None Include="xmake.lua" />
diff --git a/zenserver/xmake.lua b/zenserver/xmake.lua
index 293743c94..bb70846fa 100644
--- a/zenserver/xmake.lua
+++ b/zenserver/xmake.lua
@@ -23,7 +23,8 @@ target("zenserver")
"vcpkg::sol2",
"vcpkg::lua",
"vcpkg::asio",
- "vcpkg::json11"
+ "vcpkg::json11",
+ "vcpkg::uwebsockets", "vcpkg::usockets", "vcpkg::libuv"
)
add_packages(