回复格式为您提供回复数据包中字段的顺序和类型,基本上就像一个结构。您可以使用一个类BinaryReader
来读取数据包中的字节组并将它们解释为适当的数据类型。
你如何得到回应?
- 如果它已经在流中,那么您已经设置好了。
- 如果它在字节数组中,请将其包装在
MemoryStream
第一个中。我认为UdpClient
这样做。
然后,BinaryReader
使用流构造一个。请记住,流和阅读器都需要Disposed
。
BinaryReader reader = new BinaryReader(stream);
您现在可以调用 reader 的方法,如ReadByte
等ReadInt32
,使用与字段类型对应的方法依次从响应中读取每个字段。流在读取时会更新其内部偏移量,因此您会自动从响应缓冲区中的正确位置读取连续的字段。 BinaryReader
已经有适用于 Steam 数据包中使用的五种非字符串类型的方法:
byte: ReadByte
short: ReadInt16
long: ReadInt32
float: ReadSingle
long long: ReadUInt64
(是的,里面有一个 U;Valve 页面上说这些是未签名的)
string
有点棘手,因为BinaryReader
还没有方法来读取 Valve 指定的格式的字符串(以空字符结尾的 UTF-8),所以你必须自己做,一个字节一个字节。为了使它看起来尽可能像任何其他BinaryReader
方法,您可以编写一个扩展方法(未经测试的代码;这是它的要点):
public static string ReadSteamString(this BinaryReader reader)
{
// To hold the list of bytes making up the string
List<byte> str = new List<byte>();
byte nextByte = reader.ReadByte();
// Read up to and including the null terminator...
while (nextByte != 0)
{
// ...but don't include it in the string
str.Add(nextByte);
nextByte = reader.ReadByte();
}
// Interpret the result as a UTF-8 sequence
return Encoding.UTF8.GetString(str.ToArray());
}
一些示例用法,以及您提供的响应数据包:
// Returns -1, corresponding to FF FF FF FF
int header = reader.ReadInt32();
// Returns 0x49, for packet type
byte packetType = reader.ReadByte();
// Returns 15, for version
byte ver = reader.ReadByte();
// Returns "Lokalen TF2 #03 All maps | Vanilla"
string serverName = reader.ReadSteamString();
// Returns "cp_well"
string mapName = reader.ReadSteamString();
// etc.
您可以使用类似的代码来创建您的请求数据包,使用 aBinaryWriter
而不是手动组装单个字节值。