Is there an existing function to test the condition "are any bits set in a fd_set"?
If possible, I'd like to test whether any bits are set in a fd_set, as opposed to testing whether a specific fd is set, with FD_ISSET()
. I'm trying to code something along the lines of (pseudocode):
...
select( max_fd + 1, &readfds, &writefds, NULL, NULL );
if ( FD_ISSET( specific_read_fd, &readfds ) )
{
handleSpecificReadFdSet();
}
else if ( FD_IS_ANY_SET( &readfds ) ) // Desired functionality
{
handleOtherReadFdSet();
}
else if ( FD_ISSET( specific_write_fd, &writefds ) )
{
handleSpecificWriteFdSet();
}
else // if ( FD_IS_ANY_SET( &writefds ) )
{
handleOtherWriteFdSet()
}
...
I.e. in response to select()
becoming unblocked, I want separate handling for four conditions:
1) Whether a specific fd was set in the read fds
2) Whether any fd other than the specific read fd was set in the read fds
3) Whether a specific fd was set in the write fds
4) Whether any fd other than the specific write fds was set in the write fds
Is there an existing function that provides such a "is any fd in this fds set?" functionality? Or is the only way to do this to use FD_ISSET in a loop, e.g.:
...
bool ret_val = false;
for ( int i = 0; i < max_fd; ++i )
{
if ( i == specific_read_fd ) continue;
if ( FD_ISSET( i, &readfds ) ) { ret_val = true; break; }
}
return ret_val;
...
I'm open to alternatives to solving this problem other than specifically a "FD_IS_ANY_SET()
" function - I'm not super experienced with select()
.
This function initializes the file descriptor set to contain no file descriptors.
The fd_set structure is used by various Windows Sockets functions and service providers, such as the select function, to place sockets into a "set" for various purposes, such as testing a given socket for readability using the readfds parameter of the select function.
The select() function tests file descriptors in the range of 0 to nfds-1. If the readfds argument is not a null pointer, it points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to read, and on output indicates which file descriptors are ready to read.
On success, select() and pselect() return the number of file descriptors contained in the three returned descriptor sets (that is, the total number of bits that are set in readfds, writefds, exceptfds).
Here's how you might test whether or not an fd_set is empty:
bool FD_IS_ANY_SET(fd_set const *fdset)
{
static fd_set empty; // initialized to 0 -> empty
return memcmp(fdset, &empty, sizeof(fd_set)) != 0;
}
The function FD_IS_ANY_SET
returns true
if *fdset
contains at least one file descriptor, otherwise false
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With