Originally, We have two applications communication with TCP/IP, and both of them are implemented by C++. The messages between them are custom message type.
Now the client program will be changed to web application based on nodejs
and the communication between them will be changed to message bus such as rabbitmq
So the message type between them should be changed.
JSON
comes to my mind first, however the custom messages are too complicated, which are defined by template
and inheritance
. It seems that convert the custom message to JSON
is not a good option. Am I right?
class Address {
int network;
int addressType;
//...
};
class MsgType{
unsigned char msgSeq;
unsigned int msgLen;
//...
};
class Message{
Address destination;
Address source;
MsgType msgType;
//...
};
template <typename T, int RESPONSE_TYPE>
class ResponseMessage : public Message{
//...
}
typedef struct{
int number;
int type;
}ConfigResp;
class CfgResp : public ResponseMessage<ConfigResp, CONFIG_REQUEST>
{
//...
}
Protocol Buffers
is another option for me to do that. What should I do?
redefine the custom message into protocol buffer? no no
Here is my solution: Just wrap the whole original custom message (binary type) into protocol buffer as one message in the server side, then decode the custom message(binary type) in client side. Is that possible?
1
Well, applications tend to become more complicated with time. If you will ever need to handle separate versions of API which will in turn require you to maintain several versions of data structure, I think you’d better go with protocol buffers since they are designed with some versioning in mind. With JSON you will be on your own.
If at a given moment in time you are required to support only one version of a protocol, you may consider writing a serializer for your bussiness objects to stream them into JSON back and forth.
Another thing to consider is speed. I think protocol buffers could be faster when it comes to serialization/deserialization speed than pure json. But that needs to be carefully tested since this depends on implementation.