开发者

C# How to implement waiting for a confirmation in data communication?

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 wa开发者_如何学JAVAnt 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 ;-)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