Boost logo

Boost :

Subject: [boost] boost::asio::streambuf problem
From: rod haxton (rehaxton_at_[hidden])
Date: 2008-10-18 14:19:50


Hello,

"Long time listener, first time caller..."

I'm struggling with the "boost::asio::streambuf," and the
"boost::asio::async_read_until."

I'm trying to wait on the async_read_until, which I can do after the first
message is sent, but subsequent messages received cause the streambuf to
continuously grow. I am trying to use the streambuf via the .data() member.

void TAsioSession::Start()
{
    boost::asio::async_read_until(m_Socket, m_szAsioData,
boost::regex("\n"),
            boost::bind(&TAsioSession::HandleRead,
            this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
}

void TAsioSession::HandleRead(const boost::system::error_code& error,
size_t bytes_transferred)
{
    if(!error)
    {
        boost::asio::streambuf::const_buffers_type bufs =
m_szAsioData.data();

        std::string szBuffer(
            boost::asio::buffers_begin(bufs),
            boost::asio::buffers_begin(bufs) + bytes_transferred);

        LP_ICPACKET lpICPacket =
reinterpret_cast<LP_ICPACKET>(const_cast<char*>(szBuffer.c_str()));

     // reset streambuf for reading (how?)
   .
   .
   .

  Start();
}

 Once I grab the data that is in the buffer, I want to reset the streambuf
and go back to "async_read_until," and wait for another message to come in.

I try to do the following to reset the streambuf pointers...

        // set the current put/get ptrs of streambuf to beginning of stream
        m_szAsioData.commit(-m_szAsioData.size());
        m_szAsioData.consume(-m_szAsioData.size());

        // clear the streambuff
        char szClear[512];
        ZeroMemory(szClear, 512);
        m_szAsioData.sputn(szClear, sizeof(szClear));
        m_szAsioData.sgetn(szClear, sizeof(szClear));

        // set the current ptrs of streambuf back to beginning
        m_szAsioData.commit(-m_szAsioData.size());
        m_szAsioData.consume(-m_szAsioData.size());

 but the new message isn't at the head of the buffer, it is coming in at
position 512 of the streambuf, and the streambuf grows.

Any help would be appreciated.

Thanks,
Rod


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk