简体   繁体   English

用于替换自定义TCP消息的JSON或协议缓冲区

[英]JSON or protocol buffer to replace the custom TCP message

Originally, We have two applications communication with TCP/IP, and both of them are implemented by C++. 最初,我们有两个应用程序与TCP / IP通信,它们都是由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 现在客户端程序将基于nodejs更改为Web应用程序,并且它们之间的通信将更改为消息总线,例如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 . 首先想到JSON ,但是自定义消息太复杂了,它们是由templateinheritance定义的。 It seems that convert the custom message to JSON is not a good option. 似乎将自定义消息转换为JSON不是一个好选择。 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. Protocol Buffers是我这样做的另一种选择。 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? 那可能吗?

It looks like you are structuring your application to become more extensible. 看起来您正在构建应用程序以使其更具可扩展性。 Not using a nice message format is completely counter to that aim. 不使用好的消息格式完全违背了这一目标。

Don't embed your binary format inside a protocol buffer chunk. 不要将二进制格式嵌入协议缓冲区块中。 You'll gain nothing - you'll need to rewrite parsing and writing code for each component that wants to use the message bus. 你什么也得不到 - 你需要为每个想要使用消息总线的组件重写解析和编写代码。 Thats wasted time and effort. 这浪费了时间和精力。

There is pain in mapping your C++ structures to JSON or protocol buffers. 将C ++结构映射到JSON或协议缓冲区很困难。 But it will make hooking into those messages using node.js or other things peeking into the message bus much easier later. 但是它会使用node.js或其他更容易窥视消息总线的消息来挂钩这些消息。

Personally I'd use protocol buffers - since they're more type safe. 就个人而言,我会使用协议缓冲区 - 因为它们更安全。 And there are differences between handling of various types in JSON libraries, because the JSON format is (intentionally) lax. 并且在JSON库中处理各种类型之间存在差异,因为JSON格式(故意)松散。 In particular I've found handling of long integers problematic. 特别是我发现长整数的处理有问题。

Typically I write a helper template struct for each class I need to convert - then conversion becomes a lot of boilerplate. 通常我会为每个需要转换的类编写一个帮助器模板结构 - 然后转换成为很多样板。 Something like 就像是

template<typename T> class ProtocolBufferHelper {
}

template<> class ProtocolBufferHelper<Address> {
  typedef address_buffer protocol_buffer_type;
  void writeToProtocolBuffer( const Address &a, address_buffer & buffer) {
    buffer.setNetwork(a.network);
    ...
  }
  ...
}

template<> class ProtocolBufferHelper<Message> {
  void writeToProtocolBuffer(  const Message &m, address_buffer & buffer) {
    ::writeToProtocolBuffer(buffer.getDestination(), m.destination);
    ::writeToProtocolBuffer(buffer.getSource(), m.source);
    ...
  }
}

template<typename T> void writeToProtocolBuffer( const T &value, ProtocolBufferHelper<T>::protocol_buffer_type & buffer ) {
   ProtocolBufferHelper<T>::writeToProtocolBuffer(value, buffer);
}

You'll have to forgive me for not remembering exactly what the protocol buffer syntax is in C++ (its been a while...). 你不得不原谅我没有记住C ++中协议缓冲区语法究竟是什么(它已经有一段时间......)。 but hopefully its enough to get you started. 但希望它足以让你开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM