【发布时间】:2011-03-12 08:19:59
【问题描述】:
我将很久以前编写的 C++ 应用程序中的一些代码转换为 C#。在 C++ 中,我使用了一个有点缓冲的库,但由于缺乏 C# 知识,转换有点复杂。
当我查询我的应用程序时,我只是使用 ByteWriter 而不正确地转换任何值(就像 bf.Write(-1) 和 bf.Write("stringhere") 查询程序至少查询它,只是弄错了信息。当我正确地转换值(长、字节、短等)时,它会完全中断,查询应用程序甚至都看不到它了。
C++ 代码片段
void PlayerManager::BuildReplyInfo()
{
// Delete the old packet
g_ReplyInfo.Reset();
g_ReplyInfo.WriteLong(-1);
g_ReplyInfo.WriteByte(73);
g_ReplyInfo.WriteByte(g_ProtocolVersion.GetInt());
g_ReplyInfo.WriteString(iserver->GetName());
g_ReplyInfo.WriteString(iserver->GetMapName());
g_ReplyInfo.WriteString(gGameType);
}
C#代码
public static byte[] ConvertStringToByteArray(string str)
{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetBytes(str);
}
//-----------------------------------
while (true)
{
data = new byte[1024];
recv = socket.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message length is " + recv);
// If the length is 25 and the 5th byte is 'T' it is a A2S_INFO QUERY
if (recv == 25 && data[4] == 84)
{
Console.WriteLine("Source Engine Query!");
data = BuildReplyInformation();
socket.SendTo(data, 0, data.Length, SocketFlags.None, Remote);
}
}
}
public static byte[] BuildReplyInformation()
{
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
writer.Write((long)(-1));
writer.Write((byte)(73)); // Steam Version
writer.Write((byte)(15)); // Protocol
writer.Write(ConvertStringToByteArray("Minecraft Server\0")); // Hostname
writer.Write(ConvertStringToByteArray("Map Name\0")); // Map Name
writer.Write(ConvertStringToByteArray("tf\0")); // Game Directory
writer.Write(ConvertStringToByteArray("Minecraft Server\0")); // Game Description
writer.Write((short)(440));
writer.Write((byte)(15)); // Players
writer.Write((byte)(32)); // Max Players
writer.Write((byte)(0)); // Bots
writer.Write((byte)(100));
writer.Write((byte)(119)); // 108 Linux, 119 Windows
writer.Write((byte)(0)); // Password Boolean
writer.Write((byte)(01)); // Vac Secured
writer.Write(ConvertStringToByteArray("1.1.3.7\0"));
return stream.ToArray();
}
【问题讨论】:
-
顺便提一下,我正在尝试复制此数据包设计 (developer.valvesoftware.com/wiki/Server_queries#A2S_INFO),以便模仿游戏服务器。
-
您为什么不检查一下从您的 C++ 与您的 C# 应用程序与规范实际发送的内容? Steam 字符串在内部使用 ASCII 编码(至少在您的链接中是这样说的)。
-
嗯,C++ long 是 4 个字节,C# long 是 8 个字节。会不会是个问题?
-
马吕斯,这就是整个问题。一旦我将其更改为强制转换为整数而不是长整数,它就可以完美运行。您可以将此作为答案发布,以便我为您提供信用吗?
标签: c# c++ bytebuffer