39 lines
924 B
C++
39 lines
924 B
C++
#include "rpc_server.hpp"
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
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]);
|
|
if (!res.is_ok()) {
|
|
return res.err();
|
|
}
|
|
}
|
|
return Unit {};
|
|
}
|
|
|
|
auto sliger::rpc::BufferedWriter::write(uint8_t byte) -> Res<Unit>
|
|
{
|
|
if (this->occupied >= length) {
|
|
auto res = this->flush();
|
|
if (!res.is_ok()) {
|
|
return res.err();
|
|
}
|
|
}
|
|
this->buffer[this->occupied] = byte;
|
|
this->occupied += 1;
|
|
return Unit {};
|
|
}
|
|
|
|
auto sliger::rpc::BufferedWriter::flush() -> Res<size_t>
|
|
{
|
|
auto result = ::write(this->fd, this->buffer, this->occupied);
|
|
if (result < 0) {
|
|
return { { "unable to write" } };
|
|
}
|
|
this->occupied = 0;
|
|
return (size_t)result;
|
|
}
|