randolf.ca  1.00
Randolf's C++ classes
randolf::rsocket_mux Class Reference

The rsocket multiplexer extends the functionality of the rsocket class primarily for handling multiple sockets that are beyond the scope of the single-socket-focused nature of rsocket. More...

+ Collaboration diagram for randolf::rsocket_mux:

Public Member Functions

 rsocket_mux () noexcept
 Instantiate an rsocket multiplexer with no rsocket objects. More...
 
template<class R , class... Rs>
 rsocket_mux (const int fd_sets, R r, Rs... rs) noexcept
 Instantiate an rsocket multiplexer with any number of rsocket objects. More...
 
 rsocket_mux (const int fd_sets, rsocket *r) noexcept
 Instantiate an rsocket multiplexer with one rsocket object. More...
 
void erase (const int fd_sets, rsocket *r) noexcept
 Remove an rsocket object from the underlying std::set. More...
 
void erase (rsocket_mux_fds rmf)
 Remove an rsocket object from the underlying std::set. More...
 
template<class R , class... Rs>
void insert (const int fd_sets, R r, Rs... rs)
 Add one or more rsocket objects to the underlying std::set. More...
 
void insert (const int fd_sets, rsocket *r)
 Add one rsocket object to the underlying std::set. More...
 
std::vector< rsocket_mux_fdspoll (const int timeout=0, const bool timeout_behaviour=rsocket::TIMEOUT_EXCEPTION)
 Use the poll() method on the internal set of instantiated rsocket objects. More...
 
std::vector< rsocket_mux_fdsppoll (const struct timespec *tmo_p=nullptr, const sigset_t *sigmask=nullptr, const bool timeout_behaviour=rsocket::TIMEOUT_EXCEPTION)
 Use the ppoll() method on the internal set of instantiated rsocket objects. More...
 
std::vector< rsocket_mux_fdspselect (const struct timespec *tmo_p=nullptr, const sigset_t *sigmask=nullptr, const bool timeout_behaviour=rsocket::TIMEOUT_EXCEPTION)
 Use the pselect() method on the internal set of instantiated rsocket objects. More...
 
std::vector< rsocket_mux_fdsselect (struct timeval *tv=nullptr, const bool timeout_behaviour=rsocket::TIMEOUT_EXCEPTION)
 Use the select() method on the internal set of instantiated rsocket objects. More...
 
size_t size () noexcept
 Find out how many rsocket entries are in this mux. More...
 

Detailed Description

The rsocket multiplexer extends the functionality of the rsocket class primarily for handling multiple sockets that are beyond the scope of the single-socket-focused nature of rsocket.

Functionality of select(), poll(), and related socket functions are implemented within this separate rsocket_mux class partly because these specializations are beyond the scope of what the rsocket class is intended for.

Threads
This class is threadsafe.
Author
Randolf Richardson
Version
1.00
History
2022-Dec-24 v1.00 Initial version

Constructor & Destructor Documentation

◆ rsocket_mux() [1/3]

randolf::rsocket_mux::rsocket_mux ( )
inlinenoexcept

Instantiate an rsocket multiplexer with no rsocket objects.

See also
insert()

◆ rsocket_mux() [2/3]

randolf::rsocket_mux::rsocket_mux ( const int  fd_sets,
rsocket r 
)
inlinenoexcept

Instantiate an rsocket multiplexer with one rsocket object.

The following fd_sets correspond with each respective POSIX select() fd_set, which can be combined to include the specified rsocket in multiple fd_sets:

  • POLLIN = readfds
  • POLLOUT = writefds
  • POLLERR = exceptfds
See also
insert()
Parameters
fd_setsMust be at least one of POLLIN, POLLOUT, and POLLERR
rPointer to instantiated rsocket object

◆ rsocket_mux() [3/3]

template<class R , class... Rs>
randolf::rsocket_mux::rsocket_mux ( const int  fd_sets,
r,
Rs...  rs 
)
inlinenoexcept

Instantiate an rsocket multiplexer with any number of rsocket objects.

The following fd_sets correspond with each respective POSIX select() fd_set, which can be combined to include the specified rsocket in multiple fd_sets:

  • POLLIN = readfds
  • POLLOUT = writefds
  • POLLERR = exceptfds
See also
insert()
Parameters
fd_setsMust be at least one of POLLIN, POLLOUT, and POLLERR
rPointer to instantiated rsocket object
rsVariadic arguments (any quantity of instantiated rsocket objects)

Member Function Documentation

◆ erase() [1/2]

