Files
brandling-safari/client/LocalServer.h

42 lines
1.3 KiB
C++

#pragma once
// Runs safari_server next to the client executable for "Host & Play".
// The child's stdin is a pipe: stop() sends the console command `quit` so the
// server saves and exits cleanly on every platform (Windows has no SIGTERM),
// and only kills it if it does not exit in time. Its output goes to a log file.
#include <cstdint>
#include <filesystem>
#include <string>
namespace bs {
// Per-user data folder: %APPDATA%\brandling-safari on Windows,
// $XDG_DATA_HOME/brandling-safari or ~/.local/share/brandling-safari elsewhere
// (the server uses the same folder for world.db). Created if missing.
std::filesystem::path userDataDir();
std::filesystem::path executableDir();
class LocalServer {
public:
LocalServer() = default;
LocalServer(const LocalServer&) = delete;
LocalServer& operator=(const LocalServer&) = delete;
~LocalServer() { stop(); }
// Starts `safari_server --port <port> --name <name>`; false + `error` on failure.
bool start(uint16_t port, const std::string& name, std::string& error);
void stop();
bool running() const;
private:
#if defined(_WIN32)
void* process_ = nullptr; // HANDLE
void* stdin_ = nullptr; // HANDLE (write end)
#else
int pid_ = -1;
int stdin_ = -1;
#endif
};
} // namespace bs