mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 23:06:32 +00:00
89 lines
2.8 KiB
C++
89 lines
2.8 KiB
C++
#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) };
|
|
}
|