我正在创建一种客户端-服务器实现,我想确保每条发送的消息都会得到响应。所以我想创建一个超时机制,它不检查消息本身是否已传递,而是检查传递的消息是否得到响应。
IE,对于两台计算机 1 和 2:
1: send successfully: "hello"
2: <<nothing>>
...
1: Didn't get a response for my "hello" --> timeout
我想通过为每条消息创建一个带有 id 的大布尔数组来实现这一点,该数组将保存一个“正在进行”标志,并将在收到消息响应时设置。
我想知道也许有更好的方法可以做到这一点。
谢谢,
我愿意。
I am creating a sort of a client-server implementation, and I'd like to make sure that every sent message gets a response. So I want to create a timeout mechanism, which doesn't check if the message itself is delivered, but rather checks if the delivered message gets a response.
IE, for two computers 1 and 2:
1: send successfully: "hello"
2: <<nothing>>
...
1: Didn't get a response for my "hello" --> timeout
I thought of doing it by creating a big boolean array with id for each message, which will hold a "in progress" flag, and will be set when the message's response is received.
I was wondering perhaps there was a better way of doing that.
Thanks,
Ido.
发布评论
评论(1)
有一个更好的方法,有趣的是我自己刚刚在这里实现< /a>.它使用 TimeoutMixin 来实现您需要的超时行为,以及 DeferredLock 将正确的回复与发送了什么。
我还没有测试过这个,这只是一个起点。您可能需要在此处更改一些内容以满足您的目的:
我使用 LineOnlyReceiver — 这与问题本身无关,您需要将
sendLine
/lineReceived
替换为适当的 API 调用对于您的协议。这是用于串行连接,所以我不处理
connectionLost
等。您可能需要。我喜欢直接在实例中保存状态。如果您需要额外的状态信息,请在
_doSend
中进行设置,并在_cleanup
中进行清理。有些人不喜欢这样 - 另一种方法是在_doSend
中创建嵌套函数来关闭您需要的状态信息。不过,您仍然需要self.deferred
,否则lineReceived
(或dataReceived
)不知道该怎么做。如何使用它
就像我说的,我为串行通信创建了这个,我不必担心工厂、connectTCP 等。如果您使用 TCP 通信,您需要弄清楚您需要的额外粘合剂。
There is a better way, which funnily enough I myself just implemented here. It uses the TimeoutMixin to achieve the timeout behaviour you need, and a DeferredLock to match up the correct replies with what was sent.
I haven't tested this, it's more of a starting point. There are a few things you might want to change here to suit your purposes:
I use a LineOnlyReceiver — that's not relevant to the problem itself, and you'll need to replace
sendLine
/lineReceived
with the appropriate API calls for your protocol.This is for a serial connection, so I don't deal with
connectionLost
etc. You might need to.I like to keep state directly in the instance. If you need extra state information, set it up in
_doSend
and clean it up in_cleanup
. Some people don't like that — the alternative is to create nested functions inside_doSend
that close over the state information that you need. You'll still need thatself.deferred
there though, otherwiselineReceived
(ordataReceived
) has no idea what to do.How to use it
Like I said, I created this for serial communications, where I don't have to worry about factories, connectTCP, etc. If you're using TCP communications, you'll need to figure out the extra glue you need.