1

I am currently working on a server that handles specific "Commands" from connected clients. Something akin to what's below:

    switch CommandType.Command {
    case CommandType.A:
        //do something
        break;
    case CommandType.B
        //do something
        break;
}

A thread constantly reads commands from the clients and the server deals with them appropriately. Sometimes when a specific command is dealt with the server will want a very specific Command from the client, and to ignore everything else until that command has been received. My issue lies with this. One option is to have a variable such as "CommandBeingWaitedFor" or something like that. When the server wants a specific command from the clients it will set that variable to the required command. This method feels awful because it means that for every command in the switch statement we must first check against this variable to see if there is something currently being specifically waited on. As the number of different possible commands gets bigger this feels like there will be a lot of repeated code. Ideally, there is a simpler and more efficient way to deal with this sort of interaction.

Any help would be greatly appreciated.

Tayyab Hussain
  • 107
  • 2
  • 10
  • 2
    Sounds like you need a state machine, see eg https://stackoverflow.com/questions/5923767/simple-state-machine-example-in-c – Charlieface Aug 21 '22 at 00:18
  • "and to ignore everything else until that command has been received." - does it mean all commands between now and next "very specific command" will be dropped completely (lost)? Or they will be processed later? – Boppity Bop Aug 21 '22 at 05:31
  • Completely dropped. They should be completely ignored. – Tayyab Hussain Aug 21 '22 at 15:04
  • @Charlieface I love this idea, I'm definitely going to look into this. I'll leave this question up though to see if anyone else has any other good suggestions. – Tayyab Hussain Aug 21 '22 at 15:04

0 Answers0