Files
Eero caec44c57d Fix concurrent access issues with ConnectedClients
Replaced direct access to GameMain.Server.ConnectedClients with array snapshots in multiple server-side classes to prevent concurrent modification issues during parallel updates. Also updated PhysicsBody and LevelTrigger to avoid static/shared state in parallel contexts, improving thread safety and reliability.
2026-01-08 00:26:29 +08:00

47 lines
1.7 KiB
C#

using Barotrauma.Networking;
using System.Collections.Generic;
using System.Linq;
namespace Barotrauma
{
partial class MissionAction : EventAction
{
private static readonly HashSet<Mission> missionsUnlockedThisRound = new HashSet<Mission>();
public static void ResetMissionsUnlockedThisRound()
{
missionsUnlockedThisRound.Clear();
}
public static void NotifyMissionsUnlockedThisRound(Client client)
{
foreach (Mission mission in missionsUnlockedThisRound)
{
NotifyMissionUnlock(mission, client);
}
}
private static void NotifyMissionUnlock(Mission mission)
{
// Create snapshot to avoid concurrent access issues during parallel updates
var clients = GameMain.Server.ConnectedClients.ToArray();
foreach (Client client in clients)
{
NotifyMissionUnlock(mission, client);
}
}
private static void NotifyMissionUnlock(Mission mission, Client client)
{
IWriteMessage outmsg = new WriteOnlyMessage();
outmsg.WriteByte((byte)ServerPacketHeader.EVENTACTION);
outmsg.WriteByte((byte)EventManager.NetworkEventType.MISSION);
outmsg.WriteIdentifier(mission.Prefab.Identifier);
outmsg.WriteInt32(GameMain.GameSession?.Map?.Locations.IndexOf(mission.Locations[0]) ?? -1);
outmsg.WriteInt32(GameMain.GameSession?.Map?.Locations.IndexOf(mission.Locations[1]) ?? -1);
outmsg.WriteString(mission.Name.Value);
GameMain.Server.ServerPeer.Send(outmsg, client.Connection, DeliveryMethod.Reliable);
}
}
}