aboutsummaryrefslogtreecommitdiff
path: root/zenhttp/httpuws.cpp
blob: 992809b17edd82d44b101150a659961a36647176 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright Epic Games, Inc. All Rights Reserved.

#include "httpuws.h"

#pragma warning(push)
#pragma warning(disable : 4244 4324 4267 4458 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);
			 })
		.post("/*",
			  [](uWS::HttpResponse<false>* res, uWS::HttpRequest* req) {
				  res->onData([&](std::string_view Data, bool fin) {
					  ZEN_UNUSED(Data);
					  if (fin)
						  res->end("Hello world!");
				  });

				  res->onAborted([&] {});
				  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