// // GameSemaphores.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. namespace NosSmooth.Game; /// /// Holds information about semaphores for synchornizing the game packet data. /// internal class GameSemaphores { private Dictionary _semaphores; /// /// Initializes a new instance of the class. /// public GameSemaphores() { _semaphores = new Dictionary(); foreach (var type in Enum.GetValues(typeof(GameSemaphoreType)).Cast()) { _semaphores[type] = new SemaphoreSlim(1, 1); } } /// /// Acquire the given semaphore. /// /// The semaphore type. /// The cancellation token for cancelling the operation. /// A task that may or may not have succeeded. public async Task AcquireSemaphore(GameSemaphoreType semaphoreType, CancellationToken ct = default) { await _semaphores[semaphoreType].WaitAsync(ct); } /// /// Release the acquired semaphore. /// /// The semaphore type. public void ReleaseSemaphore(GameSemaphoreType semaphoreType) { _semaphores[semaphoreType].Release(); } }