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 8117023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:01:01+00:00 2026-06-06T04:01:01+00:00

I’m writing a server application using boost::asio. I am reading known amount of data

  • 0

I’m writing a server application using boost::asio.
I am reading known amount of data from user and write it to a data sink which has following api:

class Sink {
    ...
    void write(const char *data, size_t size);
}

The data is big and it is possible to call write(..) multiple times to process one stream.
In my code I would like to call:

boost::asio::async_read(socket, sink_buffer,
    boost::asio::transfer_exactly(size), ...);

Is it possible to wrap Sink with custom std::streambuf or boost::asio::basic_streambuf so it could handle writing data parts to it?

  • 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-06-06T04:01:02+00:00Added an answer on June 6, 2026 at 4:01 am

    For objects being passed as the buffers argument for async_read(), the buffers argument either :

    • needs to be a type that meets the MutableBufferSequence requirement.
    • be a boost::asio::basic_streambuf object.

    Thus, it is possible to write a custom class that interacts with a Sink object when reading occurs. However, the boost::asio::basic_streambuf does not appear to be designed to serve as a base class.

    If Sink::write is only an abstraction to the underlying memory, consider using an approach similar to basic_streambuf::prepare(), where a member function returns a handle to a buffer for a given size. The underlying memory implementation would still remain abstracted behind the mutable_buffer. For example:

    boost::asio::async_read( socket, sink.buffer( size ), ... );
    

    If Sink::write has business logic, such as performing logic branching based on the value of certain bytes, then it may be required to pass an intermediate buffer to async_read(). The invocation of Sink::write() with the intermediate buffer would then be done from within async_read()‘s handler. For example:

    void handle_read_into_sink( boost::system::error_code error,
                                std::size_t bytes_transferred,
                                boost::asio::ip::tcp::socket& socket,
                                Sink& sink,
                                char* buffer,
                                std::size_t buffer_size,
                                std::size_t bytes_remaining,
                                boost::function< void() > on_finish )
    {
      sink.write( buffer, buffer_size );
    
      bytes_remaining -= bytes_transferred;
      // If there are more bytes remaining, then continue reading.
      if ( bytes_remaining )
      {
        read_into_sink( socket, sink, buffer, buffer_size,
                        bytes_remaining, on_finish );
      }
      // Otherwise, all data has been read.
      else
      {
        on_finish();
      }  
    }
    
    void read_into_sink( boost::asio::ip::tcp::socket& socket,
                         Sink& sink,
                         char* buffer,
                         std::size_t buffer_size,
                         std::size_t bytes_remaining,
                         boost::function< void() > on_finish )
    {
      boost::asio::async_read(
        socket, boost::asio::buffer( buffer , buffer_size ),
        boost::asio::transfer_exactly( buffer_size ),
        boost::bind( handle_read_into_sink,
                     boost::asio::placeholders::error,
                     boost::asio::placeholders::bytes_transferred,
                     boost::ref( socket ),
                     boost::ref( sink ),
                     buffer,
                     buffer_size,
                     bytes_remaining,
                     on_finish ) );
    }
    

    And begin the async read loop with:

    read_into_sink( socket, sink, small_buffer, sizeof_small_buffer, 
                    total_stream_size, read_handler_callback );
    

    Make sure to check and handle error based on your desired logic.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.