kain
kain

Reputation: 121

C# UDP Communication-Stack implementation advise

I am currently writing a C# Program and am struggling a bit with a conceptional question.

A domain of the application is the communication with a Microcontroller via UDP. Therefore I am using the UDPClient and wrote a 'communicator' which does some encoding/decoding, checksum checks, etc.

I need to have some kind of Controller which allows me to send commands to the UDP-Server (the device). Some of them are a single pair of Send/Receive, others are long-running.

I thought about the following implemenation: enter image description here

My idea was that every capability of my Microcontroller/UDP-Server is abstracted in a class, which has an 'Execute'-function which subscribes to the 'Receive' of the underlying Communicator and sends it request.

However, I am a bit stuck here: I could wait in the 'Execute ' function of each task(capability) for an event which I trigger in the OnReceive-Handler (or timeout). But that would not work if I one task needs multiple Send/Receive.

So, is there any good Design-Pattern for that? Or does anyone have a good advise on how to implement it in a proper way?

Upvotes: 0

Views: 98

Answers (1)

Beginner
Beginner

Reputation: 1030

You can have a Communicator and a CommandExecuter and share a queue between them. Communicator puts every command it receives from UDP and goes back to it's duty to receive new commands. CommandExecuter is always watching the queue. As long as there is a new command in the queue, takes it and executes it.

Upvotes: 1

Related Questions