好的,我需要一些帮助。当一个需要可靠性的数据包被发送出去时,它会被传递到 ReliabilityLayer。ReliabilityLayer 从那里将该数据包添加到列表中,然后将其一次写入 SocketLayer。ReliabilityLayer 然后产生一个有 2 个定时器的线程。当数据包仍在列表中时,第一个计时器每 250 毫秒连续将数据包发送到 SocketLayer。第二个定时器是超时时间。它在 2 秒后抛出异常。ReliabilityLayer 挂钩到数据包接收事件,当返回的 ACK 数据包包含 ReliabilityLayer 数据包列表中数据包的校验和时,它应该将其删除以允许线程退出。问题是多线程......跨线程访问列表会给我随机空指针和其他问题。所以我要么以某种方式使其线程安全,要么重新考虑整个事情。我想知道是否有人可以帮助我?谢谢
public void Write(NetworkPacket packet, ClientInfo client, Action<byte[], EndPoint> action)
{
if (CSL)
throw new Exception("ReliabilityLayer loaded for client use.");
if (!packet.Command.RequiresReliability())
throw new ArgumentException("Packet does not require reliability.");
//Add the packet to the reliability list
packetList.Add(packet);
//Send the packet to the socket layer.
action.Invoke(packet.RawData, client.EndPoint);
new Thread(() =>
{
Stopwatch timeout = new Stopwatch();
Stopwatch timer = new Stopwatch();
timer.Start();
timeout.Start();
while (packetList.Contains(packet))
{
//Still no response from the remote connection -> send another packet
if (timer.ElapsedMilliseconds > 256)
{
action.Invoke(packet.RawData, client.EndPoint);
timer.Restart();
}
//No response after 2 seconds -> throw exception
if (timeout.ElapsedMilliseconds > 2048)
{
throw new Exception("Client has not responded to the request.");
}
}
}).Start();
}
private void ssl_OnPacketReceived(object sender, ServerPacketEventArgs e)
{
if (e.Packet.Command != Command.Ack)
return;
//Find matching packet in the packetList
NetworkPacket packet = packetList.Find(pkt => pkt.Checksum == e.Packet.Data[0]); //e.Packet.Data[0] is the checksum of the packet that was send out.
if (packet != null)
{
//Remove it to allow thread to exit
packetList.Remove(packet);
}
}