slige/runtime/rpc_server.cpp

106 lines
2.6 KiB
C++
Raw Permalink Normal View History

2024-11-12 08:52:55 +00:00
#include "rpc_server.hpp"
2024-12-13 15:11:16 +00:00
extern "C" {
#include <arpa/inet.h>
#include <asm-generic/socket.h>
#include <netdb.h>
2024-11-20 12:04:55 +00:00
#include <netinet/in.h>
2024-12-13 15:11:16 +00:00
#include <stdlib.h>
#include <string>
2024-11-18 11:35:38 +00:00
#include <sys/socket.h>
#include <unistd.h>
2024-12-13 15:11:16 +00:00
}
using namespace sliger::rpc;
static auto create_address(uint16_t port) -> sockaddr_in
{
return {
.sin_family = AF_INET,
.sin_port = ::htons(port),
.sin_addr = { .s_addr = ::inet_addr("127.0.0.1") },
.sin_zero = { 0 },
};
}
2024-11-20 12:04:55 +00:00
2024-12-13 15:11:16 +00:00
auto Socket::init() -> Res<void>
2024-11-20 12:04:55 +00:00
{
2024-12-13 15:11:16 +00:00
this->socket_fd = ::socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd < 0) {
return Err { .msg
= std::format("could not get socket ({})", socket_fd) };
};
this->initialized = true;
int trueValue = 1;
::setsockopt(
this->socket_fd, SOL_SOCKET, SO_REUSEADDR, &trueValue, sizeof(int));
int err;
auto address = create_address(13370);
err = ::bind(socket_fd, (struct sockaddr*)&address, sizeof(address));
if (err < 0) {
return Err { .msg = std::format("could not bind ({})", err) };
};
err = ::listen(socket_fd, 0);
if (err < 0) {
return Err { .msg = std::format("could not listen ({})", err) };
}
return {};
}
auto Socket::accept() -> Res<std::unique_ptr<Client>>
{
auto client_address = create_address(13370);
socklen_t address_size = sizeof(client_address);
int client
= ::accept(socket_fd, (struct sockaddr*)&client_address, &address_size);
if (client < 0) {
return Err { .msg = std::format("could not accept ({})", client) };
}
return std::make_unique<Client>(client);
}
auto Client::read(int8_t* buffer, size_t buffer_size) -> ssize_t
{
return ::read(this->client_sock, buffer, buffer_size);
}
auto Client::write(uint8_t* buffer, size_t buffer_size) -> ssize_t
{
return ::write(this->client_sock, buffer, buffer_size);
}
auto BufferedWriter::write(std::string message) -> Res<void>
{
for (auto ch : message) {
if (auto res = this->write((uint8_t)ch); !res.is_ok()) {
2024-11-20 12:04:55 +00:00
return res.err();
}
}
2024-12-13 15:11:16 +00:00
return {};
2024-11-20 12:04:55 +00:00
}
2024-12-13 15:11:16 +00:00
auto BufferedWriter::write(uint8_t byte) -> Res<void>
2024-11-20 12:04:55 +00:00
{
if (this->occupied >= length) {
2024-12-13 15:11:16 +00:00
if (auto res = this->flush(); !res.is_ok()) {
2024-11-20 12:04:55 +00:00
return res.err();
}
}
this->buffer[this->occupied] = byte;
2024-12-12 10:32:58 +00:00
this->occupied += 1;
2024-12-13 15:11:16 +00:00
return {};
2024-11-20 12:04:55 +00:00
}
2024-12-13 15:11:16 +00:00
auto BufferedWriter::flush() -> Res<size_t>
2024-11-20 12:04:55 +00:00
{
2024-12-13 15:11:16 +00:00
auto result = this->client->write(this->buffer, this->occupied);
2024-11-20 12:04:55 +00:00
if (result < 0) {
2024-12-13 15:11:16 +00:00
return Err("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
}