C++ 中的二进制序列化/反序列化和 C#

发布于 2024-10-11 22:42:27 字数 585 浏览 3 评论 0原文

我正在开发一个具有两个组件的分布式应用程序。一种是用标准 C++ 编写的(非托管 C++,在 Linux 平台上运行),另一种是用 C# 编写的。两者都通过消息总线进行通信。

我遇到的情况是,我需要将对象从 C++ 传递到 C# 应用程序,为此我需要在 C++ 中序列化这些对象,并在 C# 中反序列化它们(类似于 .NET 中的封送/取消封送)。我需要以二进制而不是 XML 的形式执行此序列化(由于性能原因)。

当两端都用 C++ 实现时,我使用 Boost.Serialization 来执行此操作,但现在我在一端有一个 .NET 应用程序,Boost.Serialization 不是可行的解决方案。

我正在寻找一种解决方案,允许我跨 C++ 和 .NET 边界执行(反)序列化,即跨平台二进制序列化

我知道我可以在 C++ dll 中实现(反)序列化代码,并在 .NET 应用程序中使用 P/Invoke,但我想将其保留为最后的手段。

另外,我想知道如果我使用像 gzip 这样的标准,会有效吗? gzip 有其他替代方案吗?它们有什么优点/缺点?

谢谢

I am working on a distributed application which has two components. One is written in standard C++ (not managed C++ and running on a Linux platform) and the other one is written in C#. Both are communicating via a message bus.

I have a situation in which I need to pass objects from C++ to C# application and for this I need to serialize those objects in C++ and de-serialize them in C# (something like marshaling/un-marshaling in .NET). I need to perform this serialization in binary and not in XML (due to performance reasons).

I have used Boost.Serialization to do this when both ends were implemented in C++ but now that I have a .NET application on one end, Boost.Serialization is not a viable solution.

I am looking for a solution that allows me to perform (de)serialization across C++ and .NET boundary i.e., cross platform binary serialization.

I know I can implement the (de)serialization code in a C++ dll and use P/Invoke in the .NET application, but I want to keep that as a last resort.

Also, I want to know if I use some standard like gzip, will that be efficient? Are there any other alternatives to gzip? What are the pros/cons of them?

Thanks

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

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

发布评论

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

评论(3

魂牵梦绕锁你心扉 2024-10-18 22:42:27

我会推荐 Protocol Buffers,这是 Google 自己的序列化库。它具有 .Net、C++ 和 Java 序列化器。大多数实现也相当快。

http://code.google.com/p/protobuf/

I would recommend Protocol Buffers, which is Googles own serialization library. It has both .Net, C++ and Java serializers. Most implementations are also quite fast.

http://code.google.com/p/protobuf/

云朵有点甜 2024-10-18 22:42:27

gzip 不会直接帮助序列化 - 它只会(尝试)缩小流。这可能有帮助,也可能没有帮助,具体取决于流中重复数据的数量。对于文本较少的密集数据,我发现 gzip 增加了有效负载的大小。

就我个人而言,我会在这里查看协议缓冲区(但我有偏见,因为我许多扩展的作者之一)。您通常(但并非总是)使用基本语言(.proto 文件)定义消息,并运行特定于语言的工具来生成类。性能非常好 - 专注于 .NET,它可以远远超过内置序列化器(1 2 3 )

gzip won't directly help with serialization - it will just (attempt to) shrink a stream. This may help, or not, depending on the amount of duplicated data in the stream. For dense data with little text, I've seen gzip increase the size of the payload.

Personally I would look at protocol buffers here (but I'm biased, as I'm one of the authors of the many extensions). You typically (but not always) define the messages in a basic language (a .proto file), and run language-specific tools to generate the classes. Performance is very good - focusing on .NET it can far exceed the inbuilt serializers (1 2 3 )

盛夏已如深秋| 2024-10-18 22:42:27

另一种可能性是 Thrift,它有更多的后端,如果需要的话,可以提供网络所需的大部分代码通信 - 如果您想横向扩展。

如果你只想要简单的对象序列化,我会看看 json.org 有很多 C++ / .NET 实现。

Another possibility would be Thrift, it has even more backends and if necessary provides a good part of the code required for network communication - in case you want to scale out.

If you only want easy object serialization I would have a look at json.org There are plenty of C++ / .NET implementations around.

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