begin work on parsing rpc server messages

This commit is contained in:
Theis Pieter Hollebeek 2024-11-26 12:49:24 +01:00
parent 353d38a5a4
commit 51d7ba7a33
3 changed files with 92 additions and 67 deletions

View File

@ -1,4 +1,6 @@
#include "arch.hpp"
#include "json.hpp"
#include "rpc_server.hpp"
#include "vm.hpp"
#include <format>
#include <iostream>
@ -88,12 +90,24 @@ auto compile_asm(const std::vector<AsmLine>& lines) -> std::vector<uint32_t>
return output;
}
auto execute_action(std::unique_ptr<sliger::json::Value> req,
std::unique_ptr<slige_rpc::BufferedWriter> writer) -> void
{
}
int main()
{
using R = Ref;
using L = Loc;
using enum sliger::Op;
auto rpc = slige_rpc::RpcServer(
[&](std::unique_ptr<sliger::json::Value> req,
std::unique_ptr<slige_rpc::BufferedWriter> writer) {
execute_action(std::move(req), std::move(writer));
});
rpc.listen();
// fn add(a, b) {
// + a b
// }

View File

@ -1,72 +1,7 @@
#include "rpc_server.hpp"
#include "json.hpp"
#include <memory>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <utility>
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 },
};
}
template <typename Functor>
auto slige_rpc::RpcServer<Functor>::listen() -> Res<Unit>
{
int socket_fd = ::socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
return Err { "could not get socket" };
};
sockaddr_in address = create_address(13370);
unsigned int address_size = sizeof(this->address);
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) {
int client = ::accept(
this->fd, (struct sockaddr*)&this->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);
auto writer = std::make_unique<BufferedWriter>(client);
this->functor(req, std::move(writer));
message.clear();
break;
}
}
}
std::unreachable();
}
auto slige_rpc::BufferedWriter::write(std::string message) -> Res<Unit>
{

View File

@ -1,5 +1,6 @@
#pragma once
#include "json.hpp"
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
@ -80,6 +81,18 @@ private:
int fd;
};
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 },
};
}
}
/// - load code
/// - program input
/// - run
@ -94,10 +107,73 @@ private:
/// - json string
template <typename Functor> class RpcServer {
public:
RpcServer(Functor functor)
RpcServer(Functor&& functor)
: functor(functor) {};
auto listen() -> Res<Unit>;
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();
};
private:
Functor functor;