Add socket abstraction

This commit is contained in:
Maurice Heumann
2025-03-20 15:17:43 +01:00
parent 2cb14a3555
commit 4da6642123
15 changed files with 437 additions and 71 deletions

View File

@@ -0,0 +1,59 @@
#include "socket_wrapper.hpp"
#include <cassert>
namespace network
{
socket_wrapper::socket_wrapper(const int af, const int type, const int protocol)
: socket_(af, type, protocol)
{
}
void socket_wrapper::set_blocking(const bool blocking)
{
this->socket_.set_blocking(blocking);
}
int socket_wrapper::get_last_error()
{
return GET_SOCKET_ERROR();
}
bool socket_wrapper::is_ready(const bool in_poll)
{
return this->socket_.is_ready(in_poll);
}
bool socket_wrapper::bind(const address& addr)
{
return this->socket_.bind(addr);
}
sent_size socket_wrapper::send(const std::span<const std::byte> data)
{
return ::send(this->socket_.get_socket(), reinterpret_cast<const char*>(data.data()),
static_cast<send_size>(data.size()), 0);
}
sent_size socket_wrapper::sendto(const address& destination, const std::span<const std::byte> data)
{
return ::sendto(this->socket_.get_socket(), reinterpret_cast<const char*>(data.data()),
static_cast<send_size>(data.size()), 0, &destination.get_addr(), destination.get_size());
}
sent_size socket_wrapper::recv(std::span<std::byte> data)
{
return ::recv(this->socket_.get_socket(), reinterpret_cast<char*>(data.data()),
static_cast<send_size>(data.size()), 0);
}
sent_size socket_wrapper::recvfrom(address& source, std::span<std::byte> data)
{
auto source_length = source.get_max_size();
const auto res = ::recvfrom(this->socket_.get_socket(), reinterpret_cast<char*>(data.data()),
static_cast<send_size>(data.size()), 0, &source.get_addr(), &source_length);
assert(source.get_size() == source_length);
return res;
}
}