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()
.
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
.