From a49bf42552d15543f170eeef83cfdc4ca51ab6fe Mon Sep 17 00:00:00 2001 From: RogueException Date: Sat, 2 Jan 2016 01:54:25 -0400 Subject: [PATCH] Made TaskManager's Signal methods async --- .../Net/WebSockets/VoiceWebSocket.cs | 2 +- src/Discord.Net/DiscordClient.cs | 2 +- src/Discord.Net/Net/WebSockets/GatewaySocket.cs | 2 +- src/Discord.Net/Net/WebSockets/WS4NetEngine.cs | 8 ++++---- src/Discord.Net/Net/WebSockets/WebSocket.cs | 6 +++--- src/Discord.Net/TaskManager.cs | 16 ++++++++-------- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Discord.Net.Audio/Net/WebSockets/VoiceWebSocket.cs b/src/Discord.Net.Audio/Net/WebSockets/VoiceWebSocket.cs index 049cd8c81..b0534f820 100644 --- a/src/Discord.Net.Audio/Net/WebSockets/VoiceWebSocket.cs +++ b/src/Discord.Net.Audio/Net/WebSockets/VoiceWebSocket.cs @@ -434,7 +434,7 @@ namespace Discord.Net.WebSockets var payload = (msg.Payload as JToken).ToObject(_serializer); _secretKey = payload.SecretKey; SendSetSpeaking(true); - EndConnect(); + await EndConnect(); } break; case OpCodes.Speaking: diff --git a/src/Discord.Net/DiscordClient.cs b/src/Discord.Net/DiscordClient.cs index ed5e6e2b5..47897ed10 100644 --- a/src/Discord.Net/DiscordClient.cs +++ b/src/Discord.Net/DiscordClient.cs @@ -185,7 +185,7 @@ namespace Discord } catch (Exception ex) { - _taskManager.SignalError(ex); + await _taskManager.SignalError(ex); throw; } } diff --git a/src/Discord.Net/Net/WebSockets/GatewaySocket.cs b/src/Discord.Net/Net/WebSockets/GatewaySocket.cs index f7f6040bc..755a9bd8c 100644 --- a/src/Discord.Net/Net/WebSockets/GatewaySocket.cs +++ b/src/Discord.Net/Net/WebSockets/GatewaySocket.cs @@ -100,7 +100,7 @@ namespace Discord.Net.WebSockets if (msg.Type == "READY" || msg.Type == "RESUMED") { _reconnects = 0; - EndConnect(); //Complete the connect + await EndConnect(); //Complete the connect } } break; diff --git a/src/Discord.Net/Net/WebSockets/WS4NetEngine.cs b/src/Discord.Net/Net/WebSockets/WS4NetEngine.cs index cbbd1c35f..916d90e40 100644 --- a/src/Discord.Net/Net/WebSockets/WS4NetEngine.cs +++ b/src/Discord.Net/Net/WebSockets/WS4NetEngine.cs @@ -86,20 +86,20 @@ namespace Discord.Net.WebSockets return TaskHelper.CompletedTask; } - private void OnWebSocketError(object sender, ErrorEventArgs e) + private async void OnWebSocketError(object sender, ErrorEventArgs e) { - _taskManager.SignalError(e.Exception); + await _taskManager.SignalError(e.Exception); _waitUntilConnect.Set(); _waitUntilDisconnect.Set(); } - private void OnWebSocketClosed(object sender, EventArgs e) + private async void OnWebSocketClosed(object sender, EventArgs e) { Exception ex; if (e is ClosedEventArgs) ex = new WebSocketException((e as ClosedEventArgs).Code, (e as ClosedEventArgs).Reason); else ex = new Exception("Connection lost"); - _taskManager.SignalError(ex); + await _taskManager.SignalError(ex); _waitUntilConnect.Set(); _waitUntilDisconnect.Set(); } diff --git a/src/Discord.Net/Net/WebSockets/WebSocket.cs b/src/Discord.Net/Net/WebSockets/WebSocket.cs index e70a0fe9d..eda4c03e7 100644 --- a/src/Discord.Net/Net/WebSockets/WebSocket.cs +++ b/src/Discord.Net/Net/WebSockets/WebSocket.cs @@ -91,11 +91,11 @@ namespace Discord.Net.WebSockets } catch (Exception ex) { - _taskManager.SignalError(ex); + await _taskManager.SignalError(ex); throw; } } - protected void EndConnect() + protected async Task EndConnect() { try { @@ -107,7 +107,7 @@ namespace Discord.Net.WebSockets } catch (Exception ex) { - _taskManager.SignalError(ex); + await _taskManager.SignalError(ex); } } diff --git a/src/Discord.Net/TaskManager.cs b/src/Discord.Net/TaskManager.cs index 26eaf8ef5..affd9b225 100644 --- a/src/Discord.Net/TaskManager.cs +++ b/src/Discord.Net/TaskManager.cs @@ -67,14 +67,14 @@ namespace Discord //Signal the rest of the tasks to stop if (firstTask.Exception != null) - SignalError(firstTask.Exception); + await SignalError(firstTask.Exception); else - SignalStop(); + await SignalStop(); //Wait for the other tasks, and signal their errors too just in case try { await allTasks.ConfigureAwait(false); } - catch (AggregateException ex) { SignalError(ex.InnerExceptions.First()); } - catch (Exception ex) { SignalError(ex); } + catch (AggregateException ex) { await SignalError(ex.InnerExceptions.First()); } + catch (Exception ex) { await SignalError(ex); } //Run the cleanup function within our lock if (_stopAction != null) @@ -87,9 +87,9 @@ namespace Discord } } - public void SignalStop(bool isExpected = false) + public async Task SignalStop(bool isExpected = false) { - using (_lock.Lock()) + using (await _lock.LockAsync()) { if (isExpected) _wasStopExpected = true; @@ -119,9 +119,9 @@ namespace Discord await task; } - public void SignalError(Exception ex) + public async Task SignalError(Exception ex) { - using (_lock.Lock()) + using (await _lock.LockAsync()) { if (_stopReason != null) return;