refactor slige_rpc -> sliger::rpc

This commit is contained in:
Theis Pieter Hollebeek 2024-11-26 13:04:40 +01:00
parent 51d7ba7a33
commit 4dccbcb17a
3 changed files with 32 additions and 24 deletions

View File

@ -91,7 +91,7 @@ auto compile_asm(const std::vector<AsmLine>& lines) -> std::vector<uint32_t>
}
auto execute_action(std::unique_ptr<sliger::json::Value> req,
std::unique_ptr<slige_rpc::BufferedWriter> writer) -> void
std::unique_ptr<sliger::rpc::BufferedWriter> writer)
{
}
@ -101,9 +101,9 @@ int main()
using L = Loc;
using enum sliger::Op;
auto rpc = slige_rpc::RpcServer(
auto rpc = sliger::rpc::RpcServer(
[&](std::unique_ptr<sliger::json::Value> req,
std::unique_ptr<slige_rpc::BufferedWriter> writer) {
std::unique_ptr<sliger::rpc::BufferedWriter> writer) {
execute_action(std::move(req), std::move(writer));
});
rpc.listen();

View File

@ -3,7 +3,7 @@
#include <sys/socket.h>
#include <unistd.h>
auto slige_rpc::BufferedWriter::write(std::string message) -> Res<Unit>
auto sliger::rpc::BufferedWriter::write(std::string message) -> Res<Unit>
{
for (size_t i = 0; i < message.length(); ++i) {
auto res = this->write((uint8_t)message[i]);
@ -14,7 +14,7 @@ auto slige_rpc::BufferedWriter::write(std::string message) -> Res<Unit>
return Unit {};
}
auto slige_rpc::BufferedWriter::write(uint8_t byte) -> Res<Unit>
auto sliger::rpc::BufferedWriter::write(uint8_t byte) -> Res<Unit>
{
if (this->occupied >= length) {
auto res = this->flush();
@ -26,7 +26,7 @@ auto slige_rpc::BufferedWriter::write(uint8_t byte) -> Res<Unit>
return Unit {};
}
auto slige_rpc::BufferedWriter::flush() -> Res<size_t>
auto sliger::rpc::BufferedWriter::flush() -> Res<size_t>
{
auto result = ::write(this->fd, this->buffer, this->occupied);
if (result < 0) {

View File

@ -7,7 +7,7 @@
#include <string>
#include <unistd.h>
namespace slige_rpc {
namespace sliger::rpc {
struct Err {
std::string msg;
@ -112,31 +112,38 @@ public:
auto listen() -> Res<Unit>
{
int socket_fd = ::socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
return Err { "could not get socket" };
};
int socket;
{
auto address = create_address(13370);
if (::bind(socket_fd, (struct sockaddr*)&address, sizeof(address))
< 0) {
return Err { "could not bind" };
socket = ::socket(AF_INET, SOCK_STREAM, 0);
if (socket < 0) {
return Err { .msg
= std::format("could not get socket ({})", socket) };
};
}
if (::listen(socket_fd, 0) < 0) {
return Err { "could not listen" };
{
auto address = create_address(13370);
auto err
= ::bind(socket, (struct sockaddr*)&address, sizeof(address));
if (err < 0) {
return Err { .msg = std::format("could not bind ({})", err) };
};
return Unit {};
}
{
auto err = ::listen(socket, 0);
if (err < 0) {
return Err { .msg = std::format("could not listen ({})", err) };
}
}
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);
socket, (struct sockaddr*)&client_address, &address_size);
if (client < 0) {
return Err { "could not accept" };
return Err { .msg
= std::format("could not accept ({})", client) };
}
const size_t buf_len = 1024;
int8_t buffer[buf_len] = {};
@ -145,11 +152,12 @@ public:
while (true) {
ssize_t bytes_read = read(client, buffer, buf_len);
if (bytes_read < 0) {
return Err { "could not read" };
return Err { .msg
= std::format("could not read ({})", bytes_read) };
} else if (bytes_read == 0) {
break;
}
for (size_t i; i < (size_t)bytes_read; ++i) {
for (size_t i = 0; i < (size_t)bytes_read; ++i) {
message += buffer[i];
bracket_finder.feed(buffer[i]);
if (!bracket_finder.bracket_closed()) {