aboutsummaryrefslogtreecommitdiff
path: root/src/zenhttp/dlltransport.cpp
blob: 04fb6caaa443010a4533d7b4c9c76b5a44462f11 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright Epic Games, Inc. All Rights Reserved.

#include "dlltransport.h"

#include <zencore/except.h>
#include <zencore/logging.h>
#include <zencore/scopeguard.h>

#include <exception>
#include <thread>
#include <vector>

#if ZEN_WITH_PLUGINS

namespace zen {

struct DllTransportConnection : public TransportConnection
{
public:
	DllTransportConnection();
	~DllTransportConnection();

	void Initialize(TransportServerConnection& ServerConnection);
	void HandleConnection();

	// TransportConnection

	virtual int64_t WriteBytes(const void* Buffer, size_t DataSize) override;
	virtual void	Shutdown(bool Receive, bool Transmit) override;
	virtual void	CloseConnection() override;

private:
	Ref<TransportServerConnection> m_ConnectionHandler;
	bool						   m_IsTerminated = false;
};

DllTransportConnection::DllTransportConnection()
{
}

DllTransportConnection::~DllTransportConnection()
{
}

void
DllTransportConnection::Initialize(TransportServerConnection& ServerConnection)
{
	m_ConnectionHandler = &ServerConnection;  // TODO: this is awkward
}

void
DllTransportConnection::HandleConnection()
{
}

void
DllTransportConnection::CloseConnection()
{
	if (m_IsTerminated)
	{
		return;
	}

	m_IsTerminated = true;
}

int64_t
DllTransportConnection::WriteBytes(const void* Buffer, size_t DataSize)
{
	ZEN_UNUSED(Buffer, DataSize);
	return DataSize;
}

void
DllTransportConnection::Shutdown(bool Receive, bool Transmit)
{
	ZEN_UNUSED(Receive, Transmit);
}

//////////////////////////////////////////////////////////////////////////

struct LoadedDll
{
	std::string			  Name;
	std::filesystem::path LoadedFromPath;
	Ref<TransportPlugin>  Plugin;
};

class DllTransportPluginImpl
{
public:
	DllTransportPluginImpl(uint16_t BasePort, unsigned int ThreadCount);
	~DllTransportPluginImpl();

	uint16_t Start(TransportServer* ServerInterface);
	void	 Stop();
	bool	 IsAvailable();
	void	 LoadDll(std::string_view Name);

private:
	TransportServer*	   m_ServerInterface = nullptr;
	RwLock				   m_Lock;
	std::vector<LoadedDll> m_Transports;
	uint16_t			   m_BasePort	 = 0;
	int					   m_ThreadCount = 0;
};

DllTransportPluginImpl::DllTransportPluginImpl(uint16_t BasePort, unsigned int ThreadCount)
: m_BasePort(BasePort)
, m_ThreadCount(ThreadCount != 0 ? ThreadCount : Max(std::thread::hardware_concurrency(), 8u))
{
}

DllTransportPluginImpl::~DllTransportPluginImpl()
{
}

uint16_t
DllTransportPluginImpl::Start(TransportServer* ServerIface)
{
	m_ServerInterface = ServerIface;

	RwLock::ExclusiveLockScope _(m_Lock);

	for (LoadedDll& Transport : m_Transports)
	{
		try
		{
			Transport.Plugin->Initialize(ServerIface);
		}
		catch (const std::exception&)
		{
			// TODO: report
		}
	}

	return m_BasePort;
}

void
DllTransportPluginImpl::Stop()
{
	RwLock::ExclusiveLockScope _(m_Lock);

	for (LoadedDll& Transport : m_Transports)
	{
		try
		{
			Transport.Plugin->Shutdown();
		}
		catch (const std::exception&)
		{
			// TODO: report
		}
	}
}

bool
DllTransportPluginImpl::IsAvailable()
{
	return true;
}

void
DllTransportPluginImpl::LoadDll(std::string_view Name)
{
	ExtendableStringBuilder<128> DllPath;
	DllPath << Name << ".dll";
	HMODULE DllHandle = LoadLibraryA(DllPath.c_str());

	if (!DllHandle)
	{
		std::error_code Ec = MakeErrorCodeFromLastError();

		throw std::system_error(Ec, fmt::format("failed to load transport DLL from '{}'", DllPath));
	}

	TransportPlugin* CreateTransportPlugin();

	PfnCreateTransportPlugin CreatePlugin = (PfnCreateTransportPlugin)GetProcAddress(DllHandle, "CreateTransportPlugin");

	if (!CreatePlugin)
	{
		std::error_code Ec = MakeErrorCodeFromLastError();

		FreeLibrary(DllHandle);

		throw std::system_error(Ec, fmt::format("API mismatch detected in transport DLL loaded from '{}'", DllPath));
	}

	LoadedDll NewDll;

	NewDll.Name			  = Name;
	NewDll.LoadedFromPath = DllPath.c_str();
	NewDll.Plugin		  = CreatePlugin();

	m_Transports.emplace_back(std::move(NewDll));
}

//////////////////////////////////////////////////////////////////////////

DllTransportPlugin::DllTransportPlugin(uint16_t BasePort, unsigned int ThreadCount)
: m_Impl(std::make_unique<DllTransportPluginImpl>(BasePort, ThreadCount))
{
}

DllTransportPlugin::~DllTransportPlugin()
{
	m_Impl->Stop();
}

uint32_t
DllTransportPlugin::AddRef() const
{
	return RefCounted::AddRef();
}

uint32_t
DllTransportPlugin::Release() const
{
	return RefCounted::Release();
}

void
DllTransportPlugin::Initialize(TransportServer* ServerInterface)
{
	m_Impl->Start(ServerInterface);
}

void
DllTransportPlugin::Shutdown()
{
	m_Impl->Stop();
}

bool
DllTransportPlugin::IsAvailable()
{
	return m_Impl->IsAvailable();
}

void
DllTransportPlugin::LoadDll(std::string_view Name)
{
	return m_Impl->LoadDll(Name);
}

}  // namespace zen

#endif