From 51d7ba7a3398ab28831727a56761c4e423c18428 Mon Sep 17 00:00:00 2001 From: Theis Pieter Hollebeek Date: Tue, 26 Nov 2024 12:49:24 +0100 Subject: [PATCH] begin work on parsing rpc server messages --- runtime/main.cpp | 14 ++++++++ runtime/rpc_server.cpp | 65 ---------------------------------- runtime/rpc_server.hpp | 80 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 92 insertions(+), 67 deletions(-) diff --git a/runtime/main.cpp b/runtime/main.cpp index 5ac7f73..7e4f21d 100644 --- a/runtime/main.cpp +++ b/runtime/main.cpp @@ -1,4 +1,6 @@ #include "arch.hpp" +#include "json.hpp" +#include "rpc_server.hpp" #include "vm.hpp" #include #include @@ -88,12 +90,24 @@ auto compile_asm(const std::vector& lines) -> std::vector return output; } +auto execute_action(std::unique_ptr req, + std::unique_ptr writer) -> void +{ +} + int main() { using R = Ref; using L = Loc; using enum sliger::Op; + auto rpc = slige_rpc::RpcServer( + [&](std::unique_ptr req, + std::unique_ptr writer) { + execute_action(std::move(req), std::move(writer)); + }); + rpc.listen(); + // fn add(a, b) { // + a b // } diff --git a/runtime/rpc_server.cpp b/runtime/rpc_server.cpp index 5b1fe0b..6fbf3de 100644 --- a/runtime/rpc_server.cpp +++ b/runtime/rpc_server.cpp @@ -1,72 +1,7 @@ #include "rpc_server.hpp" -#include "json.hpp" -#include #include #include #include -#include - -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 -auto slige_rpc::RpcServer::listen() -> Res -{ - 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(client); - this->functor(req, std::move(writer)); - message.clear(); - break; - } - } - } - std::unreachable(); -} auto slige_rpc::BufferedWriter::write(std::string message) -> Res { diff --git a/runtime/rpc_server.hpp b/runtime/rpc_server.hpp index fcf84ae..bcdd07e 100644 --- a/runtime/rpc_server.hpp +++ b/runtime/rpc_server.hpp @@ -1,5 +1,6 @@ #pragma once +#include "json.hpp" #include #include #include @@ -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 class RpcServer { public: - RpcServer(Functor functor) + RpcServer(Functor&& functor) : functor(functor) {}; - auto listen() -> Res; + auto listen() -> Res + { + + 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(client); + this->functor(std::move(req.val()), std::move(writer)); + message.clear(); + break; + } + } + } + std::unreachable(); + }; private: Functor functor;