title: Large body sent via boost::asio::tcp::ip
body: <p>My protocol is very easy: I sent a <code>size_t</code> indicating the size of body and then the body itself. </p>
  
  <p>The code is based on the <a href="http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/examples.html" rel="nofollow">official Boost examples</a>, here it is:</p>
  
  <pre><code>class tcp_conn : 
          public std::enable_shared_from_this&lt;tcp_conn&gt;, 
          private boost::noncopyable 
      {
      public:
          tcp_conn(ba::io_service&amp; io_service);
          void start();
          void stop();
  
          tcp::socket&amp; socket();
  
      private:
          void handle_read_header(const error_code&amp; e, std::size_t bytes_transferred);
          void handle_read(const error_code&amp; e, std::size_t bytes_transferred);
  
          ba::streambuf::mutable_buffers_type buffer_wrapper_;
          ba::streambuf buffer_;
          std::size_t buffer_size_;
          tcp::socket socket_;
  
  };
  </code></pre>
  
  <p>And the implementation is:</p>
  
  <pre><code>void tcp_conn::start() {
      socket_.async_read_some(
          ba::buffer(&amp;buffer_size_, sizeof(buffer_size_)),
          boost::bind(
            &amp;tcp_conn::handle_read_header,
            this,
            ba::placeholders::error,
            ba::placeholders::bytes_transferred
            )
          );
  }
  
  void tcp_conn::handle_read_header(const boost::system::error_code&amp; e, std::size_t bytes_transferred){
      if(!e) {
          buffer_wrapper_ =  buffer_.prepare(buffer_size_);
  
          socket_.async_read_some(
              ba::buffer(buffer_wrapper_),
              boost::bind(
                &amp;tcp_conn::handle_read,
                this,
                ba::placeholders::error,
                ba::placeholders::bytes_transferred
                )
              ); 
  
      } else {
          //stop connection
      }
  }
  
  
  void tcp_conn::handle_read(const boost::system::error_code&amp; e, std::size_t bytes_transferred)   {
      if (!e ) {
          buffer_.commit(bytes_transferred);
  
          if(buffer_.size() == buffer_size_) {
              std::cout &lt;&lt; "WHOLE BODY TRANSFERED NOW" &lt;&lt; std::endl;
              //has all data in buffer, handle it.
          }
          else {
              std::cout &lt;&lt; "NOT WHOLE BODY TRANSFERED, still got "&lt;&lt; (buffer_size_ - bytes_transferred) &lt;&lt; " to go" &lt;&lt; std::endl;
              socket_.async_read_some(
                  ba::buffer(buffer_wrapper_),
                  boost::bind(
                    &amp;tcp_conn::handle_read,
                    this,
                    ba::placeholders::error,
                    ba::placeholders::bytes_transferred
                    )
                  ); 
          }
  
  
      }
      else {
          //handle error
      }   
  }
  </code></pre>
  
  <p>This doesn't quite work. I'm guessing my error is somewhere in the recursive updates of <code>buffer_wrapper_</code> - some data gets lost, overwritten?</p>