41 lines
1.2 KiB
C++
41 lines
1.2 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Error.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <span>
|
|
#include <string_view>
|
|
|
|
namespace Void::Net
|
|
{
|
|
class UdpSocket final
|
|
{
|
|
public:
|
|
UdpSocket() noexcept = default;
|
|
~UdpSocket();
|
|
UdpSocket(UdpSocket&& other) noexcept;
|
|
UdpSocket& operator=(UdpSocket&& other) noexcept;
|
|
UdpSocket(const UdpSocket&) = delete;
|
|
UdpSocket& operator=(const UdpSocket&) = delete;
|
|
|
|
[[nodiscard]] static Void::Result::Result<UdpSocket, Void::Result::Error>
|
|
connect(std::string_view host, std::uint16_t port);
|
|
[[nodiscard]] bool valid() const noexcept { return handle_ >= 0; }
|
|
[[nodiscard]] int native_handle() const noexcept { return handle_; }
|
|
void close() noexcept;
|
|
void shutdown() noexcept;
|
|
[[nodiscard]] Void::Result::Result<bool, Void::Result::Error>
|
|
wait_readable(std::chrono::milliseconds timeout) const;
|
|
[[nodiscard]] Void::Result::Result<std::size_t, Void::Result::Error>
|
|
send(std::span<const std::uint8_t> bytes);
|
|
[[nodiscard]] Void::Result::Result<std::size_t, Void::Result::Error>
|
|
receive(std::span<std::uint8_t> destination);
|
|
|
|
private:
|
|
explicit UdpSocket(int handle) noexcept : handle_(handle) {}
|
|
int handle_{-1};
|
|
};
|
|
}
|