slige/runtime/rpc_server.hpp

107 lines
2.0 KiB
C++
Raw Normal View History

2024-11-12 08:52:55 +00:00
#pragma once
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-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-20 12:04:55 +00:00
RpcServer(Functor functor)
: functor(functor) {};
auto listen() -> Res<Unit>;
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
};