void randolf::rsocket_mux::erase ( const int  fd_sets,
rsocket r 
)
inlinenoexcept

Remove an rsocket object from the underlying std::set.

Threads
This method is threadsafe.
Parameters
fd_setsMust be at least one of POLLIN, POLLOUT, and POLLERR
rPointer to instantiated rsocket object

◆ erase() [2/2]

void randolf::rsocket_mux::erase ( rsocket_mux_fds  rmf)
inline

Remove an rsocket object from the underlying std::set.

Threads
This method is threadsafe.
Parameters
rmfStructure that is comprised of an rsocket object and its fd_set flags

◆ insert() [1/2]

void randolf::rsocket_mux::insert ( const int  fd_sets,
rsocket r 
)
inline

Add one rsocket object to the underlying std::set.

Threads
This method is threadsafe.
Parameters
fd_setsMust be at least one of POLLIN, POLLOUT, and POLLERR
rPointer to instantiated rsocket object

◆ insert() [2/2]

template<class R , class... Rs>
void randolf::rsocket_mux::insert ( const int  fd_sets,
r,
Rs...  rs 
)
inline

Add one or more rsocket objects to the underlying std::set.

Threads
This method is threadsafe.
Parameters
fd_setsMust be at least one of POLLIN, POLLOUT, and POLLERR
rPointer to instantiated rsocket object
rsVariadic arguments (any quantity of instantiated rsocket objects)

◆ poll()

std::vector<rsocket_mux_fds> randolf::rsocket_mux::poll ( const int  timeout = 0,
const bool  timeout_behaviour = rsocket::TIMEOUT_EXCEPTION 
)
inline

Use the poll() method on the internal set of instantiated rsocket objects.

Threads
This method is not threadsafe. Calls to insert() and erase() during this blocking operation will almost certainly yield unpredictable results.
Exceptions
randolf::rex::xEAGAINFailure to allocate internal system resources
randolf::rex::xEFAULTAddress structure/memory is not in a writable part of the user address space
randolf::rex::xEINTRInterrupted by a signal
randolf::rex::xENOMEMInsufficient memory
randolf::rex::xENOTSOCKUnderlying socket file descriptor (handle) doesn't refer to a socket
randolf::rex::xETIMEDOUTTimeout period elapsed (even if the TIMEOUT_BEHAVIOUR flag is not set to TIMEOUT_EXCEPTION, there is a highly improbable chance that a timeout could still occur if the data is read from any of the sockets in this rsocket_mux collection by another thread before the recv(..., MSG_PEEK) call)
Returns
An std::vector<socket_mux_fds> that holds only those sockets that were selected
Parameters
timeoutNumber of milliseconds to wait
timeout_behaviourTimeout behaviour (see rsocket::TIMEOUT_BEHAVIOUR for details)

◆ ppoll()

std::vector<rsocket_mux_fds> randolf::rsocket_mux::ppoll ( const struct timespec *  tmo_p = nullptr,
const sigset_t *  sigmask = nullptr,
const bool  timeout_behaviour = rsocket::TIMEOUT_EXCEPTION 
)
inline

Use the ppoll() method on the internal set of instantiated rsocket objects.

Threads
This method is not threadsafe. Calls to insert() and erase() during this blocking operation will almost certainly yield unpredictable results.
Exceptions
randolf::rex::xEAGAINFailure to allocate internal system resources
randolf::rex::xEFAULTAddress structure/memory is not in a writable part of the user address space
randolf::rex::xEINTRInterrupted by a signal
randolf::rex::xENOMEMInsufficient memory
randolf::rex::xENOTSOCKUnderlying socket file descriptor (handle) doesn't refer to a socket
randolf::rex::xETIMEDOUTTimeout period elapsed (even if the TIMEOUT_BEHAVIOUR flag is not set to TIMEOUT_EXCEPTION, there is a highly improbable chance that a timeout could still occur if the data is read from any of the sockets in this rsocket_mux collection by another thread before the recv(..., MSG_PEEK) call)
Returns
An std::vector<socket_mux_fds> that holds only those sockets that were selected
Parameters
tmo_pPointer to a timespec structure (will not be updated)
sigmaskSignal mask
timeout_behaviourTimeout behaviour (see rsocket::TIMEOUT_BEHAVIOUR for details)

◆ pselect()

std::vector<rsocket_mux_fds> randolf::rsocket_mux::pselect ( const struct timespec *  tmo_p = nullptr,
const sigset_t *  sigmask = nullptr,
const bool  timeout_behaviour = rsocket::TIMEOUT_EXCEPTION 
)
inline

