Pierre T.
Pierre T.

Reputation: 378

Hybrid tcp::iostream and socket operation with Boost.Asio

Context

Firstly some context: A Boost.Asio server receives requests formatted as follow:

We have a XML parser that takes a std::istream so it's really easy to just pass tcp::iostream to this function. The binary data that follows can be heavy, so we should asynchronously read packets of —say 500ko— until we read all the data.

Questions

Can we use the tcp::iostream first and then use an async_read with the underlining socket_streambuf? Of course, it's ok for the compilation, but does the read operation on socket correctly use the internal buffer of the tcp::iostream? (That could be not empty even after having read the metadata). I guess it should respect the Liskov Substitution Principle, but I prefer to be sure.

Can we switch from one method to another, where/whenever we want?

Upvotes: 2

Views: 250

Answers (1)

Pierre T.
Pierre T.

Reputation: 378

Finally I didn't used the stream at all. They seemed to be easy/friendly to use, but they doesn't allow asynchronous operations. I realized that mixing synchronous and asynchronous operations was not a good idea. Adding asynchronous operation to the queue help to balance the load of the server. Conclusion: You should only use asynchronous operations because Boost.Asio is mainly designed for them.

Upvotes: 2

Related Questions