0

I would like to implement simple network protocol. The message is being sent through the socket and the reply is received within timeout milliseconds. The question arises here is how to set order between timeout handler and async_read_until handler. Unfortunately, strands won't help here much:

http://www.boost.org/doc/libs/1_43_0/doc/html/boost_asio/reference/io_service__strand.html

Note that in the following case:

async_op_1(..., s.wrap(a));
async_op_2(..., s.wrap(b)); 

the completion of the first async operation will perform s.dispatch(a), and the second will perform s.dispatch(b), but the order in which those are performed is unspecified. That is, you cannot state whether one happens-before the other. Therefore none of the above conditions are met and no ordering guarantee is made.

Imagine that io_service io is shared between a number of different handlers, operations and so on. Then, between time moments A and B io.run() will run some handler function which is not relevant to this particular socket operation at all. Then actual data may be received by operating system at time moment C which is A < C < B. Also, the timer may be expired at time moment D which is A < D < B. Two this events: data reception C and timer expiration D will be detected when io.run() will finish with the current handler job not earlier than time moment B.

What I want is to preserve handler invocation order, i.e. that async_read handler is called before async_wait handler if and only if C < D. Otherwise I could end up with the situation when the data were actually obtained before timeout deadline but discarded as timeouted.

0x2207
  • 878
  • 1
  • 6
  • 20
  • I think you rather mean all the answers there are relevant, but don't address your ordering concern. You can find excellent answers regarding that from [Tanner or Sam](https://stackoverflow.com/tags/boost-asio/topusers) as I remember. So, if you remove the cross-concern from the question, I think this has been answered – sehe Feb 02 '17 at 07:55
  • E.g. look at http://stackoverflow.com/a/19963481/85371 – sehe Feb 02 '17 at 08:52
  • http://stackoverflow.com/questions/19946555 suggests to use chaining. This won't work in my case. – 0x2207 Feb 04 '17 at 19:08

0 Answers0