c ++ deserialize通过UDP从c#应用程序发送的对象 [英] c++ deserialize object sent from c# application via UDP

查看:137
本文介绍了c ++ deserialize通过UDP从c#应用程序发送的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 c#中有一个应用程序,它通过UDP连接到另一个以 c ++ 编写的应用程序。我更喜欢一个高性能的解决方案,因为我正在测量事件客户端,一些处理服务器端和客户端处理完成的响应之间的时间。基本上:往返延迟。



我有一个对象在c#,例如:

 code> public class Package {

public Package(){}

public float DeliveryTime = 1.5;
public int NumberOfItems = 2

}

我的包到字节数组:

 包package = new Package(); 
var serializedPackage = ObjectToByteArray(package);

接下来,我通过UDP发送 serializedPackage (我知道,不保证交付或订单,但我不需要那些保证)。

  client.Send(serializedPackage ,serializedPackage。Length,ip); 

我的c ++应用程序接收到UDP消息...
问题是:



我现在如何将接收到的字节数组转换为可用的对象,并保留属性值?


$ b b

我需要反序列化成一些类Package c ++,然后使用属性...例如一些sudo代码:

  DeserializedPackage deserializedPackage = someDeserializeFunction(bytes); 
if(deserializedPackage.NumberOfItems> 0){
cout< deserializedPackage.DeliveryTime;
}

我从哪里开始?这是可能的吗?



我很熟悉C#,但是对于c ++来说是新手,所以不要混用c和c ++。此外,我不要要使用 Boost 或其他图书馆。



也打开建议,例如将你的包对象转换为JSON并发送,然后反序列化你的JSON和填充一个c ++对象称为包。

解决方案<



协议缓冲区是一种方法,这里有一个类似的问题,关于做什么

c ++和java之间的href =http://stackoverflow.com/a/3986733/1734730>。建议使用Google的PB,但还有其他的。



但是,我最喜欢的以一种不可知的语言方式序列化对象的方法是JSON。很容易理解与可能是最快的最干净的事情可以实现。所以我会跟着去,除非你有任何证据表现反对。你似乎已经知道如何做到这一点?


I have an application in c# which connects to another application, written in c++, via UDP. I'd prefer a performant solution as I am looking to measure the time between an event client-side, some processing server-side and a response of processing complete back on the client-side. Essentially: Round trip delay.

I have an object in c#, for example:

public class Package{

    public Package(){}

    public float DeliveryTime = 1.5;
    public int NumberOfItems = 2

}

I then serialize my package to a byte array:

Package package = new Package();
var serializedPackage = ObjectToByteArray(package);

Next, I send my serializedPackage via UDP (I know, no guarantee of delivery or order, but I don't need those guarantees).

client.Send(serializedPackage , serializedPackage .Length, ip);

My c++ application then received the UDP message... the question is:

How do I now convert my received byte array back into a usable object with property values maintained?

I need to deserialize into some class Package c++ side and then use the properties... for example some sudo code:

DeserializedPackage deserializedPackage = someDeserializeFunction(bytes);
if(deserializedPackage.NumberOfItems > 0){
    cout << deserializedPackage.DeliveryTime;
}

Where do I start? Is this even possible?

I am well versed with C#, but absolutely new to c++, so no mixing c and c++, please. Also, I do not want to use Boost or another library.

I am also open to suggestions such as "convert your package object to JSON and send that. Then deserialize your JSON and populate a c++ object called package."

解决方案

Your problem is the same, how to serialize types between two languages.

Protocol buffers are one way, here is a similar question about what to do between c++ and java. Google's PBs are suggested there, although there are others.

However, my favorite way to serialize objects in a language agnostic way is JSON. It's easy to reason with and probably the quickest cleanest thing you can implement. So I'd go with that first, unless you have any evidenced performance objection. You seem to already know how to do this?

这篇关于c ++ deserialize通过UDP从c#应用程序发送的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