Question

From my client/server I receive serialized data, once the data is deserialized, it goes into a command handler where receivedData.Action is the ClientMessage:

Command._handlers[receivedData.Action].Handle(receivedData.Profile);

The command handler will work out the client message and return the response that should be given to the client.

I have an enum for the client messages as follow:

public enum ClientMessage
{
    INIT = 1,
    NEW_PROFILE,
    UPDATE_PROFILE_EMAIL,
    UPDATE_PROFILE_PASSWORD,
    UPDATE_PROFILE_PHONE,
    UPDATE_PROFILE_DATE,
    UPDATE_PROFILE_SECRET_ANSWER,
    UPDATE_PROFILE_POSTAL_CODE,
    UPDATE_SUCCESS,
    PING,
    PONG,
    QUIT
}

What I am having a difficult is how to have all the actions written, for example:

  • Should I have a separated enum for what the client sends and another for what the server should reply with ?
  • Or should I have a single enum with all messages and follow it as requested ?
  • Or how should I go about defining the messages and handling it ?

This is what my server/client currently does just to give you a better view:

  1. Server starts
  2. Client connects
  3. Client send auth to server
  4. Server verify client and send connected approval message
  5. Client will from there start sending and updating profiles to the server

This is roughly an example only.

IPacketHandler

public interface IPacketHandler
{
    MyCommunicationData Handle(ProfileData profile);
}

Command

public class Command
{
    public static Dictionary<ClientMessage, IPacketHandler> _handlers = new Dictionary<ClientMessage, IPacketHandler>()
    {
        {ClientMessage.INIT, new Init()},
        {ClientMessage.NEW_PROFILE, new NewProfile()},
        {ClientMessage.UPDATE_PROFILE_EMAIL, new UpdateEmail()},
        {ClientMessage.UPDATE_PROFILE_PASSWORD, new UpdatePassword()},
        {ClientMessage.UPDATE_PROFILE_PHONE, new UpdatePhone()},
        {ClientMessage.UPDATE_PROFILE_DATE, new UpdateDate()},
        {ClientMessage.UPDATE_PROFILE_SECRET_ANSWER, new UpdateSecretAnswer()},
        {ClientMessage.UPDATE_PROFILE_POSTAL_CODE, new UpdatePostalCode()},
        {ClientMessage.UPDATE_SUCCESS, new Success()},
        {ClientMessage.PING, new Ping()},
        {ClientMessage.PONG, new Pong()},
        {ClientMessage.QUIT, new Quit()},
    };
}

Example of the INIT

public class Init : IPacketHandler
{
    public MyCommunicationData Handle(ProfileData profile)
    {
        // Some verification to auth the client here 
        // bla bla
        // return response
        return new MyCommunicationData() { Action = ClientMessage.CONNECTED };
    }
}

PS: If my title is off and you have a better suggestion let me know or if you can go ahead and update it, I was not sure of how to describe this in English.

Was it helpful?

Solution

If your question is about how to design the class and interactions as I understood it, then I would - and it's totally dependant on the specifics of your application - separate this big Enumerations type into separate, smaller ones that are more descriptive of what they do, and of your intentions, for example, ProfileAction, ActionResult, PingStatus etc.. Then when you're using these enums, you make sure that you get compiler-time checks that you're doing it correctly, otherwise, what you're doing is almost like just passing strings.

It also has to do with sticking to Single Responsibility principle in OO design: an object should have single responsibility. Your enum as it stands now has more than one responsibility.

With issues like these, I find it helpful to look at what .NET framework does: for example look at Ping class and how it uses PingStatus enumerations and other enumerations as well.

OTHER TIPS

Not sure I'd use an enum at all. They are great inside a peice of code, exposed as communicated value, they are considerably less than great.

For me I'd have a different class per message, not one message with a god property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top