Use the pselect() method on the internal set of instantiated rsocket objects.

Note
Performance optimization was a major consideration in the design of this method, including only testing the readiness of socket handles that were actually added to the underlying fd_set array(s).

The easier way to use this method is to only add one polling type (such as POLLIN) to the encompassing rsocket_mux object, and then all rsocket_mux_fds records returned will only be for that polling type (and then you won't have to write additional code to confirm the polling type). However, easier doesn't necessarily equate to better, and your needs and use case(s) should normally be considered in determining which approach is the most appropriate to write code for.

Warning
The underlying POSIX pselect() method is not efficient and is known to have a tendency to use a significant amount of stack memory, particularly when monitoring more sockets. The poll() and ppoll() methods serve as improvements to these problems, and are generally known to be better options for monitoring larger numbers of sockets.
Threads
This method is not threadsafe. Calls to insert() and erase() during this blocking operation will almost certainly yield unpredictable results.
Exceptions
randolf::rex::xEAGAINFailure to allocate internal system resources
randolf::rex::xEFAULTAddress structure/memory is not in a writable part of the user address space
randolf::rex::xEINTRInterrupted by a signal
randolf::rex::xENOMEMInsufficient memory
randolf::rex::xENOTSOCKUnderlying socket file descriptor (handle) doesn't refer to a socket
randolf::rex::xETIMEDOUTTimeout period elapsed (even if the TIMEOUT_BEHAVIOUR flag is not set to TIMEOUT_EXCEPTION, there is a highly improbable chance that a timeout could still occur if the data is read from any of the sockets in this rsocket_mux collection by another thread before the recv(..., MSG_PEEK) call)
Returns
An std::vector<socket_mux_fds> that holds only those sockets that were selected
See also
poll()
ppoll()
select()
Parameters
tmo_pPointer to a timespec structure (will not be updated)
sigmaskSignal mask
timeout_behaviourTimeout behaviour (see rsocket::TIMEOUT_BEHAVIOUR for details)

◆ select()

std::vector<rsocket_mux_fds> randolf::rsocket_mux::select ( struct timeval *  tv = nullptr,
const bool  timeout_behaviour = rsocket::TIMEOUT_EXCEPTION 
)
inline

Use the select() method on the internal set of instantiated rsocket objects.

Note
Performance optimization was a major consideration in the design of this method, including only testing the readiness of socket handles that were actually added to the underlying fd_set array(s).

The easier way to use this method is to only add one polling type (such as POLLIN) to the encompassing rsocket_mux object, and then all rsocket_mux_fds records returned will only be for that polling type (and then you won't have to write additional code to confirm the polling type). However, easier doesn't necessarily equate to better, and your needs and use case(s) should normally be considered in determining which approach is the most appropriate to write code for.

Warning
The underlying POSIX select() method is not efficient and is known to have a tendency to use a significant amount of stack memory, particularly when monitoring more sockets. The poll() and ppoll() methods serve as improvements to these problems, and are generally known to be better options for monitoring larger numbers of sockets.
Threads
This method is not threadsafe. Calls to insert() and erase() during this blocking operation will almost certainly yield unpredictable results.
Exceptions
randolf::rex::xEAGAINFailure to allocate internal system resources
randolf::rex::xEFAULTAddress structure/memory is not in a writable part of the user address space
randolf::rex::xEINTRInterrupted by a signal
randolf::rex::xENOMEMInsufficient memory
randolf::rex::xENOTSOCKUnderlying socket file descriptor (handle) doesn't refer to a socket
randolf::rex::xETIMEDOUTTimeout period elapsed (even if the TIMEOUT_BEHAVIOUR flag is not set to TIMEOUT_EXCEPTION, there is a highly improbable chance that a timeout could still occur if the data is read from any of the sockets in this rsocket_mux collection by another thread before the recv(..., MSG_PEEK) call)
Returns
An std::vector<socket_mux_fds> that holds only those sockets that were selected
See also
poll()
ppoll()
pselect()
Parameters
tvPointer to a timeval structure (may also be updated with duration remaining if defined)
timeout_behaviourTimeout behaviour (see rsocket::TIMEOUT_BEHAVIOUR for details)

◆ size()

size_t randolf::rsocket_mux::size ( )
inlinenoexcept

Find out how many rsocket entries are in this mux.

Threads
This method is threadsafe.
Returns
Number of rsocket objects in the underlying std::set

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