Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

using System.Collections.Generic;
using System.Threading.Tasks;
using JustAnotherVoiceChat.Server.Wrapper.Delegates;
using JustAnotherVoiceChat.Server.Wrapper.Elements.Models;
using JustAnotherVoiceChat.Server.Wrapper.Enums;
Expand All @@ -38,7 +39,7 @@ namespace JustAnotherVoiceChat.Server.Wrapper.Tests.Fakes
{
public class VoiceWrapperEventInvoker : IVoiceWrapper
{

private const int _taskWaitTime = 5;
public readonly Mock<IVoiceWrapper> Mock = new Mock<IVoiceWrapper>();

private NativeDelegates.ClientConnectingCallback _clientConnecting;
Expand All @@ -58,36 +59,43 @@ public bool InvokeClientConnectingCallback(ushort handle, string teamspeakId)
public void InvokeClientConnectedCallback(ushort handle)
{
_clientConnected(handle);
Task.Delay(_taskWaitTime).Wait();
}

public void InvokeClientRejectedCallback(ushort handle, int statusCode)
{
_clientRejected(handle, statusCode);
Task.Delay(_taskWaitTime).Wait();
}

public void InvokeClientDisconnectedCallback(ushort handle)
{
_clientDisconnected(handle);
Task.Delay(_taskWaitTime).Wait();
}

public void InvokeClientTalkingChangedCallback(ushort handle, bool newStatus)
{
_clientTalkingChanged(handle, newStatus);
Task.Delay(_taskWaitTime).Wait();
}

public void InvokeClientSpeakersMuteChangedCallback(ushort handle, bool newStatus)
{
_clientSpeakersMuteChanged(handle, newStatus);
Task.Delay(_taskWaitTime).Wait();
}

public void InvokeClientMicrophoneMuteChangedCallback(ushort handle, bool newStatus)
{
_clientMicrophoneMuteChanged(handle, newStatus);
Task.Delay(_taskWaitTime).Wait();
}

public void InvokeLogMessageCallback(string message, int logLevel)
{
_logMessage(message, logLevel);
Task.Delay(_taskWaitTime).Wait();
}

public void RegisterClientConnectingCallback(NativeDelegates.ClientConnectingCallback callback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/

using System;
using System.Threading.Tasks;
using JustAnotherVoiceChat.Server.Wrapper.Elements.Models;
using JustAnotherVoiceChat.Server.Wrapper.Enums;
using JustAnotherVoiceChat.Server.Wrapper.Tests.Fakes;
Expand Down Expand Up @@ -98,13 +99,13 @@ public void ClientMuteNativeChangesWillSetClientProperties()

_voiceWrapper.InvokeClientMicrophoneMuteChangedCallback(1, true);
_voiceWrapper.InvokeClientSpeakersMuteChangedCallback(1, true);

Assert.IsFalse(_voiceClient.Microphone);
Assert.IsFalse(_voiceClient.Speakers);

_voiceWrapper.InvokeClientMicrophoneMuteChangedCallback(1, false);
_voiceWrapper.InvokeClientSpeakersMuteChangedCallback(1, false);

Assert.IsTrue(_voiceClient.Microphone);
Assert.IsTrue(_voiceClient.Speakers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* SOFTWARE.
*/

using System;
using JustAnotherVoiceChat.Server.Wrapper.Elements.Models;
using JustAnotherVoiceChat.Server.Wrapper.Enums;
using JustAnotherVoiceChat.Server.Wrapper.Interfaces;
Expand All @@ -34,7 +33,6 @@ namespace JustAnotherVoiceChat.Server.Wrapper.Elements.Server
{
public partial class VoiceServer<TClient, TIdentifier> where TClient : IVoiceClient
{

private bool OnClientConnectingFromVoice(ushort handle, string teamspeakId)
{
Log(LogLevel.Trace, $"OnClientConnectingFromVoice({handle}, {teamspeakId})");
Expand All @@ -47,115 +45,63 @@ private bool OnClientConnectingFromVoice(ushort handle, string teamspeakId)
});
}

private void OnClientConnectedFromVoice(ushort handle)
private async void OnClientConnectedFromVoice(ushort handle)
{
Log(LogLevel.Trace, $"OnClientConnectedFromVoice({handle})");
RunWhenClientValid(handle, client =>
await RunWhenClientValidAsync(handle, async client =>
{
InvokeProtectedEvent(() => OnClientConnected?.Invoke(client));
await InvokeProtectedEventAsync(() => OnClientConnected?.Invoke(client));
});
}

private void OnClientRejectedFromVoice(ushort handle, int statusCode)
private async void OnClientRejectedFromVoice(ushort handle, int statusCode)
{
Log(LogLevel.Trace, $"OnClientRejectedFromVoice({handle}, {statusCode})");
RunWhenClientValid(handle, client =>
await RunWhenClientValidAsync(handle, async client =>
{
InvokeProtectedEvent(() => OnClientRejected?.Invoke(client, (StatusCode) statusCode));
await InvokeProtectedEventAsync(() => OnClientRejected?.Invoke(client, (StatusCode) statusCode));
});
}

private void OnClientDisconnectedFromVoice(ushort handle)
private async void OnClientDisconnectedFromVoice(ushort handle)
{
Log(LogLevel.Trace, $"OnClientDisconnectedFromVoice({handle})");
RunWhenClientValid(handle, client =>
await RunWhenClientValidAsync(handle, async client =>
{
InvokeProtectedEvent(() => OnClientDisconnected?.Invoke(client));
await InvokeProtectedEventAsync(() => OnClientDisconnected?.Invoke(client));
});
}

private void OnClientTalkingStatusChangedFromVoice(ushort handle, bool newStatus)
private async void OnClientTalkingStatusChangedFromVoice(ushort handle, bool newStatus)
{
Log(LogLevel.Trace, $"OnClientTalkingStatusChangedFromVoice({handle}, {newStatus})");
RunWhenClientValid(handle, client =>
await RunWhenClientValidAsync(handle, async client =>
{
InvokeProtectedEvent(() => OnClientTalkingChanged?.Invoke(client, newStatus));
await InvokeProtectedEventAsync(() => OnClientTalkingChanged?.Invoke(client, newStatus));
});
}

private void OnClientSpeakersMuteChangedFromVoice(ushort handle, bool newStatus)
private async void OnClientSpeakersMuteChangedFromVoice(ushort handle, bool newStatus)
{
Log(LogLevel.Trace, $"OnClientSpeakersMuteChangedFromVoice({handle}, {newStatus})");
RunWhenClientValid(handle, client =>
await RunWhenClientValidAsync(handle, async client =>
{
InvokeProtectedEvent(() => OnClientSpeakersMuteChanged?.Invoke(client, newStatus));
await InvokeProtectedEventAsync(() => OnClientSpeakersMuteChanged?.Invoke(client, newStatus));
});
}

private void OnClientMicrophoneMuteChangedFromVoice(ushort handle, bool newStatus)
private async void OnClientMicrophoneMuteChangedFromVoice(ushort handle, bool newStatus)
{
Log(LogLevel.Trace, $"OnClientMicrophoneMuteChangedFromVoice({handle}, {newStatus})");
RunWhenClientValid(handle, client =>
{
InvokeProtectedEvent(() => OnClientMicrophoneMuteChanged?.Invoke(client, newStatus));
});
}

private void OnLogMessageFromVoice(string message, int loglevel)
{
Log((LogLevel) loglevel, message);
}



internal T RunWhenClientValid<T>(ushort handle, Func<TClient, T> callback)
{
var client = GetVoiceClient(handle);

if (client == null)
{
return default(T);
}

return callback(client);
}

internal void RunWhenClientValid(ushort handle, Action<TClient> callback)
{
var client = GetVoiceClient(handle);

if (client == null)
await RunWhenClientValidAsync(handle, async client =>
{
return;
}

callback(client);
}

internal T RunWhenClientConnected<T>(ushort handle, Func<TClient, T> callback)
{
return RunWhenClientValid(handle, client =>
{
if (!client.Connected)
{
return default(T);
}

return callback(client);
await InvokeProtectedEventAsync(() => OnClientMicrophoneMuteChanged?.Invoke(client, newStatus));
});
}

internal void RunWhenClientConnected(ushort handle, Action<TClient> callback)
private async void OnLogMessageFromVoice(string message, int loglevel)
{
RunWhenClientValid(handle, client =>
{
if (!client.Connected)
{
return;
}

callback(client);
});
await InvokeProtectedEventAsync(() => Log((LogLevel) loglevel, message));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* File: VoiceServer.Events.Helper.cs
* Date: 7.3.2018,
*
* MIT License
*
* Copyright (c) 2018 JustAnotherVoiceChat
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

using System;
using System.Threading.Tasks;
using JustAnotherVoiceChat.Server.Wrapper.Interfaces;

namespace JustAnotherVoiceChat.Server.Wrapper.Elements.Server
{
public partial class VoiceServer<TClient, TIdentifier> where TClient : IVoiceClient
{
internal T RunWhenClientValid<T>(ushort handle, Func<TClient, T> callback)
{
var client = GetVoiceClient(handle);

if (client == null)
{
return default(T);
}

return callback(client);
}

internal void RunWhenClientValid(ushort handle, Action<TClient> callback)
{
var client = GetVoiceClient(handle);

if (client == null)
{
return;
}

callback(client);
}

internal async Task<T> RunWhenClientValidAsync<T>(ushort handle, Func<TClient, Task<T>> callback)
{
var client = GetVoiceClient(handle);

if (client == null)
{
return default(T);
}

return await callback(client);
}

internal async Task RunWhenClientValidAsync(ushort handle, Func<TClient, Task> callback)
{
var client = GetVoiceClient(handle);

if (client == null)
{
return;
}

await callback(client);
}


internal Task<T> RunWhenClientConnectedAsync<T>(ushort handle, Func<TClient, Task<T>> callback)
{
return RunWhenClientValidAsync(handle, async client =>
{
if (!client.Connected)
{
return default(T);
}

return await callback(client);
});
}

internal Task RunWhenClientConnectedAsync(ushort handle, Func<TClient, Task> callback)
{
return RunWhenClientValidAsync(handle, async client =>
{
if (!client.Connected)
{
return;
}

await callback(client);
});
}

internal T RunWhenClientConnected<T>(ushort handle, Func<TClient, T> callback)
{
return RunWhenClientValid(handle, client =>
{
if (!client.Connected)
{
return default(T);
}

return callback(client);
});
}

internal void RunWhenClientConnected(ushort handle, Action<TClient> callback)
{
RunWhenClientValid(handle, client =>
{
if (!client.Connected)
{
return;
}

callback(client);
});
}
}
}
Loading