title: Where to implement a protocol using boost::asio?
body: <p>I am trying to implement a simple serial port protocol. It goes like this:</p>
  
  <ol>
  <li>discard all data until <code>0xff</code> is received</li>
  <li>read header (node address and data length, 4 bytes)</li>
  <li>read data (max. 64 bytes)</li>
  <li>read crc</li>
  <li>process the received packet</li>
  <li>send response</li>
  <li>when <code>0xff</code> is seen, even when not expected like in the middle of the data, it means a new packet is received</li>
  </ol>
  
  <p>I can implement this using <code>boost::asio::serial_port</code> with <code>boost::asio::read()</code> reading a single byte and processing this byte when it is received. Although this works, I was wondering if there is a more 'boost' like way to do this?</p>
  
  <p>I looked at <code>boost::asio::read_until()</code> for reading until <code>0xff</code>, but then I don't know how to discard the data. Storing the data in a buffer and then not using the buffer seems a bit wasteful.</p>
  
  <p>I can use <code>boost::asio::read_until()</code> for reading until the end of the packet but then the <code>MatchCondition</code> needs to have access to (the header of the packet in) the buffer. It seems the <code>MatchCondition</code> only gets an iterator to the first and last byte recently received.</p>
  
  <p>Also, the data received using <code>boost::asio::read()</code> ends up in a <code>stream_buf</code> and I have to parse the received data into a <code>Packet</code> object. I can do this parsing inside <code>Packet</code>, in a separate <code>ParsePacket</code> object or somehow integrate it with <code>boost::asio</code> (something like <code>boost::asio::read(serial, myPacket);</code> where <code>myPacket</code> is a <code>Packet</code> object)</p>
  
  <p>When <code>0xff</code> is seen anywhere in the received data, it means a new packet is starting. So when <code>0xff</code> is received, it must forget any previous received data and start receiving a new packet.</p>
  
  <p>I am planning on using asynchronous operations and adding timeouts.</p>
  
  <p>So, my question is: where to implement a protocol like this? Or more generally, where to implement a protocol using <code>boost::asio</code>. I am not looking for working code but something like advise on where to implement the protocol and which <code>boost::asio</code> functionality to use.</p>
  
  <p>update:</p>
  
  <p>There is no flow control (hardware or software) used in this case. </p>