this->considered harmful?

This commit is contained in:
Theis Pieter Hollebeek 2024-11-20 11:07:36 +01:00
parent b2563125e7
commit e269ad7e77
4 changed files with 170 additions and 136 deletions

View File

@ -1,87 +1,15 @@
#include "rpc_server.hpp" #include "rpc_server.hpp"
#include "socket.hpp"
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
auto slige_rpc::ClientSocket::Read( auto slige_rpc::RpcServer::bind(
uint8_t* buffer, size_t length) -> std::variant<size_t, slige_rpc::Ewwow> uint16_t port) -> std::variant<slige_rpc::RpcServer, slige_socket::Ewwow>
{ {
ssize_t bytes_read = recv(this->fd, buffer, length, 0); std::variant<slige_socket::ServerSocket, slige_socket::Ewwow> socket_result
if (bytes_read < 0) { = slige_socket::Socket::bind(port);
return Ewwow { .message = "unable to read" }; if (std::holds_alternative<slige_socket::Ewwow>(socket_result)) {
return std::get<slige_socket::Ewwow>(socket_result);
} }
return (size_t)bytes_read; return RpcServer(std::get<slige_socket::ServerSocket>(socket_result));
};
auto slige_rpc::ClientSocket::Write(
uint8_t* buffer, size_t length) -> std::variant<size_t, slige_rpc::Ewwow>
{
ssize_t bytes_written = send(this->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*)&this->address, sizeof(this->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(this->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(this->fd, (struct sockaddr*)&this->address, &address_size);
if (client < 0) {
return Ewwow { .message = "could not accept" };
}
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*)&this->address, sizeof(this->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, this->address) };
} }

View File

@ -1,74 +1,37 @@
#pragma once #pragma once
#include "socket.hpp"
#include <arpa/inet.h> #include <arpa/inet.h>
#include <cstdint> #include <cstdint>
#include <netdb.h> #include <netdb.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#include <unistd.h> #include <unistd.h>
#include <variant> #include <variant>
namespace slige_rpc { namespace slige_rpc {
/// - load code
/// - program input
/// - run
/// - run debug
/// - fwamegwaph option
/// - code covewage option
/// - fetch fwamegwaph
/// - json string
/// - fetch code covewage
/// - json string
/// - fetch stack
/// - json string
class RpcServer { class RpcServer {
public: public:
auto static bind(uint16_t port)
-> std::variant<slige_rpc::RpcServer, slige_socket::Ewwow>;
private: private:
}; RpcServer(slige_socket::ServerSocket socket)
: socket(socket)
struct Ewwow {
std::string message;
};
namespace {
class ClientSocket {
public:
~ClientSocket() { close(fd); };
ClientSocket(int fd)
: fd(fd)
{ {
} }
auto Read( slige_socket::ServerSocket socket;
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;
};
}; };

88
runtime/socket.cpp Normal file
View File

@ -0,0 +1,88 @@
#include "socket.hpp"
#include <sys/socket.h>
#include <unistd.h>
auto slige_socket::ClientSocket::read(
uint8_t* buffer, size_t length) -> std::variant<size_t, slige_socket::Ewwow>
{
ssize_t bytes_read = recv(this->fd, buffer, length, 0);
if (bytes_read < 0) {
return Ewwow { .message = "unable to read" };
}
return (size_t)bytes_read;
};
auto slige_socket::ClientSocket::write(
uint8_t* buffer, size_t length) -> std::variant<size_t, slige_socket::Ewwow>
{
ssize_t bytes_written = send(this->fd, buffer, length, 0);
if (bytes_written < 0) {
return Ewwow { .message = "unable to write" };
}
return (size_t)bytes_written;
};
auto slige_socket::Socket::connect(
uint16_t port) -> std::variant<ClientSocket, slige_socket::Ewwow>
{
auto address = Socket::create_address(port);
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_socket::ServerSocket::accept()
-> std::variant<ClientSocket, slige_socket::Ewwow>
{
unsigned int address_size = sizeof(this->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(this->fd, (struct sockaddr*)&this->address, &address_size);
if (client < 0) {
return Ewwow { .message = "could not accept" };
}
return { ClientSocket(client) };
}
auto slige_socket::Socket::bind(
uint16_t port) -> std::variant<ServerSocket, slige_socket::Ewwow>
{
sockaddr_in address = Socket::create_address(port);
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) };
}

55
runtime/socket.hpp Normal file
View File

@ -0,0 +1,55 @@
#pragma once
#include <arpa/inet.h>
#include <cstdint>
#include <netdb.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <variant>
namespace slige_socket {
struct Ewwow {
std::string message;
};
class ClientSocket {
public:
ClientSocket(int fd)
: fd(fd)
{
}
~ClientSocket() { close(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:
auto static connect(uint16_t port) -> std::variant<ClientSocket, Ewwow>;
auto static bind(uint16_t port) -> std::variant<ServerSocket, Ewwow>;
private:
auto static create_address(uint16_t port) -> sockaddr_in;
};
};