From: AeonLucid Date: Tue, 29 Sep 2020 03:37:39 +0000 (+0200) Subject: Change client id generation X-Git-Tag: v1.1.0~1^2~27 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=23e0a1ed5ed45650e93ee3e37a75444b0726ead0;p=rhonda%2Fimpostor.git Change client id generation --- diff --git a/src/Impostor.Server/Net/Manager/ClientManager.cs b/src/Impostor.Server/Net/Manager/ClientManager.cs index 28298c5..7e4b64e 100644 --- a/src/Impostor.Server/Net/Manager/ClientManager.cs +++ b/src/Impostor.Server/Net/Manager/ClientManager.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.Threading; using Hazel; using Impostor.Server.Exceptions; using Microsoft.Extensions.Logging; @@ -22,32 +23,19 @@ namespace Impostor.Server.Net.Manager _idLast = 0; } - // No idea what a good way for this is. private int NextId() { - lock (_idLock) + var clientId = Interlocked.Increment(ref _idLast); + if (clientId < 1) { - // 3 Attempts. - for (var i = 0; i < 3; i++) - { - // It is important that ids start from 1, a 0 id causes issues. - var result = ++_idLast; - - if (_idLast == int.MaxValue) - { - _idLast = 0; - } - - if (_clients.ContainsKey(result)) - { - continue; - } - - return result; - } + // Super rare but reset the _idLast because of overflow. + _idLast = 0; - throw new AmongUsException("Unable to generate a client id."); + // And get a new id. + clientId = Interlocked.Increment(ref _idLast); } + + return clientId; } public void Create(string name, Connection connection)