I’m currently working on a simple Client/Server model which sends packets within TCP packets (like HTTP) and the commands are basically ints (the first 4 bytes of each packet) and I’d like to work out an efficient way of how to process these commands.
The most obvious answer would be to write thousands of if’s or do one huge switch statement with thousand cases, but isn’t there any better way?
I’d like to create an array of events and then just raise the corresponding index so each int refers to one event which is named (such as MessageReceived). I’d also save time I think, so how could I work this out?
EDIT: The Server handles multiple connections, one for each client that is connected, so creating seperate connections for each command(s) isn’t that useful in my case.
Sound like a job for enums!
Visual studio can even create your entire switch statement using the built in snippets and your code becomes very readable.
becomes
Update 1
This is a little scary from a maintainability point of view, but if you created a class like:
or a little nicer but harder to maintain:
Update 2 (Coding what I think Josh C. was talking about)
This you create the SomeActionProcessor that is responsible for some value:
Then create the classes and wire them up:
The fire away and sending the generic processor requests: