简体   繁体   English

如何在客户端同步发送和接收帧?

[英]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: 如果我发送请求(帧)B并接收响应,而同时我已经在等待上一个请求(A)的响应,那么B任务中接收到的响应实际上就是对A请求的响应:

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. 使用天真的匹配,我会在(*)点得到一个匹配请求A-响应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. 也就是说,无论处理每个请求需要花费多长时间,我都希望以响应A结束请求A,并以响应B结束请求B。 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. 我正在考虑创建将任务完成令牌绑定到发送的每个请求的池请求( Dictionary )。 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?. 我是在正确的轨道上吗?我错了吗?或者它已经在NetMQ中实现?

Read about the AsyncSocket pattern in my blog: 在我的博客中了解有关AsyncSocket模式的信息:

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

It has an example with TaskCompletionSource, sequence for each request and dictionary. 它有一个TaskCompletionSource的示例,每个请求的顺序和字典。

If you want synchronous request/response communication you can use REQ/REP sockets instead of DEALER/ROUTER. 如果要同步请求/响应通信,可以使用REQ / REP套接字而不是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. 如果需要异步通信,并且需要将响应与请求匹配,则可以在请求中发送唯一的请求ID,然后在响应中返回此请求ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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