title: Writing sections of data to TCP socket with boost::asio::ip::tcp from streambuf
body: <p>Note for those down-voting: this question is not about the async aspects of asio (although perhaps an async solution might make sense here, which is a question I do offer up at the end). This is actually just about the use of streambuf and ostreams with the asio tcp socket wrapper. The examples/tutorial do not cover this particular aspect (sub-dividing a write call).</p>
  
  <p>I'm writing some (hopefully simple) code for a plugin environment that needs to send a fairly large chunk of data (~2MB) to an external server in response to some event. The data needs to be sent fairly promptly, and completely, but it's infrequent and I'm not overly concerned about raw performance. I'm using Google's Protocol Buffers to serialize the data.</p>
  
  <p>As of right now, I have the following code that almost works:</p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  
  // connect to the server:
  boost::asio::io_service io_service;
  tcp::resolver resolver(io_service);
  tcp::resolver::query query(server_address, server_port);
  tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
  tcp::socket socket(io_service);
  boost::asio::connect(socket, endpoint_iterator);
  
  // float_array consists of ~500,000 floats in a ProtoBuf message:
  //
  // message FloatArray {
  //   repeated float data = 1 [packed=true];
  // }
  
  // send the serialized float_array to the server:
  boost::asio::streambuf b;
  std::ostream os(&amp;b);
  float_array.SerializeToOstream(&amp;os);
  boost::asio::write(socket, b);
  
  // the TCP connection *must* now close to signal the server
  </code></pre>
  
  <p>The problem with this is that the environment I'm working within (which is multithreaded) sees the <code>write()</code> operation as taking too long (it blocks), and terminates the thread. As I am not permitted to create my own threads, I need to split up the write() operation into a number of separate writes.</p>
  
  <p>My concern is how to do this. I know that I can use this to send an exact number of bytes:</p>
  
  <pre><code>boost::asio::write(socket, b, boost::asio::transfer_exactly(65536));
  </code></pre>
  
  <p>But to this correctly I need to know exactly how many bytes remain in the ostream. I notice that <code>b.size()</code> reduces accordingly so I could use this. However in order to split my writes up, I need to store some state between calls to this function.</p>
  
  <p>One option I have is to store both the streambuf b and the ostream os between calls to my writing function, but I'm wondering if there is a better way to do this. As far as I can tell it's not possible to partially serialize the ProtoBuf output, so I think I'm stuck with the one call to <code>float_array.SerializeToOstream()</code>. The question then is whether there's a proper way to query the ostream directly for the number of available bytes, or possibly to make use of some other mechanism (<code>boost::asio::buffer</code> maybe?).</p>
  
  <p>I'm happy to review the boost::asio documentation myself, I'm just looking for a little guidance on how to proceed, please, as there's a lot of documentation to work through and I'm not sure which parts of the puzzle are relevant.</p>
  
  <p>A thought - is it possible to use boost::asio to create some sort of single-threaded asynchronous "sender" along these lines that can handle this sort of state for me? For example, can I call some sort of non-blocking <code>write()</code> function and then have some sort of callback (or frequently visited function) that checks for completion and then closes the TCP connection?</p>