对象到网络序列化 - 使用现有协议
我正在为用 C++ 编写的服务器程序编写客户端。很常见的是,所有网络协议都采用一种格式,可以轻松地将数据包复制到 C++ 结构中/从 C++ 结构中复制出来(1 字节数据包代码,然后根据数据包类型进行不同的安排)。
我可以在 C# 中做同样的事情,但是有没有更简单的方法,特别是考虑到很多数据是我想作为字符串使用的固定长度字符数组?或者我应该吸收它并根据需要转换类型?我研究过使用 ISerialized 接口,但它看起来并不像所需的那么低级别。
I'm writing a client for a server program written in C++. As is not unusual, all the networking protocol is in a format where packets can be easily memcopied into/out of a C++ structure (1 byte packet code, then different arrangements per packet type).
I could do the same thing in C#, but is there an easier way, especially considering lots of the data is fixed-length char arrays that I want to play with as strings? Or should I just suck it up and convert types as needed? I've looked at using the ISerializable interface, but it doesnt look as low level as is required.
我在 2004 年就此写了一篇文章,其中介绍了一些可用于将二进制流转换为 .NET 内存结构的选项。由于旧博客网站不再存在,我将其重新发布到我的新博客上。
http://taylorza.blogspot.com/2010/ 04/archive-struct-from-binary-data.html
基本上你有三个选择
当您考虑这些选项时,您还应该考虑字节顺序可能对您产生的影响。
作为示例,我将使用 IP 标头作为示例,因为在发帖时我正在处理原始 TCP 数据包。
您需要定义二进制数据将映射到的 .NET 结构。例如,IP 标头如下所示。
请注意,仅前两个选项需要 StructLayout 属性,当然您需要根据从服务器序列化的结构来设置打包。
因此,在 C/C++ 中,给定一个指向包含映射到 C/C++ 结构的数据字节的内存块的指针,您可以使用以下代码将数据块视为内存的结构块,其中 packet是内存的一个字节*。
使用 /unsafe 选项和上面定义的结构进行相同的操作,您可以使用以下代码。
封送处理选项如下所示
最后,您可以使用 BinaryReader 完全在托管代码中完成此操作。
正如我之前提到的,您可能需要考虑字节排序。例如,上面的代码并不完全正确,因为 IpHeader 不使用与 ReadInt16 假定的相同字节顺序。 ReadInt32 等。使用上述解决方案解决问题就像使用 IPAddress.NetworkToHostOrder 一样简单。
I wrote a post on this in 2004 which covers some of the options available for converting a binary stream to a .NET memory structure. I reposted it on my new blog since the old blog site no longer exists.
http://taylorza.blogspot.com/2010/04/archive-structure-from-binary-data.html
Basically you have three options
When you consider the options you should also take into account how the byte ordering might affect you.
As an example I will use the IP header as an example, since at the time of the post I was working with Raw TCP packets.
You need to define your .NET structure that the binary data will be mapped to. For example the IP Header looks like the following.
Note that the StructLayout attribute is only required for first two options, and of course you will need to set the packing as appropriate for the structure that is being serialized from the server.
So in C/C++, given a pointer to a block of memory that contains the data bytes that map to the C/C++ structure you can use the following bit of code to view the block of data as a structure piece of memory, where packet is a byte* to the memory.
Doing the same is C# using /unsafe option and the struct defined above you count use the following code.
The marshaling option would look like the following
And finally you can use the BinaryReader to do this entirely in managed code.
As I mentioned earlier, you might need to consider byte ordering. For example, the above code is not quite correct because the IpHeader does not use the same byte order as is assumed by ReadInt16. ReadInt32 etc. Resolving the issue with the above solution is as simple as using IPAddress.NetworkToHostOrder.