二进制序列化协议

发布于 2024-11-13 20:01:25 字数 738 浏览 3 评论 0原文

我有一个要求,我需要通过线路(TCP 上的二进制)在 2 个应用程序之间传输信息。一种是 Java 语言,另一种是 C++ 语言。我需要一个协议实现来在这两个应用程序之间传输对象。对象类存在于两个应用程序中(相应地映射)。我只需要在一侧进行一些编码方案,该方案在一侧保留对象表示,并可以在另一侧将其解码为完整的对象。

例如,

C++ 类

class Person
{
   int age;
   string name;
};

Java 类

class Person
{
   int age;
   String name;
}

C++ 编码

Person p;
p.age = 20;
p.name = "somename";
char[] arr = SomeProtocolEncoder.encode(p);
socket.send(arr);

Java 解码

byte[] arr = socket.read();
SomeProtocolIntermediateObject object = SomeProtocolDecoder.decode(arr);
Person p = (Person)ReflectionUtil.get(object);    

协议应该提供一些维护对象表示状态的中间对象,以便使用反射我可以稍后取回该对象。

I have a requirement where i need to transfer information through the wire(binary over tcp) between 2 applications. One is in Java and the other in C++. I need a protocol implementation to transfer objects between these 2 applications. The Object classes are present in both the applications (are mapped accordingly). I just need some encoding scheme on one side which retains the Object representation on one side and can be decoded on the other side as a complete Object.

For eg,

C++ class

class Person
{
   int age;
   string name;
};

Java class

class Person
{
   int age;
   String name;
}

C++ encoding

Person p;
p.age = 20;
p.name = "somename";
char[] arr = SomeProtocolEncoder.encode(p);
socket.send(arr);

Java decoding

byte[] arr = socket.read();
SomeProtocolIntermediateObject object = SomeProtocolDecoder.decode(arr);
Person p = (Person)ReflectionUtil.get(object);    

The protocol should provide some intermediate object which maintains the object representational state so that using reflection i can get back the object later.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

谁对谁错谁最难过 2024-11-20 20:01:25

查看 Google 的协议缓冲区

Check out Google's protocol buffers.

执笏见 2024-11-20 20:01:25

Thrift 就是您正在寻找的。您只需创建需要调用的结构和方法的定义,它就会完成所有繁重的工作。它有二进制协议(可选择使用 zlib 压缩或 ssl)。它可能会帮你缴税,但你没有从我那里听到这一点。

Thrift is what you're looking for. You just create a definition of the structs and methods you need to call and it does all of the heavy lifting. It's got binary protocols (optionally with zlib compression or ssl). It'll probably do your taxes but you didn't hear that from me.

荒人说梦 2024-11-20 20:01:25

您可能想查看这些项目并选择一个:

  1. 协议缓冲区
  2. Thrift
  3. Apache Avro

<一个href="http://floatingsun.net/articles/thrift-vs-protocol-buffers/" rel="nofollow">这里是我最近读到的 Thrift-vs-PB 比较。您还应该参考此 Wiki 了解这些库之间的性能比较。

You might want to check out these projects and choose one:

  1. Protocol Buffers
  2. Thrift
  3. Apache Avro

Here is a Thrift-vs-PB comparison I read recently. You should also refer to this Wiki for performance comparisons between these libraries.

逆蝶 2024-11-20 20:01:25

您可以查看 amef 协议,这是 amef 中 C++ 编码的示例就像,

    //Create a new AMEF object
    AMEFObject *object = new AMEFObject();

    //Add a child string object
    object->addPacket("This is the Automated Message Exchange Format Object property!!","adasd");   

    //Add a child integer object
    object->addPacket(21213);

    //Add a child boolean object
    object->addPacket(true);

    AMEFObject *object2 = new AMEFObject();
    string j = "This is the property of a nested Automated Message Exchange Format Object";
    object2->addPacket(j);
    object2->addPacket(134123);
    object2->addPacket(false);

    //Add a child character object
    object2->addPacket('d');

    //Add a child AMEF Object
    object->addPacket(object2);

    //Encode the AMEF obejct
    string str = new AMEFEncoder()->encode(object,false);

在java中解码就像,

    byte arr = amef encoded byte array value;
    AMEFDecoder decoder = new AMEFDecoder()
    AMEFObject object1 = AMEFDecoder.decode(arr,true);

协议实现有C++和Java的编解码器,有趣的部分是它可以以名称值对的形式保留对象类表示,
我在上一个项目中需要一个类似的协议,当我偶然发现这个协议时,我实际上已经根据我的要求修改了基础库。希望这对您有帮助。

You can check the amef protocol, an example of C++ encoding in amef would be like,

    //Create a new AMEF object
    AMEFObject *object = new AMEFObject();

    //Add a child string object
    object->addPacket("This is the Automated Message Exchange Format Object property!!","adasd");   

    //Add a child integer object
    object->addPacket(21213);

    //Add a child boolean object
    object->addPacket(true);

    AMEFObject *object2 = new AMEFObject();
    string j = "This is the property of a nested Automated Message Exchange Format Object";
    object2->addPacket(j);
    object2->addPacket(134123);
    object2->addPacket(false);

    //Add a child character object
    object2->addPacket('d');

    //Add a child AMEF Object
    object->addPacket(object2);

    //Encode the AMEF obejct
    string str = new AMEFEncoder()->encode(object,false);

Decoding in java would be like,

    byte arr = amef encoded byte array value;
    AMEFDecoder decoder = new AMEFDecoder()
    AMEFObject object1 = AMEFDecoder.decode(arr,true);

The Protocol implementation has codecs for both C++ and Java, the interesting part is it can retain object class representation in the form of name value pairs,
I required a similar protocol in my last project, when i incidentally stumbled upon this protocol, i had actually modified the base library according to my requirements. Hope this helps you.

皇甫轩 2024-11-20 20:01:25

普通的旧 ASN.1 怎么样?

它的优点是真正得到标准的支持(并被广泛使用)。问题是为每种语言找到一个编译器/运行时。

What about plain old ASN.1?

It would have the advantage of being really backed by a standard (and widely used). The problem is finding a compiler/runtime for each language.

九局 2024-11-20 20:01:25

这个项目是Java序列化协议的终极比较:

https://github.com/eishay/jvm- serializers/wiki

一些库还提供 C++ 序列化。
我亲自将 Python Construct 移植到了 Java。如果有兴趣,我很乐意启动一个到 C++ 和/或 JavaScript 的转换项目!

  1. http://construct.wikispaces.com/
  2. https://github.com/ZiglioNZ/construct

This project is the ultimate comparison of Java serialization protocols:

https://github.com/eishay/jvm-serializers/wiki

Some libraries also provide C++ serialization.
I've personally ported Python Construct to Java. If there's some interest I'll be happy to start a conversion project to C++ and/or JavaScript!

  1. http://construct.wikispaces.com/
  2. https://github.com/ZiglioNZ/construct
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文