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_message Class Reference

A thread-safe container for socket messages used in advanced I/O operations. More...

#include <socket/socket_message.hpp>

Public Member Functions

 socket_message ()
 Default constructor.
 
 socket_message (const socket_message &other)=delete
 Deleted copy constructor.
 
auto operator= (const socket_message &other) -> socket_message &=delete
 Deleted copy assignment operator.
 
 socket_message (socket_message &&other) noexcept
 Move constructor.
 
auto operator= (socket_message &&other) noexcept -> socket_message &
 Move assignment operator.
 
auto get () const -> message_data
 Gets the underlying message_data struct.
 
auto operator= (message_data data) -> socket_message &
 Assign message_data to socket_message.
 
auto address () const -> socket_address
 Gets the socket address.
 
auto set_address (socket_address address) -> socket_message &
 Sets the socket address.
 
auto exchange_address (socket_address address) -> socket_address
 Exchanges the socket address.
 
auto buffers () const -> scatter_gather_buffer
 Gets the data buffers.
 
auto set_buffers (scatter_gather_buffer buffers) -> socket_message &
 Sets the data buffers.
 
auto exchange_buffers (scatter_gather_buffer buffers) -> scatter_gather_buffer
 Exchanges the data buffers.
 
auto push_back (socket_buffer_type buffer) -> socket_message &
 Adds a new data buffer to the end of the buffer collection.
 
template<typename... Args>
auto emplace_back (Args &&...args) -> socket_message &
 Constructs a new buffer in-place at the end of the buffer collection.
 
auto control () const -> ancillary_buffer
 Gets the ancillary data.
 
auto set_control (ancillary_buffer control) -> socket_message &
 Sets the ancillary data.
 
auto exchange_control (ancillary_buffer control) -> ancillary_buffer
 Exchanges the ancillary data.
 
auto flags () const -> int
 Gets the message flags.
 
auto set_flags (int flags) -> socket_message &
 Sets the message flags.
 
auto exchange_flags (int flags) -> int
 Exchanges the message flags.
 
 ~socket_message ()=default
 Default destructor.
 

Friends

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

Detailed Description

A thread-safe container for socket messages used in advanced I/O operations.

The socket_message class provides a thread-safe wrapper around the socket message data, supporting scatter-gather I/O operations with ancillary data and control information. This class is designed for use with the sendmsg() and recvmsg() system calls, which require complex message structures.

Key features:

Thread Safety
All operations on a socket_message instance are thread-safe due to internal mutex protection. Multiple threads can safely access different socket_message instances concurrently. Concurrent access to the same instance is serialized.
Note
This class uses eager initialization, meaning the internal data structure is created immediately in the constructor.
Warning
Copy operations are explicitly deleted to prevent ambiguity in resource ownership. Use move semantics instead.

Definition at line 110 of file socket_message.hpp.

Constructor & Destructor Documentation

◆ socket_message() [1/3]

io::socket::socket_message::socket_message ( )

Default constructor.

Creates an empty socket message with initialized internal data.

Definition at line 20 of file socket_message.cpp.

◆ socket_message() [2/3]

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

Deleted copy constructor.

Copying is disallowed to enforce move-only semantics and prevent resource ownership issues.

◆ socket_message() [3/3]

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

Move constructor.

Transfers ownership of the socket message data from another instance, leaving the source instance in a valid but unspecified state.

Parameters
otherThe socket_message to move from.

Definition at line 22 of file socket_message.cpp.

◆ ~socket_message()

io::socket::socket_message::~socket_message ( )
default

Default destructor.

Cleans up internal resources. The unique_ptr ensures that the message_data structure is properly deallocated.

Member Function Documentation

◆ address()

auto io::socket::socket_message::address ( ) const -> socket_address

Gets the socket address.

Returns
A copy of the socket address associated with this message.

Definition at line 55 of file socket_message.cpp.

◆ buffers()

auto io::socket::socket_message::buffers ( ) const -> scatter_gather_buffer

Gets the data buffers.

These buffers are used for vectored I/O operations that can read or write multiple non-contiguous memory regions in a single system call.

Returns
A copy of the scatter-gather buffer collection.

Definition at line 74 of file socket_message.cpp.

◆ control()

auto io::socket::socket_message::control ( ) const -> ancillary_buffer

Gets the ancillary data.

This data contains control information that can be passed alongside the main message data, such as file descriptors, credentials, or other metadata.

Returns
A copy of the ancillary data.

Definition at line 100 of file socket_message.cpp.

◆ emplace_back()

template<typename... Args>
auto io::socket::socket_message::emplace_back ( Args &&...  args) -> socket_message &
inline

Constructs a new buffer in-place at the end of the buffer collection.

This method constructs a new socket_buffer_type object directly in the buffer collection using the provided arguments, avoiding unnecessary copies or moves. This is more efficient than push_back when the buffer can be constructed from the arguments directly.

Template Parameters
ArgsThe types of arguments to forward to the buffer constructor.
Parameters
argsThe arguments to forward to construct the new buffer.
Returns
A reference to this socket_message for method chaining.
Note
This operation is thread-safe due to internal mutex protection.

