I'm working on an app which takes in a raw binary message (very simple, first byte is the message type, rest is payload), and then does something with it. The thing I'm trying to accomplish is making sure that the networking service is abstracted away from the rest of the app, to allow for modifying the protocol now and then without affecting the rest of the application too much. The context of the application is a very simple client-server game, for which I am doing the client work now.
I'm kinda struggling now though. I need to find an elegant way to just throw a connection into some sort of translator/adapter service, which returns pretty objects (I think). Those objects will be thrown in a queue awaiting consumption by the rest of the app. The problem I'm facing is more or less this construct (pseudo code):
Let's assume each message is the 20 bytes, so I can deal with calling this function for each 20 bytes:
public Message GetMessage(byte[] buffer)
{
switch(buffer[0])
{
case 1:
return Message1(...);
case 2:
return Message2(...);
.....
case n:
return MessageN(...);
}
}
Obviously, I'll use an enum or constants for the case, but that's not the thing that's bugging me. Here's the thing. I think I've got about 50 message types, which would mean I'll get a switch statement with 50 cases. I can't really think of a proper way to cut this up into smaller pieces, which will result in a huge, error prone method. I was wondering if there's any patterns to make this easier, as I couldn't find any.
Thanks for the input in advance!