Beispiel #1
0
static int
selfpipe_test( compat_socket_selfpipe_t *self )
{
  fd_set readfds;
  int active;
  struct timeval tv = { 1, 0 };

  /* Send testing packet */
  compat_socket_selfpipe_wake( self );

  /* Safe reading from control socket */
  FD_ZERO( &readfds );
  FD_SET( self->self_socket, &readfds );
  active = select( 0, &readfds, NULL, NULL, &tv );
  if( active == 0 || active == compat_socket_invalid ) {
    return -1;
  }

  /* Discard testing packet */
  if( FD_ISSET( self->self_socket, &readfds ) ) {
    compat_socket_selfpipe_discard_data( self );
  }

  return 0;
}
Beispiel #2
0
static void*
w5100_io_thread( void *arg )
{
  nic_w5100_t *self = arg;
  int i;

  while( !self->stop_io_thread ) {
    fd_set readfds, writefds;
    int active;
    compat_socket_t selfpipe_socket =
      compat_socket_selfpipe_get_read_fd( self->selfpipe );
    int max_fd = selfpipe_socket;

    FD_ZERO( &readfds );
    FD_ZERO( &writefds );

    FD_SET( selfpipe_socket, &readfds );

    for( i = 0; i < 4; i++ )
      nic_w5100_socket_add_to_sets( &self->socket[i], &readfds, &writefds,
        &max_fd );

    /* Note that if a socket is closed between when we added it to the sets
       above and when we call select() below, it will cause the select to fail
       with EBADF. We catch this and just run around the loop again - the
       offending socket will not be added to the sets again as it's now been
       closed */

    nic_w5100_debug( "w5100: io thread select\n" );

    active = select( max_fd + 1, &readfds, &writefds, NULL, NULL );

    nic_w5100_debug( "w5100: io thread wake; %d active\n", active );

    if( active != -1 ) {
      if( FD_ISSET( selfpipe_socket, &readfds ) ) {
        nic_w5100_debug( "w5100: discarding selfpipe data\n" );
        compat_socket_selfpipe_discard_data( self->selfpipe );
      }

      for( i = 0; i < 4; i++ )
        nic_w5100_socket_process_io( &self->socket[i], readfds, writefds );
    }
    else if( compat_socket_get_error() == compat_socket_EBADF ) {
      /* Do nothing - just loop again */
    }
    else {
      nic_w5100_debug( "w5100: select returned unexpected errno %d: %s\n",
                       compat_socket_get_error(),
                       compat_socket_get_strerror() );
    }
  }

  return NULL;
}