mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 07:46:32 +00:00
rpc_server progress
This commit is contained in:
parent
0a4ba8fa95
commit
fca7370fee
@ -1,6 +1,6 @@
|
||||
|
||||
CXX_FLAGS = \
|
||||
-std=c++20 \
|
||||
-std=c++23 \
|
||||
-Og \
|
||||
-fsanitize=address,undefined \
|
||||
-pedantic -pedantic-errors \
|
||||
|
@ -1,5 +1,5 @@
|
||||
-xc++
|
||||
-std=c++20
|
||||
-std=c++23
|
||||
-pedantic
|
||||
-pedantic-errors
|
||||
-Wall
|
||||
|
@ -1 +1,84 @@
|
||||
#include "rpc_server.hpp"
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
auto slige_rpc::ClientSocket::Read(
|
||||
uint8_t* buffer, size_t length) -> std::variant<size_t, slige_rpc::Ewwow>
|
||||
{
|
||||
ssize_t bytes_read = recv(fd, buffer, length, 0);
|
||||
if (bytes_read < 0) {
|
||||
return Ewwow { .message = "unable to read" };
|
||||
}
|
||||
return (size_t)bytes_read;
|
||||
};
|
||||
|
||||
auto slige_rpc::ClientSocket::Write(
|
||||
uint8_t* buffer, size_t length) -> std::variant<size_t, slige_rpc::Ewwow>
|
||||
{
|
||||
ssize_t bytes_written = send(fd, buffer, length, 0);
|
||||
if (bytes_written < 0) {
|
||||
return Ewwow { .message = "unable to write" };
|
||||
}
|
||||
return (size_t)bytes_written;
|
||||
};
|
||||
|
||||
auto slige_rpc::Socket::Connect()
|
||||
-> std::variant<ClientSocket, slige_rpc::Ewwow>
|
||||
{
|
||||
int socket_fd;
|
||||
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
return { Ewwow { .message = "could not get socket" } };
|
||||
}
|
||||
// TODO:
|
||||
// 1) does address (presumably 'socket)
|
||||
// live as long as child socket ('client)?
|
||||
// - immediate answer is no
|
||||
// 2) does it need to?
|
||||
if (connect(socket_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
|
||||
return { Ewwow {
|
||||
.message = "could not connect, is the server running?" } };
|
||||
}
|
||||
return { ClientSocket(socket_fd) };
|
||||
}
|
||||
|
||||
auto slige_rpc::ServerSocket::Accept()
|
||||
-> std::variant<ClientSocket, slige_rpc::Ewwow>
|
||||
{
|
||||
unsigned int address_size = sizeof(address);
|
||||
// TODO:
|
||||
// 1) does address (presumably 'server)
|
||||
// live as long as child socket ('socket)?
|
||||
// - immediate answer is no
|
||||
// 2) does it need to?
|
||||
int client = accept(fd, (struct sockaddr*)&address, &address_size);
|
||||
if (client < 0) {
|
||||
return Ewwow { .message = "could not accept" };
|
||||
}
|
||||
return client;
|
||||
return { ClientSocket(client) };
|
||||
}
|
||||
|
||||
auto slige_rpc::Socket::Bind() -> std::variant<ServerSocket, slige_rpc::Ewwow>
|
||||
{
|
||||
int socket_fd;
|
||||
if ((socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
return { Ewwow { .message = "could not get socket" } };
|
||||
}
|
||||
// TODO:
|
||||
// 1) does address (presumably 'socket)
|
||||
// live as long as child socket ('server)?
|
||||
// - immediate answer is no
|
||||
// 2) does it need to?
|
||||
if (bind(socket_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
|
||||
return { Ewwow { .message = "could not bind" } };
|
||||
}
|
||||
|
||||
if (listen(socket_fd, 0) < 0) {
|
||||
return { Ewwow { .message = "could not listen" } };
|
||||
}
|
||||
// TODO:
|
||||
// 1) does this address get moved, copied or cloned?
|
||||
// it should be represented as a u32,
|
||||
// so copying should be fine - but does it?
|
||||
return { ServerSocket(socket_fd, address) };
|
||||
}
|
||||
|
@ -1,5 +1,13 @@
|
||||
#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 {
|
||||
@ -7,4 +15,60 @@ 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)
|
||||
{
|
||||
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;
|
||||
};
|
||||
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user