예제 #1
0
 engine::want operator()(engine& eng,
     asio::error_code& ec,
     std::size_t& bytes_transferred) const
 {
   bytes_transferred = 0;
   return eng.handshake(type_, ec);
 }
  engine::want operator()(engine& eng,
      asio::error_code& ec,
      std::size_t& bytes_transferred) const
  {
    typename ConstBufferSequence::const_iterator iter = buffers_.begin();
    typename ConstBufferSequence::const_iterator end = buffers_.end();
    std::size_t accumulated_size = 0;

    for (;;)
    {
      engine::want want = eng.handshake(type_, ec);
      if (want != engine::want_input_and_retry
          || bytes_transferred == total_buffer_size_)
        return want;

      // Find the next buffer piece to be fed to the engine.
      while (iter != end)
      {
        const_buffer buffer(*iter);

        // Skip over any buffers which have already been consumed by the engine.
        if (bytes_transferred >= accumulated_size + buffer_size(buffer))
        {
          accumulated_size += buffer_size(buffer);
          ++iter;
          continue;
        }

        // The current buffer may have been partially consumed by the engine on
        // a previous iteration. If so, adjust the buffer to point to the
        // unused portion.
        if (bytes_transferred > accumulated_size)
          buffer = buffer + (bytes_transferred - accumulated_size);

        // Pass the buffer to the engine, and update the bytes transferred to
        // reflect the total number of bytes consumed so far.
        bytes_transferred += buffer_size(buffer);
        buffer = eng.put_input(buffer);
        bytes_transferred -= buffer_size(buffer);
        break;
      }
    }
  }