2

Context

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

  • X bytes of metadata in XML
  • X bytes of optional binary data

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?

4

1 回答 1

2

最后我根本没有使用流。它们似乎易于使用/友好,但它们不允许异步操作。我意识到混合同步和异步操作并不是一个好主意。向队列添加异步操作有助于平衡服务器的负载。结论:你应该只使用异步操作,因为 Boost.Asio 主要是为它们设计的。

于 2013-08-07T16:27:57.280 回答