Copy a streambuf’s contents to a string

I don’t know whether it counts as “excessive copying“, but you can use a stringstream: std::ostringstream ss; ss << someStreamBuf; std::string s = ss.str(); Like, to read everything from stdin into a string, do std::ostringstream ss; ss << std::cin.rdbuf(); std::string s = ss.str(); Alternatively, you may also use a istreambuf_iterator. You will have to measure … Read more

Boost ASIO streambuf

The nomenclature for boost::asio::streambuf is similar to that of which is defined in the C++ standard, and used across various classes in the standard template library, wherein data is written to an output stream and data is read from an input stream. For example, one could use std::cout.put() to write to the output stream, and … Read more

boost::asio and Active Object

Boost.Asio can be used to encompass the intention of Active Object: decouple method execution from method invocation. Additional requirements will need to be handled at a higher-level, but it is not overly complex when using Boost.Asio in conjunction with other Boost libraries. Scheduler could use: boost::thread for thread abstraction. boost::thread_group to manage lifetime of threads. … Read more

Thread pool using boost asio

In short, you need to wrap the user’s provided task with another function that will: Invoke the user function or callable object. Lock the mutex and decrement the counter. I may not be understanding all the requirements for this thread pool. Thus, for clarity, here is an explicit list as to what I believe are … Read more

Boost async_* functions and shared_ptr’s

In short, boost::bind creates a copy of the boost::shared_ptr<Connection> that is returned from shared_from_this(), and boost::asio may create a copy of the handler. The copy of the handler will remain alive until one of the following occurs: The handler has been called by a thread from which the service’s run(), run_one(), poll() or poll_one() member … Read more

boost asio ssl async_shutdown always finishes with an error?

For a cryptographically secure shutdown, both parties musts execute shutdown operations on the boost::asio::ssl::stream by either invoking shutdown() or async_shutdown() and running the io_service. If the operation completes with an error_code that does not have an SSL category and was not cancelled before part of the shutdown could occur, then the connection was securely shutdown … Read more

Why do we need to use boost::asio::io_service::work?

When the io_service::run method is called without a work object, it will return right away. Typically, that is not the behavior most developers are looking for. There are of course some exceptions, but most developers are looking to specify a thread to handle all of the asynchronous processing and don’t want that thread to exit … Read more

Boost::Asio : io_service.run() vs poll() or how do I integrate boost::asio in mainloop

Using io_service::poll instead of io_service::run is perfectly acceptable. The difference is explained in the documentation The poll() function may also be used to dispatch ready handlers, but without blocking. Note that io_service::run will block if there’s any work left in the queue The work class is used to inform the io_service when work starts and … Read more