slige/runtime/rpc_server.hpp

75 lines
1.4 KiB
C++

#pragma once
#include <arpa/inet.h>
#include <cstdint>
#include <netdb.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <variant>
namespace slige_rpc {
class RpcServer {
public:
private:
};
struct Ewwow {
std::string message;
};
namespace {
class ClientSocket {
public:
~ClientSocket() { close(fd); };
ClientSocket(int fd)
: fd(fd)
{
}
auto Read(
uint8_t* buffer, size_t length) -> std::variant<size_t, Ewwow>;
auto Write(
uint8_t* buffer, size_t length) -> std::variant<size_t, Ewwow>;
private:
int fd;
};
class ServerSocket {
public:
ServerSocket(int fd, sockaddr_in address)
: fd(fd)
, address(address)
{
}
~ServerSocket() { close(fd); };
auto Accept() -> std::variant<ClientSocket, Ewwow>;
private:
int fd;
sockaddr_in address;
};
}
class Socket {
public:
Socket(uint16_t port)
{
this->address = {
.sin_family = AF_INET,
.sin_port = htons(port),
.sin_addr = { .s_addr = inet_addr("127.0.0.1") },
.sin_zero = { 0 },
};
}
auto Connect() -> std::variant<ClientSocket, Ewwow>;
auto Bind() -> std::variant<ServerSocket, Ewwow>;
private:
sockaddr_in address;
};
};