143 lines
4.2 KiB
C++
143 lines
4.2 KiB
C++
#include <Void/Net/Socket.hpp>
|
|
|
|
#include <cerrno>
|
|
#include <algorithm>
|
|
#include <cstring>
|
|
#include <format>
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#ifdef _WIN32
|
|
namespace Void::Net
|
|
{
|
|
UdpSocket::~UdpSocket() = default;
|
|
UdpSocket::UdpSocket(UdpSocket&&) noexcept = default;
|
|
UdpSocket& UdpSocket::operator=(UdpSocket&&) noexcept = default;
|
|
Void::Result::Result<UdpSocket, Void::Result::Error>
|
|
UdpSocket::connect(std::string_view, std::uint16_t)
|
|
{
|
|
return Void::Result::fail("UdpSocket is not implemented on Windows");
|
|
}
|
|
void UdpSocket::close() noexcept {}
|
|
void UdpSocket::shutdown() noexcept {}
|
|
Void::Result::Result<bool, Void::Result::Error>
|
|
UdpSocket::wait_readable(std::chrono::milliseconds) const
|
|
{
|
|
return Void::Result::fail("UdpSocket is not implemented on Windows");
|
|
}
|
|
Void::Result::Result<std::size_t, Void::Result::Error>
|
|
UdpSocket::send(std::span<const std::uint8_t>)
|
|
{
|
|
return Void::Result::fail("UdpSocket is not implemented on Windows");
|
|
}
|
|
Void::Result::Result<std::size_t, Void::Result::Error>
|
|
UdpSocket::receive(std::span<std::uint8_t>)
|
|
{
|
|
return Void::Result::fail("UdpSocket is not implemented on Windows");
|
|
}
|
|
}
|
|
#else
|
|
#include <netdb.h>
|
|
#include <poll.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
|
|
namespace Void::Net
|
|
{
|
|
namespace
|
|
{
|
|
Void::Result::Failure<Void::Result::Error> socketError(
|
|
std::string_view operation)
|
|
{
|
|
return Void::Result::fail(std::format("{}: {}", operation,
|
|
std::strerror(errno)));
|
|
}
|
|
}
|
|
|
|
UdpSocket::~UdpSocket() { close(); }
|
|
UdpSocket::UdpSocket(UdpSocket&& other) noexcept
|
|
: handle_(std::exchange(other.handle_, -1))
|
|
{
|
|
}
|
|
UdpSocket& UdpSocket::operator=(UdpSocket&& other) noexcept
|
|
{
|
|
if (this != &other) {
|
|
close();
|
|
handle_ = std::exchange(other.handle_, -1);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
Void::Result::Result<UdpSocket, Void::Result::Error>
|
|
UdpSocket::connect(std::string_view host, std::uint16_t port)
|
|
{
|
|
addrinfo hints{};
|
|
hints.ai_family = AF_UNSPEC;
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
addrinfo* addresses = nullptr;
|
|
const auto service = std::to_string(port);
|
|
const auto hostname = std::string(host);
|
|
const int resolved = ::getaddrinfo(hostname.c_str(), service.c_str(),
|
|
&hints, &addresses);
|
|
if (resolved != 0) return Void::Result::fail(std::format(
|
|
"getaddrinfo '{}': {}", hostname, gai_strerror(resolved)));
|
|
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> guard(
|
|
addresses, &freeaddrinfo);
|
|
for (auto* address = addresses; address; address = address->ai_next) {
|
|
const int handle = ::socket(address->ai_family,
|
|
address->ai_socktype, address->ai_protocol);
|
|
if (handle < 0) continue;
|
|
if (::connect(handle, address->ai_addr, address->ai_addrlen) == 0)
|
|
return UdpSocket(handle);
|
|
::close(handle);
|
|
}
|
|
return socketError("UDP connect");
|
|
}
|
|
|
|
void UdpSocket::close() noexcept
|
|
{
|
|
if (handle_ >= 0) {
|
|
::close(handle_);
|
|
handle_ = -1;
|
|
}
|
|
}
|
|
|
|
void UdpSocket::shutdown() noexcept
|
|
{
|
|
if (handle_ >= 0) ::shutdown(handle_, SHUT_RDWR);
|
|
}
|
|
|
|
Void::Result::Result<bool, Void::Result::Error>
|
|
UdpSocket::wait_readable(std::chrono::milliseconds timeout) const
|
|
{
|
|
if (!valid()) return Void::Result::fail("UDP socket is closed");
|
|
pollfd descriptor{.fd = handle_, .events = POLLIN, .revents = 0};
|
|
const auto bounded = std::clamp<long long>(timeout.count(), 0,
|
|
std::numeric_limits<int>::max());
|
|
const int result = ::poll(&descriptor, 1, static_cast<int>(bounded));
|
|
if (result < 0 && errno != EINTR) return socketError("UDP poll");
|
|
return result > 0 && (descriptor.revents & POLLIN) != 0;
|
|
}
|
|
|
|
Void::Result::Result<std::size_t, Void::Result::Error>
|
|
UdpSocket::send(std::span<const std::uint8_t> bytes)
|
|
{
|
|
if (!valid()) return Void::Result::fail("UDP socket is closed");
|
|
const auto result = ::send(handle_, bytes.data(), bytes.size(), 0);
|
|
if (result < 0) return socketError("UDP send");
|
|
return static_cast<std::size_t>(result);
|
|
}
|
|
|
|
Void::Result::Result<std::size_t, Void::Result::Error>
|
|
UdpSocket::receive(std::span<std::uint8_t> destination)
|
|
{
|
|
if (!valid()) return Void::Result::fail("UDP socket is closed");
|
|
const auto result = ::recv(handle_, destination.data(), destination.size(), 0);
|
|
if (result < 0) return socketError("UDP receive");
|
|
return static_cast<std::size_t>(result);
|
|
}
|
|
}
|
|
#endif
|