aboutsummaryrefslogtreecommitdiff
path: root/src/zenhttp/servers/httpmulti.cpp
blob: 310ac9dc04b8361cc463b52a429a1529a98f7c5f (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright Epic Games, Inc. All Rights Reserved.

#include "httpmulti.h"

#include <zencore/logging.h>
#include <zencore/trace.h>

#if ZEN_PLATFORM_WINDOWS
#	include <conio.h>
#endif

namespace zen {

HttpMultiServer::HttpMultiServer()
{
}

HttpMultiServer::~HttpMultiServer()
{
}

void
HttpMultiServer::OnRegisterService(HttpService& Service)
{
	for (auto& Server : m_Servers)
	{
		Server->RegisterService(Service);
	}
}

int
HttpMultiServer::OnInitialize(int BasePort, std::filesystem::path DataDir)
{
	ZEN_TRACE_CPU("HttpMultiServer::Initialize");

	ZEN_UNUSED(DataDir);
	ZEN_ASSERT(!m_IsInitialized);

	int EffectivePort = 0;

	for (auto& Server : m_Servers)
	{
		const int InitializeResult = Server->Initialize(BasePort, DataDir);

		if (!EffectivePort)
		{
			EffectivePort = InitializeResult;
		}
	}

	m_IsInitialized = true;

	return EffectivePort;
}

void
HttpMultiServer::OnSetHttpRequestFilter(IHttpRequestFilter* RequestFilter)
{
	for (auto& Server : m_Servers)
	{
		Server->SetHttpRequestFilter(RequestFilter);
	}
}

void
HttpMultiServer::OnRun(bool IsInteractiveSession)
{
	const int WaitTimeout		= 1000;
	bool	  ShutdownRequested = false;

#if ZEN_PLATFORM_WINDOWS
	if (IsInteractiveSession)
	{
		ZEN_CONSOLE("Zen Server running (multi server). Press ESC or Q to quit");
	}

	do
	{
		if (IsInteractiveSession && _kbhit() != 0)
		{
			char c = (char)_getch();

			if (c == 27 || c == 'Q' || c == 'q')
			{
				m_ShutdownEvent.Set();
				RequestApplicationExit(0);
			}
		}

		ShutdownRequested = m_ShutdownEvent.Wait(WaitTimeout);
	} while (!ShutdownRequested);
#else
	if (IsInteractiveSession)
	{
		ZEN_CONSOLE("Zen Server running (null HTTP). Ctrl-C to quit");
	}

	do
	{
		ShutdownRequested = m_ShutdownEvent.Wait(WaitTimeout);
	} while (!ShutdownRequested);
#endif
}

void
HttpMultiServer::OnRequestExit()
{
	m_ShutdownEvent.Set();
}

void
HttpMultiServer::OnClose()
{
	for (auto& Server : m_Servers)
	{
		Server->Close();
	}
}

void
HttpMultiServer::AddServer(Ref<HttpServer> Server)
{
	ZEN_ASSERT(!m_IsInitialized);

	m_Servers.push_back(std::move(Server));
}

}  // namespace zen