WIP Make networking code thread-safe and refactor update ID

Replaces direct increments of LastClientListUpdateID with a thread-safe IncrementLastClientListUpdateID method and uses Interlocked for atomic operations. Refactors EntitySpawner to lock access to the spawn/remove queue for thread safety. Updates INetSerializableStruct to use concurrent collections for cached variables and type behaviors, improving thread safety in networking code.
This commit is contained in:
Eero
2025-12-28 13:10:17 +08:00
parent 31812d524d
commit c5fa49405f
8 changed files with 111 additions and 51 deletions
@@ -1,6 +1,7 @@
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Threading;
namespace Barotrauma.Networking
{
@@ -186,10 +187,19 @@ namespace Barotrauma.Networking
{
protected const int MaxSubNameLengthInErrorMessages = 16;
private int lastClientListUpdateID;
public UInt16 LastClientListUpdateID
{
get;
set;
get => (UInt16)Interlocked.CompareExchange(ref lastClientListUpdateID, 0, 0);
set => Interlocked.Exchange(ref lastClientListUpdateID, value);
}
/// <summary>
/// Thread-safe increment of LastClientListUpdateID
/// </summary>
public void IncrementLastClientListUpdateID()
{
Interlocked.Increment(ref lastClientListUpdateID);
}
public abstract bool IsServer { get; }