Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7418301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:51:41+00:00 2026-05-29T07:51:41+00:00

Is there a way to read from a streambuf without removing the bytes? I’m

  • 0

Is there a way to read from a streambuf without removing the bytes?

I’m reading a ‘message size’ field from the buffer to check if the whole message was received.

If not, I’m posting another async read to get it, but the handler then has no way to know how long the message was supposed to be – because the size field was removed.

Any help appreciated!

E.g.

boost::asio::streambuf _buffer;

void onReceive(const boost::system::error_code& e, std::size_t bytesTransferred)
{
  if(e) return;

  if(_buffer.size() > 0)
  {
    // Partial message was previously received, but I don't know how long.
  }
  else
  {
    _buffer.commit(bytesTransferred);

    /* Read the size (and remove it from the stream) */
    unsigned short size = 0;
    std::istream in(&_buffer);
    in.read((char*)&size, sizeof(unsigned short);

    /* Got the whole message? */
    if(_buffer.size() > size)
    {
      /* Yes. */
    }
    else
    {
      /* No - read the rest. */
      boost::asio::async_read(/*...*/);
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:51:41+00:00Added an answer on May 29, 2026 at 7:51 am

    You can use read_async to initiate a read using the size of the message header and then adjust it in a ‘completion condition’ callback, like so:

    typedef boost::system::error_code error_code;
    
    template <typename Stream, typename Message>
    void MessageReader<Stream, Message>::startRead()
    {
      readBuffer = allocateMsg();
      async_read(stream, 
                 boost::asio::buffer(readBuffer.get(), sizeof(*readBuffer)),
                 boost::bind(&MessageReader<Stream, Message>::bytesToRead, this,
                             boost::asio::placeholders::error, 
                             boost::asio::placeholders::bytes_transferred),
                 boost::bind(&MessageReader<Stream, Message>::readDone, this, 
                             boost::asio::placeholders::error, 
                             boost::asio::placeholders::bytes_transferred));
    }
    
    template <typename Stream, typename Message>
    size_t MessageReader<Stream, Message>::bytesToRead(const error_code& error, 
                                                       size_t bytes_read)
    {
      size_t result;
    
      if (error)
        result = 0;                                         // error - stop reading
    
      else if (bytes_read < sizeof(CmnMessageHeader))
        result = sizeof(CmnMessageHeader) - bytes_read;     // read rest of header
    
      else if (readBuffer->header.byteCount > sizeof(*readBuffer))
        result = 0;                                         // bad byte count
    
      else
        result = readBuffer->header.byteCount - bytes_read; // read message body
    
      return result;
    }
    
    template <typename Stream, typename Message>
    void MessageReader<Stream, Message>::readDone(const error_code& error, 
                                                  size_t bytes_read)
    {
      if (error)
      {
        if (error.value() == boost::system::errc::no_such_file_or_directory)
        {
          notifyStop();
        }
    
        else if (error.value() != boost::system::errc::operation_canceled)
        {
          notifyStop();
        }
    
        // else the operation was cancelled, thus no stop notification is needed and
        // we can merely return
      }
    
      else if (bytes_read != readBuffer->header.byteCount)
      {
        LOG4CXX_ERROR(logger, "Message byte count mismatch");
        notifyStop();
      }
    
      else
      {
        handleMsg(readBuffer);
        startRead();
      }
    }
    

    EDIT: Added typedef for error_code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to limit the maximum buffer size to be read from
On a Windows PC, is there a way to read the raw bytes from
Is there any way to read specific bytes from a file? For example, I
Is there a way to read off from an [external] xml (an xml file
I have some code to read from a pdf file. Is there a way
Is there a way to read value for the WiX variable from a text
Is there an easy way to read a single char from the console as
Is there a way to read a ByteBuffer with a BufferedReader without having to
Is there a way to read from a USB barcode reader while ignoring the
Is there a way to read mails from pop3 recipit? I'm already using SwiftMailer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.