简体   繁体   中英

Is this real scenario using boost::asio for full duplex communication?

I'm using boost::asio library for creating full duplex server. I wondering whether this situation is possible to happen.

  1. Came a request which is stored in its own buffer.
  2. I filled the response data in its own buffer.
  3. I'm starting to send the response asynchronously.
  4. Came a new request.
  5. I filled the response data in the same buffer in which I had filled it previous time.
  6. The buffer of the previous write operation which is not yet finished is corrupted with the new response data.

The question is whether I need separate buffer for every write operation or in point 3 the buffer is copied in some kind of internal buffer and I can safely to fill the new response in the same buffer?

boost::asio is able to do full-duplex operation, but you should manage buffers carefully.

The general rules is:

  1. Only 1 read operation on given socket can be active at a time.
  2. Same for write operations

So you can do 1 write and 1 read operation simultaneously.

Situation you described should work in single-threaded environment since process cannot add to buffer and use it for write simultaneously. But, there is a trick however: after you append to write buffer you cannot know is current async_write done or not. Do you need to start new async_write or not? This moment needs to be carefully verified.

PS And no, asio never copies buffers internally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM