this->should work idk

This commit is contained in:
Theis Pieter Hollebeek 2024-11-20 14:30:43 +01:00
parent d26da549cc
commit 0b9d7c7d8a
2 changed files with 45 additions and 1 deletions

View File

@ -1,4 +1,5 @@
#include "rpc_server.hpp" #include "rpc_server.hpp"
#include "json.hpp"
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
@ -38,7 +39,29 @@ auto slige_rpc::RpcServer<Functor>::listen() -> Res<Unit>
if (client < 0) { if (client < 0) {
return Err { "could not accept" }; 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(); std::unreachable();
} }

View File

@ -34,10 +34,31 @@ private:
Err error; 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 Unit { };
struct Req { }; struct Req { };
class BufferedWriter { class BufferedWriter {
public:
BufferedWriter(int fd) BufferedWriter(int fd)
: fd(fd) : fd(fd)
{ {