简体   繁体   中英

How to synchronize sending and receiving frames on the client side?

If I send request (frame) B and receive response, while at the same time I am already waiting for response for the previous request (A), the received response in B task, is in fact a response for A request:

request A sent
request B sent
request B processed (quick)
response B sent
response B received (*)
...
request A processed (slow)
response A sent
response A received 

With naive matching I would get at point (*) a match request A--response B.

So how to synchronize them? Ie I would like to end request A with response A, and request B with response B no matter how long does it take to handle each request. For the record I am using dealer-router sockets.

I am thinking about creating pool requests ( Dictionary ) with task completion token bound to each request sent. A task creates new slot in the pool, sends requests and awaits the completion. In the background there is task running all the time -- a receiver -- it simply receives the responses, put each response in appropriate slot and sets given task token as completed. Am I on the right track, am I wrong, or it is already implemented in NetMQ?.

Read about the AsyncSocket pattern in my blog:

http://somdoron.com/2014/08/netmq-asp-net/

It has an example with TaskCompletionSource, sequence for each request and dictionary.

If you want synchronous request/response communication you can use REQ/REP sockets instead of DEALER/ROUTER.

If you want asynchronous communication and you need to match responses to request, you can send a unique request ID in the request and return this request ID in the response.

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