Arbeiten mit boost::asio::streambuf

Suchen für ein boost::asio (und mit sich selbst steigern) beschlossen, das schreiben asynchronen server. Zum speichern von eingehenden Daten nutze ich boost::asio::streambuf.
Hier habe ich ein problem. Wenn ich eine zweite Nachricht von dem client und anschließende sehe ich, dass der Puffer enthält Daten aus vorherigen Nachrichten.
Obwohl ich call Verbrauchen-Methode mit der input-buffer. Was ist falsch?

class tcp_connection
//Using shared_ptr and enable_shared_from_this 
//because we want to keep the tcp_connection object alive 
//as long as there is an operation that refers to it.
: public boost::enable_shared_from_this<tcp_connection>
{
...

boost::asio::streambuf receive_buffer;

boost::asio::io_service::strand strand;
}

...

void tcp_connection::receive()
{
//Read the response status line. The response_ streambuf will
//automatically grow to accommodate the entire line. The growth may be
//limited by passing a maximum size to the streambuf constructor.
boost::asio::async_read_until(m_socket, receive_buffer, "\r\n",
    strand.wrap(boost::bind(&tcp_connection::handle_receive, shared_from_this()/*this*/,
    boost::asio::placeholders::error,
    boost::asio::placeholders::bytes_transferred)));

}


void tcp_connection::handle_receive(const boost::system::error_code& error, 
std::size_t bytes_transferred)
{

if (!error)
{
    //process the data

    /*  boost::asio::async_read_until remarks

    After a successful async_read_until operation, 
    the streambuf may contain additional data beyond the delimiter.
    An application will typically leave that data in the streambuf for a
    subsequent async_read_until operation to examine.
    */

    /* didn't work      
    std::istream is(&receive_buffer);
    std::string line;
    std::getline(is, line); 
    */


    //clean up incomming buffer but it didn't work 
    receive_buffer.consume(bytes_transferred);  

    receive(); 

}
else if (error != boost::asio::error::operation_aborted)
{
    std::cout << "Client Disconnected\n";

    m_connection_manager.remove(shared_from_this());
}
}

InformationsquelleAutor vint | 2015-02-12

Schreibe einen Kommentar