slige/runtime/rpc_server.hpp

75 lines
1.4 KiB
C++
Raw Normal View History

2024-11-12 08:52:55 +00:00
#pragma once
2024-11-18 11:35:38 +00:00
#include <arpa/inet.h>
#include <cstdint>
#include <netdb.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <variant>
2024-11-12 08:52:55 +00:00
namespace slige_rpc {
class RpcServer {
public:
private:
};
2024-11-18 11:35:38 +00:00
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;
};
2024-11-12 08:52:55 +00:00
}
2024-11-18 11:35:38 +00:00
class Socket {
public:
Socket(uint16_t port)
{
this->address = {
2024-11-18 11:35:38 +00:00
.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;
};
};