slige/runtime/rpc_server.cpp

39 lines
924 B
C++
Raw Normal View History

2024-11-12 08:52:55 +00:00
#include "rpc_server.hpp"
2024-11-20 12:04:55 +00:00
#include <netinet/in.h>
2024-11-18 11:35:38 +00:00
#include <sys/socket.h>
#include <unistd.h>
2024-11-20 12:04:55 +00:00
2024-11-26 12:04:40 +00:00
auto sliger::rpc::BufferedWriter::write(std::string message) -> Res<Unit>
2024-11-20 12:04:55 +00:00
{
for (size_t i = 0; i < message.length(); ++i) {
auto res = this->write((uint8_t)message[i]);
if (!res.is_ok()) {
return res.err();
}
}
return Unit {};
}
2024-11-26 12:04:40 +00:00
auto sliger::rpc::BufferedWriter::write(uint8_t byte) -> Res<Unit>
2024-11-20 12:04:55 +00:00
{
if (this->occupied >= length) {
auto res = this->flush();
if (!res.is_ok()) {
return res.err();
}
}
this->buffer[this->occupied] = byte;
2024-12-12 10:32:58 +00:00
this->occupied += 1;
2024-11-20 12:04:55 +00:00
return Unit {};
}
2024-11-26 12:04:40 +00:00
auto sliger::rpc::BufferedWriter::flush() -> Res<size_t>
2024-11-20 12:04:55 +00:00
{
auto result = ::write(this->fd, this->buffer, this->occupied);
if (result < 0) {
return { { "unable to write" } };
2024-11-18 11:35:38 +00:00
}
2024-11-20 12:04:55 +00:00
this->occupied = 0;
return (size_t)result;
2024-11-18 11:35:38 +00:00
}