message - Deserializing unknown inherited type [C++] -


Let's say I have a class that sends messages to their handler, this class is getting messages from another class The message that receives through the socket. Therefore, the socket receives a type of message buffer.

Roots messages that are know about class message types. Every message is the successor of the message class, in which there is a message id, and of course its own parameter is included.

The problem is how do I correctly transfer the message from a buffer to an active message instance?

For exmaple, I have a DoSomethingMessage that receives message messages, I get a buffer with a message, but I need to convert buffer into two group messages, it's actually anything Do not know about

I could transfer the buffer to the messenger, and make the id and the exact example, but it really looks like a bad design for me.

Any suggestions?

You may summarize the message deserialization is a "message holder" class in which the object is buffer Has started. This would be a way:

IMessageInterface NarrowToInterface (MessageId id);

This was not clear to me if your router already knew what kind of message was or not, if this happens, then it will receive the message of the holder and the NarrowToInterface method on it Will call on

This will pass the ID of the appropriate type if the router did not know how it was, then you also have a property on the message holder object:

MessageId GetMessageType ();

The router will use to determine what kind of message was to decide how it is applied even after this path is still there.

The IMessageInterface is an abstract class or interface, which was given below for the appropriate type of message to the recipient, because it is expected that what kind of expectation is if all the different messages are well known And you have generic or templates available, you can have a NarrowToInterface method as a template method, which took the return value as a template parameter, so that you can get better Doors to enable security. If you do not have templates then you can use double-dispatch technique of "Vistor" pattern. Google "Double-Dispatch Visitor" for more information

If the types of messages are not well defined or can develop in the future, then you just have to be with the (compiler-unreachable) disappointment at some points. The implementation I am suggesting is possible as much as possible, and as much as I know, the expansion by adding as little as I can

In addition to this, your messages should be prepared with a standard identifier to work. Headers, that is, a standard header that contains the length of the entire message as well as the message type ID. In this way, the socket endpoint can parse the basics of the message and put it in the message holder. MessageHolder knows about all the different message types to implement the NarrowToInterface () method, or there may be a global repository that will return "IMessageDeserializer" objects to implement NarrowToInterface for every message type. All the loaded messaging clients will register all deserializers for all those messages that they support with the repository and want the message type ID with message router.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -