4

I'm trying to get to grips with boost asio but I'm having trouble understanding some of the behavior behind the asynchronous interface.

I have a simple setup with a client and a server.

  • The client calls async_write regularly with a fixed amount of data
  • The server polls for data regularly
  • What happens when the server stops polling for data ?

I guess the various buffers would fill up in the server OS and it would stop sending ACKs ?

Regardless of what happens it seems that the client can happily continue to send several gigabytes of data without receiving any error callback (doesn't receive any success either of course).

I assume the client OS stops accepting packets at one point since they can't be TX'ed ?

Does this means that boost::asio buffers data internally ?

If it does, can I use socket.cancel() to drop packets in case I don't want to wait for delivery ? (I need to make sure ASIO forgets about my packets so I can reuse old buffers for new packets)

user2510141
  • 374
  • 3
  • 12

1 Answers1

3

asio doesn't buffer internally. And you will always get signaled if you can't transfer more data to the remote.

E.g. if you use synchronous writes in asio they will block until the data could be sent (or at least be copied into the kernel send buffers). If you use async writes the callback/acknowledgement will only be called once it could be sent. If you use nonblocking writes you get EAGAIN/WOULD_BLOCK errors. If you use multiple async_write's in parallel - well - you shouldn't do that, it's behavior is undefined according to the asio docs:

This operation is implemented in terms of zero or more calls to the stream's async_write_some function, and is known as a composed operation. The program must ensure that the stream performs no other write operations (such as async_write, the stream's async_write_some function, or any other composed operations that perform writes) until this operation completes.

Guarantee in your application that you always only perform a single async write operation and once that finishes write the next piece of data. If you need to write data in between you would need to buffer that inside your application.

Matthias247
  • 9,836
  • 1
  • 20
  • 29
  • Thanks for the detailed answer and the heads up on the "single call at the time". For future reference this answer http://stackoverflow.com/a/1998127/2510141 also provides a bit of insight. – user2510141 Jan 05 '17 at 18:40