C#如何实现数据通信中的等待确认?
我有一个类应该以数据包形式发送/接收数据。此类包含一个事件处理程序,该事件处理程序在可从物理介质读取新数据时运行。
在事件处理程序中,我从介质中读取数据并解析完整数据包的可用数据。一旦识别出数据包,就会引发一个事件以将新数据包传递给订阅者。该事件的订阅者决定是否要使用该数据包。
到目前为止一切顺利...现在回答我的问题。虽然上面的场景可以将传入数据分发给众多订阅者,并将进一步的处理逻辑放在应用程序中,但它给我带来了一个问题:
有时类会收到一个只是回复的数据包(想想 ACK/FAIL )到该类发送的另一个数据包。
我如何实现一种方法来发送等待此类确认的内容,同时又不破坏上述在事件处理程序中处理传入原始数据的概念?
一些伪代码说明问题:
class CommCenter
{
public event NewPacketAvailable;
private void _newRawDataAvailable_EventHandler
{
// read incoming data from physical medium
// parse incoming data for packet structure
// the received packet may either contain a reply to
// a previously sent message or unrelated
// if packet found raise NewPacketAvailable event
// so that subscribers can handle the packet
}
public void SendMessage(DataPacket packet, Int32 timeoutInMilliseconds)
{
// put message on the physical medium
// wait for a packet confirming the previously sent packet
// How would I have to do this??
// the packet confirmation would arrive in _newRawDataAvailable_EventHandler
}
}
也许有一个好主意,甚至以前实现过这样的逻辑。我现在有点困惑该怎么办。很高兴获得任何帮助或正确方向的指示;-)
I have an class which should send/receive data in packet form. This class contains an event handler which runs when new data is available to be read from the physical medium.
In the event handler I read the data from the medium and parse the available data for complete packets. Once a packet is identified an event is raised to pass the new packet to subscribers. The subscribers to this event decide if they want to use the packet or not.
So far so good... Now to my question. While the scenario above works to distribute the incoming data to a multitude of subscribers and place further processing logic further up in the application it leaves me with a problem:
Sometimes the class will receive a data packet which is just a reply (think ACK/FAIL) to another packet which was sent by the class.
How would I implement a method to send something which would wait for such a confirmation while not breaking the aforementioned concept of handling incoming raw data in the event handler?
Some pseudo code to illustrate the problem:
class CommCenter
{
public event NewPacketAvailable;
private void _newRawDataAvailable_EventHandler
{
// read incoming data from physical medium
// parse incoming data for packet structure
// the received packet may either contain a reply to
// a previously sent message or unrelated
// if packet found raise NewPacketAvailable event
// so that subscribers can handle the packet
}
public void SendMessage(DataPacket packet, Int32 timeoutInMilliseconds)
{
// put message on the physical medium
// wait for a packet confirming the previously sent packet
// How would I have to do this??
// the packet confirmation would arrive in _newRawDataAvailable_EventHandler
}
}
Maybe has a good idea or even implemented such a logic before. I'm a bit confused at the moment what to do. Glad for any help or pointers in the right direction ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这就是它的工作原理:
在事件处理程序中,从介质中读取数据。检查它是数据还是只是前一个数据包的确认 (ACK)。
如果是数据,就传递出去。
如果是 ACK,则将其放入专门的 ACK 队列中。
在发送消息后,在
SendMessage
方法中迭代此 ACKQueue
,直到:就是这样。
Okay, this is how it works:
In the event handler, read the data from the medium. Check if it's data or just a confirmation of a previous packet (ACK).
If it is data, pass it on.
If it is ACK, put it into a special
Queue
for ACKs.In the
SendMessage
method iterate over this ACKQueue
after sending your message until:That's it.