C#如何实现数据通信中的等待确认?

发布于 2024-08-12 12:29:12 字数 1262 浏览 3 评论 0原文

我有一个类应该以数据包形式发送/接收数据。此类包含一个事件处理程序,该事件处理程序在可从物理介质读取新数据时运行。

在事件处理程序中,我从介质中读取数据并解析完整数据包的可用数据。一旦识别出数据包,就会引发一个事件以将新数据包传递给订阅者。该事件的订阅者决定是否要使用该数据包。

到目前为止一切顺利...现在回答我的问题。虽然上面的场景可以将传入数据分发给众多订阅者,并将进一步的处理逻辑放在应用程序中,但它给我带来了一个问题:

有时类会收到一个只是回复的数据包(想想 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛时过境迁 2024-08-19 12:29:12

好的,这就是它的工作原理:

在事件处理程序中,从介质中读取数据。检查它是数据还是只是前一个数据包的确认 (ACK)。

如果是数据,就传递出去。

如果是 ACK,则将其放入专门的 ACK 队列中。

在发送消息后,在 SendMessage 方法中迭代此 ACK Queue,直到:

  1. 发生超时 已
  2. 发送数据包的确认到达

就是这样。

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 ACK Queue after sending your message until:

  1. Timeout occurs
  2. The confirmation for your sent packet arrives

That's it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文