M
M
Mozzarella2018-03-30 21:39:38
C++ / C#
Mozzarella, 2018-03-30 21:39:38

How to organize message processing on the server?

I am developing a game like HearthStone in c#. Server on TCPClient. At this stage, I'm trying to organize the processing of messages. The Message class encoded in json goes between the client and the server

public class Message
    {
        public MessageContent Content { get; set; }
        public Encoding Encoding { get; set; }
        public int BufferLengthSize { get; set; }
        public Type Type { get; set; }
    }

MessageContent, in fact, is the content that needs to be processed, there are various types of messages, for example AttackMessage
public class AttackMessage : MessageContent
    {
        public int AtackCartId { get; set; }
        public int AttackedCartId { get; set; }
    }

Now the idea is this, using the dependency container, I register named bindings
container.RegisterType<IController, AttackMessageController>(nameof(AttackMessage));

And in a single message handler, depending on the type of content, I call the desired controller
var controller = container.Resolve<IController>(response.Type.Name);

And form the answer
var response  = controller.GetResponse(response.Content)

Is it justifiable to use a dependency container for this, or is there a more practical solution?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lam0x86, 2018-03-30
@lam0x86

If you use a separate container for controllers, then this solution is possible, but in fact the container can be replaced with a regular one Dictionary<Type, IController>.
If you register your controllers in a shared container, it looks weird.
And in general, the structure of the Message class is questionable. Why Encoding and BufferLengthSize? Why is Type of type System.Type and not System.String?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question