slige/runtime/socket.hpp

56 lines
1.0 KiB
C++
Raw Normal View History

2024-11-20 11:07:36 +01:00
#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;
};
};