Definition at line 265 of file socket_message.hpp.

◆ exchange_address()

auto io::socket::socket_message::exchange_address ( socket_address  address) -> socket_address

Exchanges the socket address.

Parameters
addressThe socket address to set.
Returns
The previous address that was replaced.

Definition at line 66 of file socket_message.cpp.

◆ exchange_buffers()

auto io::socket::socket_message::exchange_buffers ( scatter_gather_buffer  buffers) -> scatter_gather_buffer

Exchanges the data buffers.

The provided buffers are moved into the message, transferring ownership. The previous buffers are returned to the caller.

Parameters
buffersThe buffer collection to set (will be moved).
Returns
The previous buffer collection that was replaced.

Definition at line 86 of file socket_message.cpp.

◆ exchange_control()

auto io::socket::socket_message::exchange_control ( ancillary_buffer  control) -> ancillary_buffer

Exchanges the ancillary data.

The provided control data is moved into the message, transferring ownership. The previous control data is returned to the caller.

Parameters
controlThe ancillary data to set (will be moved).
Returns
The previous ancillary data that was replaced.

Definition at line 111 of file socket_message.cpp.

◆ exchange_flags()

auto io::socket::socket_message::exchange_flags ( int  flags) -> int

Exchanges the message flags.

The provided flags are set in the message, replacing the current flags. The previous flags are returned to the caller.

Parameters
flagsThe message flags to set.
Returns
The previous message flags that were replaced.

Definition at line 130 of file socket_message.cpp.

◆ flags()

auto io::socket::socket_message::flags ( ) const -> int

Gets the message flags.

These flags control the behavior of socket operations and correspond to the flags parameter used with the sendmsg() and recvmsg() system calls.

Returns
The message flags.

Definition at line 119 of file socket_message.cpp.

◆ get()

auto io::socket::socket_message::get ( ) const -> message_data

Gets the underlying message_data struct.

Definition at line 43 of file socket_message.cpp.

◆ operator=() [1/3]

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

Deleted copy assignment operator.

Copying is disallowed to enforce move-only semantics and prevent resource ownership issues.

◆ operator=() [2/3]

auto io::socket::socket_message::operator= ( message_data  data) -> socket_message &

Assign message_data to socket_message.

Definition at line 49 of file socket_message.cpp.

◆ operator=() [3/3]

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

Move assignment operator.

Transfers ownership of the socket message data from another instance, leaving the source instance in a valid but unspecified state.

Parameters
otherThe socket_message to move from.
Returns
A reference to this instance.

Definition at line 27 of file socket_message.cpp.

◆ push_back()

auto io::socket::socket_message::push_back ( socket_buffer_type  buffer) -> socket_message &

Adds a new data buffer to the end of the buffer collection.

This method appends the provided buffer to the end of the scatter-gather buffer collection. The operation is thread-safe and the buffer is copied into the collection.

Parameters
bufferThe buffer to add to the collection.
Returns
A reference to this socket_message for method chaining.
Note
This operation is thread-safe due to internal mutex protection.

Definition at line 94 of file socket_message.cpp.

◆ set_address()

auto io::socket::socket_message::set_address ( socket_address  address) -> socket_message &

Sets the socket address.

Parameters
addressThe socket address to set.
Returns
A reference to this socket_message for method chaining.

Definition at line 60 of file socket_message.cpp.

◆ set_buffers()

auto io::socket::socket_message::set_buffers ( scatter_gather_buffer  buffers) -> socket_message &

Sets the data buffers.

The provided buffers are moved into the message, transferring ownership.

Parameters
buffersThe buffer collection to set (will be moved).
Returns
A reference to this socket_message for method chaining.

Definition at line 79 of file socket_message.cpp.

◆ set_control()

auto io::socket::socket_message::set_control ( ancillary_buffer  control) -> socket_message &

Sets the ancillary data.

The provided data is moved into the message, transferring ownership.

Parameters
controlThe ancillary data to set (will be moved).
Returns
A reference to this socket_message for method chaining.

Definition at line 105 of file socket_message.cpp.

◆ set_flags()

auto io::socket::socket_message::set_flags ( int  flags) -> socket_message &

Sets the message flags.

These flags control the behavior of socket operations and correspond to the flags parameter used with the sendmsg() and recvmsg() system calls (e.g., MSG_DONTWAIT, MSG_PEEK, MSG_TRUNC).

Parameters
flagsThe message flags to set.
Returns
A reference to this socket_message for method chaining.

Definition at line 124 of file socket_message.cpp.

Friends And Related Symbol Documentation

◆ swap

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

Swaps the contents of two socket_message instances.

This function atomically swaps the contents of two socket_message instances using a dual-mutex lock to prevent deadlocks.

Parameters
lhsThe first socket_message instance.
rhsThe second socket_message instance.
Note
This function uses std::scoped_lock to prevent deadlocks when locking multiple mutexes.

Definition at line 33 of file socket_message.cpp.


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