diff --git a/runtime/main.cpp b/runtime/main.cpp index 7e4f21d..1c24bad 100644 --- a/runtime/main.cpp +++ b/runtime/main.cpp @@ -91,7 +91,7 @@ auto compile_asm(const std::vector& lines) -> std::vector } auto execute_action(std::unique_ptr req, - std::unique_ptr writer) -> void + std::unique_ptr 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 req, - std::unique_ptr writer) { + std::unique_ptr writer) { execute_action(std::move(req), std::move(writer)); }); rpc.listen(); diff --git a/runtime/rpc_server.cpp b/runtime/rpc_server.cpp index 6fbf3de..1dc4eaa 100644 --- a/runtime/rpc_server.cpp +++ b/runtime/rpc_server.cpp @@ -3,7 +3,7 @@ #include #include -auto slige_rpc::BufferedWriter::write(std::string message) -> Res +auto sliger::rpc::BufferedWriter::write(std::string message) -> Res { 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 return Unit {}; } -auto slige_rpc::BufferedWriter::write(uint8_t byte) -> Res +auto sliger::rpc::BufferedWriter::write(uint8_t byte) -> Res { if (this->occupied >= length) { auto res = this->flush(); @@ -26,7 +26,7 @@ auto slige_rpc::BufferedWriter::write(uint8_t byte) -> Res return Unit {}; } -auto slige_rpc::BufferedWriter::flush() -> Res +auto sliger::rpc::BufferedWriter::flush() -> Res { auto result = ::write(this->fd, this->buffer, this->occupied); if (result < 0) { diff --git a/runtime/rpc_server.hpp b/runtime/rpc_server.hpp index bcdd07e..ca1d417 100644 --- a/runtime/rpc_server.hpp +++ b/runtime/rpc_server.hpp @@ -7,7 +7,7 @@ #include #include -namespace slige_rpc { +namespace sliger::rpc { struct Err { std::string msg; @@ -112,31 +112,38 @@ public: auto listen() -> Res { - - 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()) {