对象到网络序列化 - 使用现有协议

发布于 08-29 08:19 字数 238 浏览 11 评论 0原文

我正在为用 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.

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

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

发布评论

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

评论(1

奶茶白久2024-09-05 08:19:46

我在 2004 年就此写了一篇文章,其中介绍了一些可用于将二进制流转换为 .NET 内存结构的选项。由于旧博客网站不再存在,我将其重新发布到我的新博客上。

http://taylorza.blogspot.com/2010/ 04/archive-struct-from-binary-data.html

基本上你有三个选择

  1. 在 C# 中使用 C++ 风格的内存指针,这需要 /unsafe 开关
  2. 使用 .NET 封送处理来分配非托管内存块,复制字节到非托管内存,然后使用 Marshal.PtrToStructure 将数据封送回托管堆,将其映射到结构中。
  3. 使用 BinaryReader 手动读取字节流并将数据打包到结构中。就我个人而言,这是我的首选。

当您考虑这些选项时,您还应该考虑字节顺序可能对您产生的影响。

作为示例,我将使用 IP 标头作为示例,因为在发帖时我正在处理原始 TCP 数据包。

您需要定义二进制数据将映射到的 .NET 结构。例如,IP 标头如下所示。

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct IpHeader
{  
  public byte VerLen;  
  public byte TOS;  
  public short TotalLength;   
  public short ID;  
  public short Offset;  
  public byte TTL;  
  public byte Protocol;  
  public short Checksum;  
  public int SrcAddr;  
  public int DestAddr;
}

请注意,仅前两个选项需要 StructLayout 属性,当然您需要根据从服务器序列化的结构来设置打包。

因此,在 C/C++ 中,给定一个指向包含映射到 C/C++ 结构的数据字节的内存块的指针,您可以使用以下代码将数据块视为内存的结构块,其中 packet是内存的一个字节*。

IpHeader *pHeader = (IpHeader*)packet;

使用 /unsafe 选项和上面定义的结构进行相同的操作,您可以使用以下代码。

IpHeader iphdr;
unsafe
{  
  fixed ( byte *pData = packet)  
  {    
    iphdr = *(IpHeader*)pData;  
  }
}
//Use iphdr...

封送处理选项如下所示

IntPtr pIP = Marshal.AllocHGlobal( len );
Marshal.Copy( packet, 0, pIP, len );
iphdr = (IpHeader)Marshal.PtrToStructure( pIP, typeof(IpHeader) );
Marshal.FreeHGlobal( pIP );

最后,您可以使用 BinaryReader 完全在托管代码中完成此操作。

MemoryStream stm = new MemoryStream( packet, 0, len );
BinaryReader rdr = new BinaryReader( stm );

iphdr.VerLen = rdr.ReadByte();
iphdr.TOS = rdr.ReadByte();
iphdr.TotalLength = rdr.ReadInt16();
iphdr.ID = rdr.ReadInt16();
iphdr.Offset = rdr.ReadInt16();
iphdr.TTL = rdr.ReadByte();
iphdr.Protocol = rdr.ReadByte();
iphdr.Checksum = rdr.ReadInt16();
iphdr.SrcAddr = rdr.ReadInt32();
iphdr.DestAddr = rdr.ReadInt32();

正如我之前提到的,您可能需要考虑字节排序。例如,上面的代码并不完全正确,因为 IpHeader 不使用与 ReadInt16 假定的相同字节顺序。 ReadInt32 等。使用上述解决方案解决问题就像使用 IPAddress.NetworkToHostOrder 一样简单。

iphdr.VerLen = rdr.ReadByte();
iphdr.TOS = rdr.ReadByte();
iphdr.TotalLength = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
iphdr.ID = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
iphdr.Offset = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
iphdr.TTL = rdr.ReadByte();
iphdr.Protocol = rdr.ReadByte();
iphdr.Checksum = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
iphdr.SrcAddr = IPAddress.NetworkToHostOrder(rdr.ReadInt32());
iphdr.DestAddr = IPAddress.NetworkToHostOrder(rdr.ReadInt32());

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

  1. Use C++ style memory pointers in C# which requires the /unsafe switch
  2. Use the .NET marshaling to allocate an unmanaged block of memory, copy the bytes to the unmanaged memory and then use Marshal.PtrToStructure to marshal the data back to the managed heap mapping it into your structure.
  3. Use a BinaryReader to manualy read the byte stream and pack the data into the structure. Personally, this was my preferred option.

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.

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct IpHeader
{  
  public byte VerLen;  
  public byte TOS;  
  public short TotalLength;   
  public short ID;  
  public short Offset;  
  public byte TTL;  
  public byte Protocol;  
  public short Checksum;  
  public int SrcAddr;  
  public int DestAddr;
}

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.

IpHeader *pHeader = (IpHeader*)packet;

Doing the same is C# using /unsafe option and the struct defined above you count use the following code.

IpHeader iphdr;
unsafe
{  
  fixed ( byte *pData = packet)  
  {    
    iphdr = *(IpHeader*)pData;  
  }
}
//Use iphdr...

The marshaling option would look like the following

IntPtr pIP = Marshal.AllocHGlobal( len );
Marshal.Copy( packet, 0, pIP, len );
iphdr = (IpHeader)Marshal.PtrToStructure( pIP, typeof(IpHeader) );
Marshal.FreeHGlobal( pIP );

And finally you can use the BinaryReader to do this entirely in managed code.

MemoryStream stm = new MemoryStream( packet, 0, len );
BinaryReader rdr = new BinaryReader( stm );

iphdr.VerLen = rdr.ReadByte();
iphdr.TOS = rdr.ReadByte();
iphdr.TotalLength = rdr.ReadInt16();
iphdr.ID = rdr.ReadInt16();
iphdr.Offset = rdr.ReadInt16();
iphdr.TTL = rdr.ReadByte();
iphdr.Protocol = rdr.ReadByte();
iphdr.Checksum = rdr.ReadInt16();
iphdr.SrcAddr = rdr.ReadInt32();
iphdr.DestAddr = rdr.ReadInt32();

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.

iphdr.VerLen = rdr.ReadByte();
iphdr.TOS = rdr.ReadByte();
iphdr.TotalLength = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
iphdr.ID = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
iphdr.Offset = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
iphdr.TTL = rdr.ReadByte();
iphdr.Protocol = rdr.ReadByte();
iphdr.Checksum = IPAddress.NetworkToHostOrder(rdr.ReadInt16());
iphdr.SrcAddr = IPAddress.NetworkToHostOrder(rdr.ReadInt32());
iphdr.DestAddr = IPAddress.NetworkToHostOrder(rdr.ReadInt32());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文