slige/runtime/rpc_server.hpp

211 lines
4.8 KiB
C++
Raw Permalink Normal View History

2024-11-12 09:52:55 +01:00
#pragma once
#include "json.hpp"
2024-12-13 16:11:16 +01:00
#include <format>
#include <iostream>
2024-11-18 12:35:38 +01:00
2024-11-26 13:04:40 +01:00
namespace sliger::rpc {
2024-11-12 09:52:55 +01:00
2024-11-20 13:04:55 +01:00
struct Err {
std::string msg;
};
template <typename T> class Res {
public:
Res(T value)
2024-12-13 16:11:16 +01:00
: holds_value(true)
, value(std::move(value))
2024-11-20 13:04:55 +01:00
{
}
Res(Err error)
2024-12-13 16:11:16 +01:00
: holds_value(false)
, error(error)
2024-11-20 13:04:55 +01:00
{
}
auto is_ok() -> bool { return this->holds_value; }
2024-12-13 16:11:16 +01:00
auto ok() & -> T& { return this->value; }
auto ok() && -> T&& { return std::move(this->value); }
2024-11-20 13:04:55 +01:00
auto err() -> Err { return this->error; }
private:
bool holds_value;
T value;
Err error;
};
2024-12-13 16:11:16 +01:00
template <> class Res<void> {
public:
Res()
: holds_value(true)
{
}
Res(Err error)
: holds_value(false)
, error(error)
{
}
auto is_ok() -> bool { return this->holds_value; }
auto err() -> Err { return this->error; }
private:
bool holds_value;
Err error;
};
2024-11-20 14:30:43 +01:00
class BracketFinder {
public:
BracketFinder()
: layers(0)
{
}
auto feed(int8_t c)
{
if (c == '{') {
this->layers += 1;
} else if (c == '}') {
this->layers -= 1;
}
}
auto bracket_closed() -> bool { return this->layers == 0; }
private:
int layers;
};
2024-12-13 16:11:16 +01:00
#define DONT_COPY_OR_MOVE(T) \
T(const T&) = delete; \
T operator=(const T&) = delete; \
T(T&&) = delete; \
T operator=(T&&) = delete;
class Client;
class Socket {
public:
DONT_COPY_OR_MOVE(Socket);
Socket() = default;
~Socket()
{
if (this->initialized) {
close(this->socket_fd);
}
}
auto init() -> Res<void>;
auto accept() -> Res<std::unique_ptr<Client>>;
private:
int socket_fd = 0;
bool initialized = false;
};
class Client {
public:
DONT_COPY_OR_MOVE(Client);
Client(int client_sock)
: client_sock(client_sock)
{
}
~Client() { close(client_sock); }
auto read(int8_t* buffer, size_t buffer_size) -> ssize_t;
auto write(uint8_t* buffer, size_t buffer_size) -> ssize_t;
void flush();
private:
int client_sock;
};
2024-11-20 13:04:55 +01:00
class BufferedWriter {
2024-11-20 14:30:43 +01:00
public:
2024-12-13 16:11:16 +01:00
BufferedWriter(Client& client)
: client(&client)
2024-11-20 13:04:55 +01:00
{
}
auto flush() -> Res<size_t>;
2024-12-13 16:11:16 +01:00
auto write(uint8_t byte) -> Res<void>;
auto write(std::string message) -> Res<void>;
2024-11-20 13:04:55 +01:00
private:
static const size_t length = 1024;
size_t occupied = 0;
uint8_t buffer[length];
2024-12-13 16:11:16 +01:00
Client* client;
2024-11-20 13:04:55 +01:00
};
2024-11-20 11:07:36 +01:00
/// - load code
/// - program input
/// - run
/// - run debug
/// - fwamegwaph option
/// - code covewage option
/// - fetch fwamegwaph
/// - json string
/// - fetch code covewage
/// - json string
/// - fetch stack
/// - json string
2024-11-20 13:04:55 +01:00
template <typename Functor> class RpcServer {
2024-11-12 09:52:55 +01:00
public:
RpcServer(Functor&& functor)
2024-12-13 16:11:16 +01:00
: functor(functor) { };
2024-11-20 13:04:55 +01:00
2024-12-13 16:11:16 +01:00
auto listen() -> Res<void>
{
2024-12-13 16:11:16 +01:00
auto socket = Socket();
if (auto res = socket.init(); not res.is_ok()) {
return res.err();
}
while (true) {
2024-12-13 16:11:16 +01:00
auto client_res = socket.accept();
if (!client_res.is_ok()) {
return client_res.err();
}
2024-12-13 16:11:16 +01:00
auto client = std::move(client_res.ok());
const size_t buf_len = 1024;
int8_t buffer[buf_len] = {};
auto bracket_finder = BracketFinder();
std::string message = {};
while (true) {
2024-12-13 16:11:16 +01:00
ssize_t bytes_read = client->read(buffer, buf_len);
2024-12-12 11:35:26 +01:00
if (bytes_read <= 0) {
break;
}
2024-12-13 16:11:16 +01:00
2024-11-26 13:04:40 +01:00
for (size_t i = 0; i < (size_t)bytes_read; ++i) {
message += buffer[i];
bracket_finder.feed(buffer[i]);
if (!bracket_finder.bracket_closed()) {
continue;
}
auto req = sliger::json::parse_json(message);
if (!req.ok()) {
auto err = req.err();
auto msg = std::format(
"error parsing rpc message: '{}' @ {}:{}\n",
err.msg, err.pos.line, err.pos.col);
return Err {
.msg = msg,
};
}
2024-12-13 16:11:16 +01:00
auto writer = std::make_unique<BufferedWriter>(*client);
this->functor(std::move(req.val()), std::move(writer));
message.clear();
2024-12-13 16:11:16 +01:00
goto message_done;
}
}
2024-12-13 16:11:16 +01:00
message_done:
}
std::unreachable();
};
2024-11-18 12:35:38 +01:00
2024-11-20 11:07:36 +01:00
private:
2024-11-20 13:04:55 +01:00
Functor functor;
2024-11-18 12:35:38 +01:00
};
2024-11-20 13:04:55 +01:00
2024-11-18 12:35:38 +01:00
};