Skip to content

Commit 0d5eb26

Browse files
Micky5991dr1zzle
authored and
dr1zzle
committed
Merge CreateClient and RegisterClient into PrepareClient
1 parent ea0c08c commit 0d5eb26

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

JustAnotherVoiceChat.Server.Dummy/src/ServerHandler.cs

+1-12
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,7 @@ public ServerHandler(IDummyClientRepository clientRepository, IVoiceWrapper voic
6161

6262
public IVoiceClient PrepareClient()
6363
{
64-
var voiceClient = CreateClient(1);
65-
if (voiceClient == null)
66-
{
67-
return null;
68-
}
69-
70-
if (!RegisterClient(voiceClient))
71-
{
72-
return null;
73-
}
74-
75-
return voiceClient;
64+
return PrepareClient(1);
7665
}
7766

7867
public void TriggerClientConnect(ushort handle)

JustAnotherVoiceChat.Server.GTMP/src/Elements/Server/GtmpVoiceServer.Players.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,11 @@ public IGtmpVoiceClient GetVoiceClient(Client player)
4141

4242
private IGtmpVoiceClient RegisterPlayer(Client player)
4343
{
44-
var voiceClient = CreateClient(player);
44+
var voiceClient = PrepareClient(player);
4545
if (voiceClient == null)
4646
{
4747
return null;
4848
}
49-
50-
if (!RegisterClient(voiceClient))
51-
{
52-
return null;
53-
}
5449

5550
OnClientPrepared?.Invoke(voiceClient);
5651

JustAnotherVoiceChat.Server.Wrapper/src/Elements/Server/VoiceServer.Clients.cs

+18-8
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,45 @@ public partial class VoiceServer<TClient, TIdentifer> where TClient : IVoiceClie
3838
{
3939
private readonly ConcurrentDictionary<ushort, IVoiceClient> _clients = new ConcurrentDictionary<ushort, IVoiceClient>();
4040
private readonly object _voiceHandleGenerationLock = new object();
41+
42+
protected TClient PrepareClient(TIdentifer identifer)
43+
{
44+
var client = CreateClient(identifer);
45+
if (ReferenceEquals(client, default(TClient)) || !RegisterClient(client))
46+
{
47+
return default(TClient);
48+
}
49+
50+
return client;
51+
}
4152

42-
protected TClient CreateClient(TIdentifer identifer)
53+
private TClient CreateClient(TIdentifer identifer)
4354
{
44-
if (!CreateVoiceHandle(out var voiceHandle))
55+
var voiceHandle = CreateVoiceHandle();
56+
if (voiceHandle == VoiceHandle.Empty)
4557
{
4658
return default(TClient);
4759
}
4860

4961
return _factory.MakeClient(identifer, this, voiceHandle);
5062
}
5163

52-
protected bool CreateVoiceHandle(out VoiceHandle voiceHandle)
64+
private VoiceHandle CreateVoiceHandle()
5365
{
5466
lock (_voiceHandleGenerationLock)
5567
{
5668
try
5769
{
58-
voiceHandle = CreateFreeVoiceHandle();
59-
return true;
70+
return CreateFreeVoiceHandle();
6071
}
6172
catch (InvalidOperationException)
6273
{
63-
voiceHandle = new VoiceHandle();
64-
return false;
74+
return VoiceHandle.Empty;
6575
}
6676
}
6777
}
6878

69-
protected bool RegisterClient(IVoiceClient client)
79+
private bool RegisterClient(IVoiceClient client)
7080
{
7181
lock (_voiceHandleGenerationLock)
7282
{

0 commit comments

Comments
 (0)