175 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
148 lines
5.2 KiB
C++
148 lines
5.2 KiB
C++
// Owned standalone Windows worker. It never loads into or attaches to the game.
|
|
#ifndef NOMINMAX
|
|
#define NOMINMAX
|
|
#endif
|
|
#include "windows_backend_ipc.hpp"
|
|
#include <array>
|
|
#include <charconv>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <iostream>
|
|
#include <set>
|
|
#include <windows.h>
|
|
|
|
namespace {
|
|
using Dispatch = int32_t(WINAPI *)(void *);
|
|
using namespace gateway::backend_ipc;
|
|
struct Module {
|
|
HMODULE value = nullptr;
|
|
~Module() {
|
|
if (value)
|
|
FreeLibrary(value);
|
|
}
|
|
};
|
|
struct RegistryLifetime {
|
|
Dispatch dispatch;
|
|
std::set<uint64_t> logins, transports;
|
|
~RegistryLifetime() {
|
|
for (auto token : transports)
|
|
close(GW_WIRE_TRANSPORT_CLOSE, token);
|
|
for (auto token : logins)
|
|
close(GW_WIRE_LOGIN_CLOSE, token);
|
|
}
|
|
void close(uint32_t operation, uint64_t token) noexcept {
|
|
gw_wire_call call{};
|
|
call.abi = GW_WINE_WIRE_ABI;
|
|
call.size = sizeof(call);
|
|
call.operation = operation;
|
|
if (operation == GW_WIRE_TRANSPORT_CLOSE)
|
|
call.transport = token;
|
|
else
|
|
call.login = token;
|
|
dispatch(&call);
|
|
}
|
|
void observe(const gw_wire_call &call, uint64_t old_login,
|
|
uint64_t old_transport) {
|
|
if (call.result)
|
|
return;
|
|
if (auth(call.operation) && call.login)
|
|
logins.insert(call.login);
|
|
if (path(call.operation) && call.transport)
|
|
transports.insert(call.transport);
|
|
if (call.operation == GW_WIRE_TRANSPORT_CLOSE)
|
|
transports.erase(old_transport);
|
|
if (call.operation == GW_WIRE_LOGIN_CLOSE)
|
|
logins.erase(old_login);
|
|
}
|
|
};
|
|
bool read(HANDLE pipe, uint8_t *out, size_t bytes, bool clean_eof = false) {
|
|
size_t done = 0;
|
|
while (done < bytes) {
|
|
DWORD received = 0;
|
|
if (!ReadFile(pipe, out + done, DWORD(bytes - done), &received, nullptr)) {
|
|
if (GetLastError() == ERROR_BROKEN_PIPE && !done && clean_eof)
|
|
return false;
|
|
throw gateway::Error("backend host input failed");
|
|
}
|
|
if (!received) {
|
|
if (!done && clean_eof)
|
|
return false;
|
|
throw gateway::Error("backend host truncated input");
|
|
}
|
|
done += received;
|
|
}
|
|
return true;
|
|
}
|
|
void write(HANDLE pipe, std::span<const uint8_t> input) {
|
|
size_t done = 0;
|
|
while (done < input.size()) {
|
|
DWORD sent = 0;
|
|
if (!WriteFile(pipe, input.data() + done, DWORD(input.size() - done), &sent,
|
|
nullptr) ||
|
|
!sent)
|
|
throw gateway::Error("backend host output failed");
|
|
done += sent;
|
|
}
|
|
}
|
|
} // namespace
|
|
int main(int argc, char **argv) {
|
|
try {
|
|
if (argc != 3)
|
|
throw gateway::Error("backend host arguments");
|
|
uint64_t generation = 0;
|
|
const auto end = argv[2] + std::strlen(argv[2]);
|
|
const auto parsed = std::from_chars(argv[2], end, generation);
|
|
if (parsed.ec != std::errc{} || parsed.ptr != end || !generation)
|
|
throw gateway::Error("backend host generation");
|
|
const auto input = GetStdHandle(STD_INPUT_HANDLE),
|
|
output = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
if (GetFileType(input) != FILE_TYPE_PIPE ||
|
|
GetFileType(output) != FILE_TYPE_PIPE)
|
|
throw gateway::Error("backend host requires owned anonymous pipes");
|
|
const auto backend = std::filesystem::canonical(std::filesystem::path(
|
|
std::u8string(argv[1], argv[1] + std::strlen(argv[1]))));
|
|
Module module{LoadLibraryExW(backend.c_str(), nullptr,
|
|
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
|
|
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)};
|
|
if (!module.value)
|
|
throw gateway::Error("backend host DLL load failed");
|
|
auto symbol = GetProcAddress(module.value, "gw_native_wire_dispatch");
|
|
Dispatch dispatch = nullptr;
|
|
static_assert(sizeof(dispatch) == sizeof(symbol));
|
|
std::memcpy(&dispatch, &symbol, sizeof(dispatch));
|
|
if (!dispatch)
|
|
throw gateway::Error("backend host dispatch absent");
|
|
RegistryLifetime lifetime{dispatch, {}, {}};
|
|
gw_wire_call ping{};
|
|
ping.abi = GW_WINE_WIRE_ABI;
|
|
ping.size = sizeof(ping);
|
|
ping.operation = GW_WIRE_PING;
|
|
if (dispatch(&ping) || ping.result || ping.value != GW_NATIVE_ABI)
|
|
throw gateway::Error("backend host ABI mismatch");
|
|
for (uint64_t sequence = 1; sequence; ++sequence) {
|
|
std::array<uint8_t, HeaderBytes> header{};
|
|
if (!read(input, header.data(), header.size(), true))
|
|
return 0;
|
|
const auto bytes = frame_size(header);
|
|
gateway::Bytes request(HeaderBytes + bytes);
|
|
WipeBytes request_clean{request};
|
|
std::copy(header.begin(), header.end(), request.begin());
|
|
read(input, request.data() + HeaderBytes, bytes);
|
|
auto owned = decode_request(request, {generation, sequence});
|
|
auto &call = owned->call;
|
|
const auto login = call.login, transport = call.transport;
|
|
const auto status = dispatch(&call);
|
|
lifetime.observe(call, login, transport);
|
|
auto response = encode_response(call, status, {generation, sequence});
|
|
WipeBytes response_clean{response};
|
|
write(output, response);
|
|
}
|
|
throw gateway::Error("backend host sequence exhausted");
|
|
} catch (const std::exception &) {
|
|
// Never print request data, credentials, endpoints, keys or paths.
|
|
std::cerr
|
|
<< "Standalone backend host stopped at its owned protocol boundary\n";
|
|
return 1;
|
|
}
|
|
}
|