English 中文(简体)
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 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 ;-)

最佳回答

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.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签