I have a library that talks to a hardware device using UDP. The conversation goes something like this:
|------------000E------------>|
| |
|<-----------000F-------------|
| |
|------------DC23------------>|
| |
|<-----------DC24-------------|
First I send out opcode 000E and expect to get a 000F in response. Once I get the 000F, I send out a DC23 and expect a DC24 in response. (There is additional information included in the response along with the opcode.) In the future, more steps may need to be added to this conversation.
The object in charge of communicating with the device has the following interface:
public class Communication : ICommunication
{
public Communication();
public bool Send_LAN(byte subnetID, byte deviceID, int operateCode, ref byte[] addtional);
public event DataArrivalHandler DataArrival;
public delegate void DataArrivalHandler(byte subnetID, byte deviceID, int deviceType, int operateCode, int lengthOfAddtional, ref byte[] addtional);
}
When I try to write this code naively I end up with a switch
statement in the DataArrival
event handler that does different things according to the response code, like so:
private void _com_DataArrival(byte subnetID, byte deviceID, int deviceTypeCode, int operateCode, int lengthOfAddtional, ref byte[] addtional)
{
Debug.WriteLine($"OpCode: 0x{operateCode:X4}");
switch (operateCode)
{
case 0x000F: // Response to scan
// Process the response...
_com.Send_LAN(subnet, device, 0xDC23, ...);
break;
case 0xDC24:
// Continue processing...
break;
}
}
It's beginning to look like it's going to turn into a state machine. I think there has to be a better way to do it using TaskCompletionSource
and async/await
.
How do I go about doing this?