我不会占用你的大部分时间。但我正在开发一个游戏服务器,有时当数据被发送(数据包)时,我得到了这个错误"索引超出了数组的范围"。
这就是我的代码的样子:
internal void HandleConnectionData(ref byte[] data)
{
if (data[0] == 64)
{
int pos = 0;
while (pos < data.Length)
{
try
{
int MessageLength = Base64Encoding.DecodeInt32(new byte[] { data[pos++], data[pos++], data[pos++] });
uint MessageId = Base64Encoding.DecodeUInt32(new byte[] { data[pos++], data[pos++] });
byte[] Content = new byte[MessageLength - 2];
for (int i = 0; i < Content.Length; i++)
{
Content[i] = data[pos++];
}
ClientMessage Message = new ClientMessage(MessageId, Content);
if (MessageHandler == null)
{
InitHandler(); //Never ever register the packets BEFORE you receive any data.
}
//DateTime PacketMsgStart = DateTime.Now;
try
{
MessageHandler.HandleRequest(Message);
}
catch (Exception e) { Logging.LogPacketException(Message.ToString(), e.ToString()); }
}
catch (Exception e)
{
Logging.HandleException(e, "packet handling");
Disconnect();
}
}
}
else
{
Connection.SendData(CrossdomainPolicy.GetXmlPolicy());
Connection.Disposes();
}
}
有什么想法吗?谢谢。