0

From user view, the property of "may not transmit all of the data" is a trouble thing. That will cause handler calls more than one time(may be).

The free function async_write ensure handler call only once, but it requires caller must call it in sequence or the data written will be interleaving. For network application usage, this is more bad than handler be called more than once.

If user want to handler called only once and data written is correct, user need to to do something.

I want to ask is: why asio not just make socket::async_write_some transmit all data?

jean
  • 2,825
  • 2
  • 35
  • 72

1 Answers1

0

I want to ask is: why asio not just make socket::async_write_some transmit all data?

Opposed to async_write, socket::async_write_some is lower-level method.

The OS network stack is designed with send buffers and receive buffers. This buffers are required to be limited with some amount of memory. When you send many data over socket, receiving side can be more slow than sending and/or there can be network speed issues.

This is the reason why socket send buffers are limited and as a result system's syscalls like write or writev should be able to notify user program that system cannot accept chunk of data right now. With socket in async mode its even more critical. So, socket syscalls cannot work in async manner without signaling program to hold on.

So, the async_write_some as a mid-level wrapper to writev is required to support partial writes. In other hand async_write is composed operation and can call async_write_some many times in order to send buffers until operation is complete or possibly failed. It calls completion handler only once, not for each chunk of data passed to network stack.

If user want to handler called only once and data written is correct, user need to to do something.

Nothing special, just to use async_write, not socket::async_write_some.

Galimov Albert
  • 7,269
  • 1
  • 24
  • 50
  • I mean loop{async_write(200k data);} will make receiver gets corrupted data. – jean Mar 31 '16 at 09:22
  • @jean ah, you talking about write queue. Sometimes i want this feature too to be standard for asio package ;) – Galimov Albert Mar 31 '16 at 09:24
  • yeah, like this one: http://stackoverflow.com/a/7756894/851185. I really feel this thing should support by asio itself. – jean Mar 31 '16 at 09:30