16#include "../error.hpp"
18#include <system_error>
21static auto throw_system_error(
const char *message) ->
void {
22 throw std::system_error(errno, std::generic_category(), message);
28 if (handle == INVALID_SOCKET)
34 return ::getsockopt(handle, SOL_SOCKET, SO_TYPE, &type, &len) == 0;
53 if (!is_valid_socket(handle))
58 : socket_{::socket(domain, type, protocol)} {
59 if (socket_ == INVALID_SOCKET)
64 return socket_.load(std::memory_order_relaxed);
71 std::scoped_lock lock(lhs.mtx_, rhs.mtx_);
74 auto temp = lhs.socket_.exchange(rhs.socket_);
75 rhs.socket_.store(temp);
78socket_handle::operator bool() const noexcept {
79 return socket_ != INVALID_SOCKET;
83 -> std::strong_ordering {
84 return socket_ <=> other.socket_;
89 return (*this <=> other) == 0;
93 -> std::strong_ordering {
94 return socket_ <=> other;
99 return (*this <=> other) == 0;
104auto socket_handle::close() noexcept ->
void {
105 std::lock_guard lock{mtx_};
106 if (socket_ != INVALID_SOCKET) {
108 socket_ = INVALID_SOCKET;
A thread-safe, move-only RAII wrapper for a native socket handle.
~socket_handle()
Destructor.
auto operator=(const socket_handle &other) -> socket_handle &=delete
Deleted copy assignment operator.
socket_handle()=default
Default constructor. Initializes an invalid socket handle.
auto operator==(const socket_handle &other) const noexcept -> bool
Checks for equality between two socket_handle objects.
auto operator<=>(const socket_handle &other) const noexcept -> std::strong_ordering
Three-way compares two socket_handle objects.
#define IO_ERROR_MESSAGE(msg)
Constructs a formatted error message with the file and line number.
The io::socket namespace provides a cross-platform abstraction for socket-level I/O operations.
auto close(native_socket_type socket) noexcept -> int
Closes a socket descriptor on POSIX systems.
int native_socket_type
The native socket handle type for POSIX systems.
auto swap(socket_handle &lhs, socket_handle &rhs) noexcept -> void
socklen_t socklen_type
The type used to represent socket-related sizes on POSIX systems.
This file defines the socket_handle class, a cross-platform, thread-safe RAII wrapper for native sock...