io
C++ I/O scheduling library with asynchronous socket operations
Loading...
Searching...
No Matches
Public Member Functions | Friends | List of all members
io::socket::socket_handle Class Reference

A thread-safe, move-only RAII wrapper for a native socket handle. More...

#include <socket/socket_handle.hpp>

Public Member Functions

 socket_handle ()=default
 Default constructor. Initializes an invalid socket handle.
 
 socket_handle (const socket_handle &other)=delete
 Deleted copy constructor.
 
auto operator= (const socket_handle &other) -> socket_handle &=delete
 Deleted copy assignment operator.
 
 socket_handle (socket_handle &&other) noexcept
 Move constructor.
 
auto operator= (socket_handle &&other) noexcept -> socket_handle &
 Move assignment operator.
 
 socket_handle (native_socket_type handle)
 Constructs a socket_handle from an existing native socket.
 
 socket_handle (int domain, int type, int protocol)
 Constructs a socket_handle by creating a new socket.
 
 operator native_socket_type () const noexcept
 Explicitly converts the socket_handle to its underlying native socket representation.
 
 operator bool () const noexcept
 Checks if the handle owns a valid, open socket.
 
auto operator<=> (const socket_handle &other) const noexcept -> std::strong_ordering
 Three-way compares two socket_handle objects.
 
auto operator== (const socket_handle &other) const noexcept -> bool
 Checks for equality between two socket_handle objects.
 
auto operator<=> (native_socket_type other) const noexcept -> std::strong_ordering
 Three-way compares the socket_handle with a native socket handle.
 
auto operator== (native_socket_type other) const noexcept -> bool
 Checks for equality between a socket_handle and a native socket handle.
 
 ~socket_handle ()
 Destructor.
 

Friends

auto swap (socket_handle &lhs, socket_handle &rhs) noexcept -> void
 Swaps the contents of two socket_handle objects.
 

Detailed Description

A thread-safe, move-only RAII wrapper for a native socket handle.

This class is the cornerstone of socket lifetime management in this library. It wraps a native socket handle, ensuring it is automatically closed upon destruction. By deleting the copy constructor and copy assignment operator, it enforces unique ownership, preventing common errors related to resource duplication. All access to the handle is protected by a mutex, ensuring thread-safe operations.

Definition at line 46 of file socket_handle.hpp.

Constructor & Destructor Documentation

◆ socket_handle() [1/5]

io::socket::socket_handle::socket_handle ( )
default

Default constructor. Initializes an invalid socket handle.

A default-constructed handle does not represent a valid socket.

◆ socket_handle() [2/5]

io::socket::socket_handle::socket_handle ( const socket_handle other)
delete

Deleted copy constructor.

socket_handle is a unique, resource-owning type and cannot be copied.

◆ socket_handle() [3/5]

io::socket::socket_handle::socket_handle ( socket_handle &&  other)
noexcept

Move constructor.

Transfers ownership of a socket from another socket_handle. After the move, other is left in an invalid state. This operation is thread-safe.

Parameters
otherThe socket_handle to move from.

Definition at line 42 of file socket_handle.cpp.

◆ socket_handle() [4/5]

io::socket::socket_handle::socket_handle ( native_socket_type  handle)
explicit

Constructs a socket_handle from an existing native socket.

Takes ownership of the provided native handle.

Parameters
handleThe native socket handle to wrap.
Exceptions
std::system_errorif the handle is not a valid socket.

Definition at line 52 of file socket_handle.cpp.

◆ socket_handle() [5/5]

io::socket::socket_handle::socket_handle ( int  domain,
int  type,
int  protocol 
)

Constructs a socket_handle by creating a new socket.

Parameters
domainThe communication domain (e.g., AF_INET).
typeThe socket type (e.g., SOCK_STREAM).
protocolThe protocol (e.g., IPPROTO_TCP).
Exceptions
std::system_errorif socket creation fails.

Definition at line 57 of file socket_handle.cpp.

◆ ~socket_handle()

io::socket::socket_handle::~socket_handle ( )

Destructor.

Closes the managed socket if it is valid, ensuring RAII compliance.

Definition at line 102 of file socket_handle.cpp.

Member Function Documentation

◆ operator bool()

io::socket::socket_handle::operator bool ( ) const
explicitnoexcept

Checks if the handle owns a valid, open socket.

This operation is thread-safe and allows the handle to be used in boolean contexts (e.g., if (handle)).

Returns
true if the socket handle is valid, false otherwise.

Definition at line 78 of file socket_handle.cpp.

◆ operator native_socket_type()

io::socket::socket_handle::operator native_socket_type ( ) const
explicitnoexcept

Explicitly converts the socket_handle to its underlying native socket representation.

This operation is thread-safe.

Returns
The raw native socket handle.

Definition at line 63 of file socket_handle.cpp.

◆ operator<=>() [1/2]

auto io::socket::socket_handle::operator<=> ( const socket_handle other) const -> std::strong_ordering
noexcept

Three-way compares two socket_handle objects.

The comparison is based on the underlying native socket values. This operation is thread-safe.

Parameters
otherThe socket_handle to compare against.
Returns
A std::strong_ordering value.

Definition at line 82 of file socket_handle.cpp.

◆ operator<=>() [2/2]

auto io::socket::socket_handle::operator<=> ( native_socket_type  other) const -> std::strong_ordering
noexcept

Three-way compares the socket_handle with a native socket handle.

This operation is thread-safe.

Parameters
otherThe native socket handle to compare against.
Returns
A std::strong_ordering value.

Definition at line 92 of file socket_handle.cpp.

◆ operator=() [1/2]

auto io::socket::socket_handle::operator= ( const socket_handle other) -> socket_handle &=delete
delete

Deleted copy assignment operator.

socket_handle is a unique, resource-owning type and cannot be copied.

◆ operator=() [2/2]

auto io::socket::socket_handle::operator= ( socket_handle &&  other) -> socket_handle &
noexcept

Move assignment operator.

Transfers ownership of a socket from another socket_handle. If this handle already owns a socket, it is closed before the new socket is acquired. After the move, other is left in an invalid state. This operation is thread-safe.

Parameters
otherThe socket_handle to move from.
Returns
A reference to this socket_handle.

Definition at line 46 of file socket_handle.cpp.

◆ operator==() [1/2]

auto io::socket::socket_handle::operator== ( const socket_handle other) const -> bool
noexcept

Checks for equality between two socket_handle objects.

This operation is thread-safe.

Parameters
otherThe socket_handle to compare against.
Returns
true if the underlying native socket handles are equal, false otherwise.

Definition at line 87 of file socket_handle.cpp.

◆ operator==() [2/2]

auto io::socket::socket_handle::operator== ( native_socket_type  other) const -> bool
noexcept

Checks for equality between a socket_handle and a native socket handle.

This operation is thread-safe.

Parameters
otherThe native socket handle to compare against.
Returns
true if the underlying native socket handles are equal, false otherwise.

Definition at line 97 of file socket_handle.cpp.

Friends And Related Symbol Documentation

◆ swap

auto swap ( socket_handle lhs,
socket_handle rhs 
) -> void
friend

Swaps the contents of two socket_handle objects.

This operation is thread-safe.

Parameters
lhsThe first socket_handle.
rhsThe second socket_handle.

Definition at line 67 of file socket_handle.cpp.


The documentation for this class was generated from the following files: