+bool
+NetChannelPoller::poll(unsigned int timeout)
+{
+ if (channels.empty()) {
+ return false;
+ }
+
+ enum { MAX_SOCKETS = 256 } ;
+ Socket* reads [ MAX_SOCKETS+1 ] ;
+ Socket* writes [ MAX_SOCKETS+1 ] ;
+ int nreads = 0 ;
+ int nwrites = 0 ;
+ int nopen = 0 ;
+
+ ChannelList::iterator it = channels.begin();
+ while( it != channels.end() )
+ {
+ NetChannel* ch = *it;
+ if ( ch -> should_delete )
+ {
+ // avoid the channel trying to remove itself from us, or we get
+ // bug http://code.google.com/p/flightgear-bugs/issues/detail?id=1144
+ ch->poller = NULL;
+ delete ch;
+ it = channels.erase(it);
+ continue;
+ }
+
+ ++it; // we've copied the pointer into ch
+ if ( ch->closed ) {
+ continue;
+ }
+
+ if (ch -> resolving_host )
+ {
+ ch -> handleResolve();
+ continue;
+ }
+
+ nopen++ ;
+ if (ch -> readable()) {
+ assert(nreads<MAX_SOCKETS);
+ reads[nreads++] = ch ;
+ }
+ if (ch -> writable()) {
+ assert(nwrites<MAX_SOCKETS);
+ writes[nwrites++] = ch ;
+ }
+ } // of array-filling pass
+
+ reads[nreads] = NULL ;
+ writes[nwrites] = NULL ;
+
+ if (!nopen)
+ return false ;
+ if (!nreads && !nwrites)
+ return true ; //hmmm- should we shutdown?
+
+ Socket::select (reads, writes, timeout) ;
+
+ for ( int i=0; reads[i]; i++ )
+ {
+ NetChannel* ch = (NetChannel*)reads[i];
+ if ( ! ch -> closed )
+ ch -> handleReadEvent();
+ }