void display_fd_set( const char *msg, fd_set *set, int max, bool try_dup ) { int i, count; dprintf( D_ALWAYS, "%s {", msg ); for( i=0, count=0; i<=max; i++ ) { if( MY_FD_ISSET(i,set) ) { count++; dprintf( D_ALWAYS | D_NOHEADER, "%d", i ); if( try_dup ) { # if defined( UNIX ) int newfd = dup( i ); if ( newfd >= 0 ) { close( newfd ); } else if ( EBADF == errno ) { dprintf( D_ALWAYS | D_NOHEADER, "<EBADF> " ); } else { dprintf( D_ALWAYS | D_NOHEADER, "<%d> ", errno ); } # endif } dprintf( D_ALWAYS | D_NOHEADER, " " ); } } dprintf( D_ALWAYS | D_NOHEADER, "} = %d\n", count ); }
unsigned long do_select() { int max = -1, i, rc; struct timeval ts; struct timeval start, end; int fd; MY_FD_ZERO(rdset, rdset_nbytes); for(i = 0; i < ncur - 1; i++) { fd = fds[i]; MY_FD_SET(fd, rdset); } fd = fds[nceil - 1]; MY_FD_SET(fd, rdset); max = fds[nceil - 1] + 1; ts.tv_sec = TIMEOUT_SEC; ts.tv_usec = 0; gettimeofday(&start, NULL); rc = select(max, (fd_set *)rdset, NULL, NULL, &ts); gettimeofday(&end, NULL); switch(rc) { case -1: fprintf(stderr, "select failed: %s\n", strerror(errno)); break; case 0: fprintf(stderr, "select timed out\n"); break; default: //fprintf(stderr, "%d fds are ready for reading.\n", rc); for(i = 0; i < ncur - 1; i++) { fd = fds[i]; if(MY_FD_ISSET(fd, rdset)) { read(fds[i], buf, READ_SIZE); } } fd = fds[nceil - 1]; if(MY_FD_ISSET(fd, rdset)) { read(fds[nceil - 1], buf, READ_SIZE); } break; } return (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); }
bool Selector::fd_ready( int fd, IO_FUNC interest ) { if( state != FDS_READY && state != TIMED_OUT ) { EXCEPT( "Selector::fd_ready() called, but selector not in FDS_READY state" ); } #if !defined(WIN32) // on UNIX, make sure the value of fd makes sense // if ( fd < 0 || fd >= fd_select_size() ) { return false; } #endif switch( interest ) { case IO_READ: return (SINGLE_SHOT_OK == m_single_shot) ? (m_poll.revents & (POLLIN|POLLHUP)) : MY_FD_ISSET( fd, read_fds ); break; case IO_WRITE: return (SINGLE_SHOT_OK == m_single_shot) ? (m_poll.revents & (POLLOUT|POLLHUP)) : MY_FD_ISSET( fd, write_fds ); break; case IO_EXCEPT: return (SINGLE_SHOT_OK == m_single_shot) ? (m_poll.revents & POLLERR) : MY_FD_ISSET( fd, except_fds ); break; } // Can never get here return false; }
void SelectSetRemove(SelectSetPtr const ssp, const int fd) { #if defined(__DECC) || defined(__DECCXX) #pragma message save #pragma message disable trunclongint #endif if ((fd >= 0) && (MY_FD_ISSET(fd, &ssp->fds))) { MY_FD_CLR(fd, &ssp->fds); /* Note that maxfd is left alone, even if maxfd was * this one. That is okay. */ --ssp->numfds; } #if defined(__DECC) || defined(__DECCXX) #pragma message restore #endif } /* SelectSetRemove */
void tcpip_task( void *dummy) { /* wait for an IO signal, find out what is happening and * call the right routine to handle the situation. */ fd_set rfds, *pfds; #ifndef __linux__ fd_set efds; #endif int conn_id, ret, count; #ifndef WIN32 int data; #endif if(dummy){} while(1) { while(!DIM_IO_valid) dim_usleep(1000); list_to_fds( &rfds ); MY_FD_ZERO(&efds); #ifdef WIN32 pfds = &efds; #else pfds = &rfds; #endif MY_FD_SET( DIM_IO_path[0], pfds ); #ifdef __linux__ ret = poll(Pollfds, Pollfd_size, -1); #else ret = select(FD_SETSIZE, &rfds, NULL, &efds, NULL); #endif if(ret <= 0) { printf("poll returned %d, errno %d\n", ret, errno); } if(ret > 0) { if(MY_FD_ISSET(DIM_IO_path[0], pfds) ) { #ifndef WIN32 read(DIM_IO_path[0], &data, 4); DIM_IO_Done = 0; #endif MY_FD_CLR( (unsigned)DIM_IO_path[0], pfds ); } /* { DISABLE_AST */ conn_id = 0; while( (ret = fds_get_entry( &rfds, &conn_id )) > 0 ) { if( Net_conns[conn_id].reading ) { count = 0; do { DISABLE_AST if(Net_conns[conn_id].channel) { do_read( conn_id ); count = get_bytes_to_read(conn_id); } else { count = 0; } ENABLE_AST }while(count > 0 ); } else { DISABLE_AST do_accept( conn_id ); ENABLE_AST } MY_FD_CLR( (unsigned)Net_conns[conn_id].channel, &rfds ); } /* ENABLE_AST } */ #ifndef WIN32 return; #endif }