Show
Ignore:
Timestamp:
10/06/05 14:28:01 (3 years ago)
Author:
ross
Message:

added code to implement UdpSocket::AsynchronousBreak?() on posix systems

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • oscpack/trunk/ip/posix/UdpSocket.cpp

    r38 r39  
    302302 
    303303  volatile bool break_; 
     304  int breakPipe_[2]; // [0] is the reader descriptor and [1] the writer 
    304305 
    305306  double GetCurrentTimeMs() const 
     
    315316    Implementation() 
    316317  { 
     318    if( pipe(breakPipe_) != 0 ) 
     319      throw std::runtime_error( "creation of asynchronous break pipes failed\n" ); 
    317320  } 
    318321 
    319322    ~Implementation() 
    320323  { 
     324    close( breakPipe_[0] ); 
     325    close( breakPipe_[1] ); 
    321326  } 
    322327 
     
    370375    FD_ZERO( &masterfds ); 
    371376    FD_ZERO( &tempfds ); 
    372     int fdmax = 0; 
     377     
     378    // in addition to listening to the inbound sockets we 
     379    // also listen to the asynchronous break pipe, so that AsynchronousBreak() 
     380    // can break us out of select() from another thread. 
     381    FD_SET( breakPipe_[0], &masterfds ); 
     382    int fdmax = breakPipe_[0];     
    373383 
    374384    for( std::vector< std::pair< PacketListener*, UdpSocket* > >::iterator i = socketListeners_.begin(); 
     
    406416          timeoutMs = 0; 
    407417       
    408                 // 1000000 microseconds in a second 
    409                 timeout.tv_sec = (long)(timeoutMs * .001); 
    410                 timeout.tv_usec = (long)((timeoutMs - (timeout.tv_sec * 1000)) * 1000); 
     418       // 1000000 microseconds in a second 
     419       timeout.tv_sec = (long)(timeoutMs * .001); 
     420       timeout.tv_usec = (long)((timeoutMs - (timeout.tv_sec * 1000)) * 1000); 
    411421        timeoutPtr = &timeout; 
    412422      } 
     
    415425          throw std::runtime_error("select failed\n"); 
    416426      } 
     427 
     428      if ( FD_ISSET( breakPipe_[0], &tempfds ) ){ 
     429        // clear pending data from the asynchronous break pipe 
     430        char c; 
     431        read( breakPipe_[0], &c, 1 ); 
     432      } 
     433       
    417434      if( break_ ) 
    418435        break; 
     
    460477  { 
    461478    break_ = true; 
    462     // FIXME: need to do something here to break through the select, not sure what 
    463     // for now single threaded apps will work fine because ctrl-c will cause 
    464     // our select call to fall out with an E_INTR or something 
     479 
     480    // Send a termination message to the asynchronous break pipe, so select() will return 
     481    write( breakPipe_[1], "!", 1 ); 
    465482  } 
    466483};