2024-11-12 08:52:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
2024-11-26 11:49:24 +00:00
|
|
|
#include "json.hpp"
|
2024-11-18 11:35:38 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdlib.h>
|
2024-11-20 12:04:55 +00:00
|
|
|
#include <string>
|
2024-11-18 11:35:38 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2024-11-12 08:52:55 +00:00
|
|
|
namespace slige_rpc {
|
|
|
|
|
2024-11-20 12:04:55 +00:00
|
|
|
struct Err {
|
|
|
|
std::string msg;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> class Res {
|
|
|
|
public:
|
|
|
|
Res(T value)
|
|
|
|
{
|
|
|
|
this->value = value;
|
|
|
|
this->holds_value = true;
|
|
|
|
}
|
|
|
|
Res(Err error)
|
|
|
|
{
|
|
|
|
this->error = error;
|
|
|
|
this->holds_value = false;
|
|
|
|
}
|
|
|
|
auto is_ok() -> bool { return this->holds_value; }
|
|
|
|
auto ok() -> T { return this->value; }
|
|
|
|
auto err() -> Err { return this->error; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool holds_value;
|
|
|
|
T value;
|
|
|
|
Err error;
|
|
|
|
};
|
|
|
|
|
2024-11-20 13:30:43 +00: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-11-20 12:04:55 +00:00
|
|
|
struct Unit { };
|
|
|
|
struct Req { };
|
|
|
|
|
|
|
|
class BufferedWriter {
|
2024-11-20 13:30:43 +00:00
|
|
|
public:
|
2024-11-20 12:04:55 +00:00
|
|
|
BufferedWriter(int fd)
|
|
|
|
: fd(fd)
|
|
|
|
{
|
|
|
|
}
|
2024-11-21 10:31:33 +00:00
|
|
|
BufferedWriter(const BufferedWriter&) = delete;
|
|
|
|
BufferedWriter operator=(const BufferedWriter&) = delete;
|
|
|
|
BufferedWriter(BufferedWriter&&) = delete;
|
|
|
|
BufferedWriter operator=(BufferedWriter&&) = delete;
|
2024-11-20 12:04:55 +00:00
|
|
|
~BufferedWriter() { close(fd); }
|
|
|
|
|
|
|
|
auto flush() -> Res<size_t>;
|
|
|
|
auto write(uint8_t byte) -> Res<Unit>;
|
|
|
|
auto write(std::string message) -> Res<Unit>;
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const size_t length = 1024;
|
|
|
|
size_t occupied = 0;
|
|
|
|
uint8_t buffer[length];
|
|
|
|
int fd;
|
|
|
|
};
|
|
|
|
|
2024-11-26 11:49:24 +00:00
|
|
|
namespace {
|
|
|
|
static inline auto create_address(uint16_t port) -> sockaddr_in
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
.sin_family = AF_INET,
|
|
|
|
.sin_port = htons(port),
|
|
|
|
.sin_addr = { .s_addr = inet_addr("127.0.0.1") },
|
|
|
|
.sin_zero = { 0 },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-20 10:07:36 +00: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 12:04:55 +00:00
|
|
|
template <typename Functor> class RpcServer {
|
2024-11-12 08:52:55 +00:00
|
|
|
public:
|
2024-11-26 11:49:24 +00:00
|
|
|
RpcServer(Functor&& functor)
|
2024-11-20 12:04:55 +00:00
|
|
|
: functor(functor) {};
|
|
|
|
|
2024-11-26 11:49:24 +00:00
|
|
|
auto listen() -> Res<Unit>
|
|
|
|
{
|
|
|
|
|
|
|
|
int socket_fd = ::socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (socket_fd < 0) {
|
|
|
|
return Err { "could not get socket" };
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
auto address = create_address(13370);
|
|
|
|
if (::bind(socket_fd, (struct sockaddr*)&address, sizeof(address))
|
|
|
|
< 0) {
|
|
|
|
return Err { "could not bind" };
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (::listen(socket_fd, 0) < 0) {
|
|
|
|
return Err { "could not listen" };
|
|
|
|
}
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
auto client_address = create_address(13370);
|
|
|
|
socklen_t address_size = sizeof(client_address);
|
|
|
|
int client = ::accept(
|
|
|
|
socket_fd, (struct sockaddr*)&client_address, &address_size);
|
|
|
|
if (client < 0) {
|
|
|
|
return Err { "could not accept" };
|
|
|
|
}
|
|
|
|
const size_t buf_len = 1024;
|
|
|
|
int8_t buffer[buf_len] = {};
|
|
|
|
auto bracket_finder = BracketFinder();
|
|
|
|
std::string message = {};
|
|
|
|
while (true) {
|
|
|
|
ssize_t bytes_read = read(client, buffer, buf_len);
|
|
|
|
if (bytes_read < 0) {
|
|
|
|
return Err { "could not read" };
|
|
|
|
} else if (bytes_read == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (size_t i; 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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
auto writer = std::make_unique<BufferedWriter>(client);
|
|
|
|
this->functor(std::move(req.val()), std::move(writer));
|
|
|
|
message.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::unreachable();
|
|
|
|
};
|
2024-11-18 11:35:38 +00:00
|
|
|
|
2024-11-20 10:07:36 +00:00
|
|
|
private:
|
2024-11-20 12:04:55 +00:00
|
|
|
Functor functor;
|
2024-11-18 11:35:38 +00:00
|
|
|
};
|
2024-11-20 12:04:55 +00:00
|
|
|
|
2024-11-18 11:35:38 +00:00
|
|
|
};
|