title: Boost::asio and async_read
body: <p>I am trying to use Boost::asio and async_read to implement a timeout in protocol which uses the serial port.</p>
  
  <p>I have done a test implementation using synchronous read, which works just find, but my implementation with time out does not work.</p>
  
  <pre><code>void set_result( boost::optional&lt;boost::system::error_code&gt; * a, boost::system::error_code b ) {
      a-&gt;reset( b );
  }
  
  void receive(boost::asio::io_service &amp; io, boost::asio::serial_port &amp; cctalk_port, boost::asio::streambuf &amp; result){
  #if 1
      // Non-working implementation with timeout
      boost::optional&lt;boost::system::error_code&gt; timer_result;
      boost::optional&lt;boost::system::error_code&gt; read_result;
  
      boost::asio::deadline_timer timer( io );
      LOG(INFO) &lt;&lt; "Timer at 5000ms starts here";
      timer.expires_from_now( boost::posix_time::milliseconds(5000) ); // allow up to 50ms of timeout for every char
      timer.async_wait( boost::bind(&amp;set_result, &amp;timer_result, _1) );
  
      LOG(INFO) &lt;&lt; "Async read starts here (result.size() == " &lt;&lt; result.size() &lt;&lt; ")";
      boost::asio::async_read(
              cctalk_port,
              result,
              boost::asio::transfer_at_least(1),
              boost::bind( &amp;set_result, &amp;read_result, _1 ));
      boost::system::error_code ec;
  
      while(1) {
          io.poll_one(ec);
  
          if( ec != 0 || read_result != 0 || timer_result != 0)
              LOG(INFO) &lt;&lt; "Error code: " &lt;&lt; ec &lt;&lt; " read_result: " &lt;&lt; read_result &lt;&lt; " timer_result: " &lt;&lt; timer_result;
  
          if ( read_result ) {
              timer.cancel();
              LOG(INFO) &lt;&lt; "Result ready (" &lt;&lt; result.size() &lt;&lt; ")";
              return;
  
          } else if ( timer_result ) {
              LOG(INFO) &lt;&lt; "Timeout";
              throw runtime_error("timeout");
          }
      }
  
      LOG(INFO) &lt;&lt; "Not suppose to happen";
  
  #else
      // Working implementation without timeout
      boost::asio::read(cctalk_port, result, boost::asio::transfer_at_least(1));
  #endif
  }
  
  void receive(boost::asio::io_service &amp; io, boost::asio::serial_port &amp; cctalk_port, size_t size, boost::asio::streambuf &amp; result){
      LOG(INFO) &lt;&lt; "Fetch at least " &lt;&lt; size &lt;&lt; " has allready: " &lt;&lt; result.size();
      while( result.size() &lt; size ) {
          receive(io, cctalk_port, result);
          LOG(INFO) &lt;&lt; "Buffer size: " &lt;&lt; result.size() &lt;&lt; "/" &lt;&lt; size;
      }
  }
  </code></pre>
  
  <p>When I run this code I got the following outputs:</p>
  
  <pre><code>I0808 17:25:40.809615  3682 ccTalkScan.cxx:137] Fetch at least 5 has allready: 0
  I0808 17:25:40.809672  3682 ccTalkScan.cxx:99] Timer at 5000ms starts here
  I0808 17:25:40.809686  3682 ccTalkScan.cxx:103] Async read starts here (result.size() == 0)
  I0808 17:25:40.809731  3682 ccTalkScan.cxx:115] Error code: system:0 read_result: 1 timer_result: 0
  I0808 17:25:40.809738  3682 ccTalkScan.cxx:119] Result ready (12)
  I0808 17:25:40.809742  3682 ccTalkScan.cxx:140] Buffer size: 12/5
  I0808 17:25:40.809778  3682 ccTalkScan.cxx:137] Fetch at least 5 has allready: 7
  I0808 17:25:40.809783  3682 ccTalkScan.cxx:137] Fetch at least 9 has allready: 7
  I0808 17:25:40.809788  3682 ccTalkScan.cxx:99] Timer at 5000ms starts here
  I0808 17:25:40.809797  3682 ccTalkScan.cxx:103] Async read starts here (result.size() == 7)
  I0808 17:25:40.809808  3682 ccTalkScan.cxx:115] Error code: system:0 read_result: 0 timer_result: 1
  I0808 17:25:40.809811  3682 ccTalkScan.cxx:123] Timeout
  </code></pre>
  
  <p>As it appears from the output, the timer is set must faster than 5000ms, in this case less than 1 ms.</p>
  
  <p>Further more, by looking at the actual bytes I does receives it seems like I am missing the first byte.</p>