// SPDX-License-Identifier: MIT // SPDX-FileCopyrightText: 2025 Niels Martignène #pragma once #include "lib/native/base/base.hh" #include "lib/native/wrap/json.hh" namespace K { static const int MaxTowerSources = 10; class TowerServer { #if defined(_WIN32) char name[512] = {}; LocalArray pipes; #else int fd = -1; #endif LocalArray sources; std::function handle_func; public: TowerServer() {} ~TowerServer() { Stop(); } bool Bind(const char *path); void Start(std::function func); void Stop(); Span GetWaitSources() const { return sources; } bool Process(uint64_t ready = UINT64_MAX); void Send(FunctionRef func); void Send(Span buf) { Send([&](StreamWriter *writer) { writer->Write(buf); }); } void Send(Span buf) { Send([&](StreamWriter *writer) { writer->Write(buf); }); } private: #if defined(_WIN32) void RunClients(FunctionRef func); #else void RunClients(FunctionRef func); #endif }; class TowerClient { #if defined(_WIN32) struct OverlappedPipe *pipe = nullptr; #else int sock = -1; #endif std::function handle_func; WaitSource src = {}; public: ~TowerClient() { Stop(); } bool Connect(const char *path); void Start(std::function func); void Stop(); WaitSource GetWaitSource() const { return src; } bool Process(); bool Send(FunctionRef func); bool Send(Span buf) { return Send([&](StreamWriter *writer) { writer->Write(buf); }); } bool Send(Span buf) { return Send([&](StreamWriter *writer) { writer->Write(buf); }); } }; enum class ControlScope { System, User }; const char *GetControlSocketPath(ControlScope scope, const char *name, Allocator *alloc); }