#include #include #include #include #include #include #include #include #include #ifdef _WIN32 namespace Void::Net { UdpSocket::~UdpSocket() = default; UdpSocket::UdpSocket(UdpSocket&&) noexcept = default; UdpSocket& UdpSocket::operator=(UdpSocket&&) noexcept = default; Void::Result::Result 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 UdpSocket::wait_readable(std::chrono::milliseconds) const { return Void::Result::fail("UdpSocket is not implemented on Windows"); } Void::Result::Result UdpSocket::send(std::span) { return Void::Result::fail("UdpSocket is not implemented on Windows"); } Void::Result::Result UdpSocket::receive(std::span) { return Void::Result::fail("UdpSocket is not implemented on Windows"); } } #else #include #include #include #include namespace Void::Net { namespace { Void::Result::Failure 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::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 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 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(timeout.count(), 0, std::numeric_limits::max()); const int result = ::poll(&descriptor, 1, static_cast(bounded)); if (result < 0 && errno != EINTR) return socketError("UDP poll"); return result > 0 && (descriptor.revents & POLLIN) != 0; } Void::Result::Result UdpSocket::send(std::span 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(result); } Void::Result::Result UdpSocket::receive(std::span 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(result); } } #endif