diff --git a/runtime/rpc_server.cpp b/runtime/rpc_server.cpp index 24b9a2b..9d110da 100644 --- a/runtime/rpc_server.cpp +++ b/runtime/rpc_server.cpp @@ -1,4 +1,5 @@ #include "rpc_server.hpp" +#include "json.hpp" #include #include #include @@ -38,7 +39,29 @@ auto slige_rpc::RpcServer::listen() -> Res if (client < 0) { return Err { "could not accept" }; } - uint8_t buffer[1024] = {}; + 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 = BufferedWriter(client); + this->functor(req, writer); + message.clear(); + } + } } std::unreachable(); } diff --git a/runtime/rpc_server.hpp b/runtime/rpc_server.hpp index e06ceaf..6e3b9c2 100644 --- a/runtime/rpc_server.hpp +++ b/runtime/rpc_server.hpp @@ -34,10 +34,31 @@ private: Err error; }; +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; +}; + struct Unit { }; struct Req { }; class BufferedWriter { +public: BufferedWriter(int fd) : fd(fd) {