this->should work idk
This commit is contained in:
parent
d26da549cc
commit
0b9d7c7d8a
@ -1,4 +1,5 @@
|
||||
#include "rpc_server.hpp"
|
||||
#include "json.hpp"
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
@ -38,7 +39,29 @@ auto slige_rpc::RpcServer<Functor>::listen() -> Res<Unit>
|
||||
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();
|
||||
}
|
||||
|
@ -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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user