/** Enables a frame to be drawn. */
   void SoundManagerSonix::update()
   {
      const vpr::Interval cur_time(vpr::Interval::now());
      const vpr::Interval delta(cur_time - mLastFrameTime);
      float delta_sec(0.0f);

      if ( cur_time > mLastFrameTime )
      {
         delta_sec = delta.secf();

         if ( delta_sec > 1.0f )
         {
            delta_sec = 1.0f;
         }
      }

      mLastFrameTime = cur_time;

      snx::sonix::instance()->step(delta_sec);
   }
vpr::Uint32 SerialPortImplWin32::read_i(void* buffer, const vpr::Uint32 length,
                                        const vpr::Interval& timeout)
{
   unsigned long bytes;

   // Shouldn't be setting this every read, but don't have any other way of
   // specifying the timeout.
   if ( vpr::Interval::NoTimeout != timeout )
   {
      COMMTIMEOUTS t;
      GetCommTimeouts(mHandle, &t);
      t.ReadTotalTimeoutConstant = (int)timeout.msec();
      SetCommTimeouts(mHandle, &t);
   }

   if ( ! ReadFile(mHandle, buffer, length, &bytes, NULL) )
   {
      std::stringstream msg_stream;
      msg_stream << "Failed to read from serial port " << mName << ": "
                 << getErrorMessageWithCode(GetLastError());
      throw IOException(msg_stream.str(), VPR_LOCATION);
   }

   //Now set the timeout back
   if ( vpr::Interval::NoTimeout != timeout )
   {
      COMMTIMEOUTS t;
      GetCommTimeouts(mHandle, &t);
      t.ReadTotalTimeoutConstant = (int)mCurrentTimeout*100;
      SetCommTimeouts(mHandle, &t);
   }

   // XXX: Does reading 0 bytes really indicate a timeout?
   if ( bytes == 0 )
   {
      std::stringstream msg_stream;
      msg_stream << "Timeout while attempting to read from serial port "
                 << mName;
      throw TimeoutException(msg_stream.str(), VPR_LOCATION);
   }

   return bytes;
}
bool FileHandleImplUNIX::isWriteable(const vpr::Interval timeout) const
{
   fd_set write_set;
   int num_events;
   struct timeval timeout_obj;

   if ( mFdesc == -1 )
   {
      throw IOException("Invalid file handle.", VPR_LOCATION);
   }
   else
   {
      if ( timeout == vpr::Interval::NoWait )
      {
         timeout_obj.tv_sec  = 0;
         timeout_obj.tv_usec = 0;
      }
      else
      {
         if ( timeout.msec() >= 1000 )
         {
            timeout_obj.tv_sec  = timeout.msec() / 1000;
            timeout_obj.tv_usec = (timeout.msec() % 1000) * 1000000;
         }
         else
         {
            timeout_obj.tv_sec  = 0;
            timeout_obj.tv_usec = timeout.msec() * 1000;
         }
      }

      FD_ZERO(&write_set);
      FD_SET(mFdesc, &write_set);

      // Watch to see if a write will block. Timeout is an upper bound on the amount of time
      // elapsed before select returns. It may be zero, causing select to return
      // immediately. (This is useful for polling.) If timeout is NULL (no timeout),
      // select can block indefinitely.
      num_events = select(mFdesc + 1, NULL, &write_set, NULL,
                          (timeout != vpr::Interval::NoTimeout) ? &timeout_obj :
                                                                  NULL);

      if ( num_events == 0 )
      {
         // Added in revision 1.33
         // If timeout is vpr::Interval::NoWait(NULL) the file handle may be ready for
         // writing even though select returned 0. This is vauge in the documentation
         // because it says that calling select with a timeout of NULL will return
         // immediately.
         if ( ! FD_ISSET(mFdesc, &write_set) )
         {
            return false;
         }
      }
      else if ( num_events < 0 )
      {
         // TODO: Test additional select errors.
         // EBADF  An invalid file descriptor was given in one of the sets.
         // EINTR  A non blocked signal was caught.
         // EINVAL n is negative or the value contained within timeout is invalid.
         // ENOMEM select was unable to allocate memory for internal tables.
         throw IOException("Error while testing if file handle is writeable.", VPR_LOCATION);
      }
   }
   return true;
}
vpr::Uint32 SocketDatagramImplBOOST::recvfrom(void* msg,
        const vpr::Uint32 length,
        vpr::InetAddr& from,
        const vpr::Interval& timeout)
{
    mBytesRead = 0;

    // NOTE: It appears that binding the address of a stack variable
    // (timer_result) to an asynchronous callback functor works here because
    // the I/O service is being pumped by this method.
    boost::optional<boost::system::error_code> timer_result;
    boost::asio::deadline_timer timer(mUdpSocket->get_io_service());
    timer.expires_from_now(boost::posix_time::microseconds(timeout.msec()));
    timer.async_wait(boost::bind(&SocketDatagramImplBOOST::setResult, this,
                                 &timer_result, _1, -1));

    // Same situation for read_result.
    boost::optional<boost::system::error_code> read_result;
    mUdpSocket->async_receive_from(boost::asio::buffer(msg, length),
                                   from.mUdpAddr,
                                   boost::bind(&SocketDatagramImplBOOST::setResult,
                                           this, &read_result, _1, _2));
    mUdpSocket->get_io_service().reset();
    static bool cancel_supported(true);

    while (mUdpSocket->get_io_service().run_one())
    {
        if (read_result)
        {
            timer.cancel();
        }
        else if (timer_result)
        {
            if (cancel_supported)
            {
                try
                {
                    mUdpSocket->cancel();
                }
                catch (std::exception & ex)
                {
                    vprDEBUG(vprDBG_ALL, vprDBG_CONFIG_STATUS_LVL)
                            << "[SocketDatagramImplBOOST] caught an exception "
                            << "cancelling UDP socket, switching to pre-Vista mode..."
                            << std::endl << vprDEBUG_FLUSH;
                    cancel_supported = false;
                }
            }

            if (! cancel_supported)
            {
                const bool was_bound(isBound());
                mUdpSocket->shutdown(boost::asio::ip::udp::socket::shutdown_both);
                mUdpSocket->close();
                this->close();
                this->open();

                if (was_bound)
                {
                    this->bind();
                }
            }

            throw TimeoutException("recvfrom operation timed out", VPR_LOCATION);
        }
    }

    return mBytesRead;
}