Fix rpc file getting deleted when launching gsr-ui twice. Use unix domain socket instead

This commit is contained in:
dec05eba
2025-10-03 13:01:40 +02:00
parent 9e03cd0354
commit 0b4af1e6bb
4 changed files with 204 additions and 85 deletions

View File

@@ -4,31 +4,47 @@
#include <functional>
#include <unordered_map>
#include <string>
#include <poll.h>
typedef struct _IO_FILE FILE;
#define GSR_RPC_MAX_CONNECTIONS 8
#define GSR_RPC_MAX_POLLS (1 + GSR_RPC_MAX_CONNECTIONS) /* +1 to include the socket_fd itself for accept */
#define GSR_RPC_MAX_MESSAGE_SIZE 128
namespace gsr {
using RpcCallback = std::function<void(const std::string &name)>;
enum class RpcOpenResult {
OK,
CONNECTION_REFUSED,
ERROR
};
class Rpc {
public:
Rpc() = default;
struct PollData {
char buffer[GSR_RPC_MAX_MESSAGE_SIZE];
int buffer_size = 0;
};
Rpc();
Rpc(const Rpc&) = delete;
Rpc& operator=(const Rpc&) = delete;
~Rpc();
bool create(const char *name);
bool open(const char *name);
RpcOpenResult open(const char *name);
bool write(const char *str, size_t size);
void poll();
bool add_handler(const std::string &name, RpcCallback callback);
private:
bool open_filepath(const char *filepath);
void handle_client_data(int client_fd, PollData &poll_data);
private:
int fd = 0;
FILE *file = nullptr;
std::string fifo_filepath;
int socket_fd = 0;
std::string socket_filepath;
struct pollfd polls[GSR_RPC_MAX_POLLS];
PollData polls_data[GSR_RPC_MAX_POLLS];
int num_polls = 0;
std::unordered_map<std::string, RpcCallback> handlers_by_name;
};
}