当您使用简单的 ZeroMQ REQ/REP 模式时,您依赖于固定的 send()->recv()/recv()->send() 序列。 作为this文章描述了当参与者在请求中间断开连接时你会遇到麻烦,因为这样你就不能从另一个连接接收下一个请求,但状态机会强制你向断开连接的请求发送请求。
自从提到的文章已经写完之后,是否出现了更优雅的方法来解决这个问题?
重新连接是解决这个问题的唯一方法(除了不使用 REQ/REP 而是使用其他模式)
最佳答案
由于接受的答案让我感到非常难过,所以我做了一些研究,发现我们需要的一切实际上都在文档中。
具有正确参数的 .setsockopt()
可以帮助您重置套接字状态机,而无需粗暴地破坏它并在前一个尸体之上重建另一个.
(是的,我喜欢这张图片)。
ZMQ_REQ_CORRELATE:
match replies with requests
The default behaviour ofREQ
sockets is to rely on the ordering of messages to match requests and responses and that is usually sufficient. When this option is set to1
, theREQ
socket will prefix outgoing messages with an extra frame containing a request id. That means the full message is (request id
,0
,user frames…
). TheREQ
socket will discard all incoming messages that don't begin with these two frames.
Option value typeint
Option value unit0
,1
Default value0
Applicable socket typesZMQ_REQ
ZMQ_REQ_RELAXED:
relax strict alternation between request and reply
By default, aREQ
socket does not allow initiating a new request withzmq_send(3)
until the reply to the previous one has been received. When set to1
, sending another message is allowed and has the effect of disconnecting the underlying connection to the peer from which the reply was expected, triggering a reconnection attempt on transports that support it. The request-reply state machine is reset and a new request is sent to the next available peer.
If set to1
, also enableZMQ_REQ_CORRELATE
to ensure correct matching of requests and replies. Otherwise a late reply to an aborted request can be reported as the reply to the superseding request.
Option value typeint
Option value unit0
,1
Default value0
Applicable socket typesZMQ_REQ
关于c++ - zeromq:重置 REQ/REP 套接字状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26915347/