Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more error handling in ArcDpsClient #989

Merged
merged 1 commit into from
Sep 29, 2024
Merged
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
23 changes: 18 additions & 5 deletions Blish HUD/GameServices/ArcDps/V2/ArcDpsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -147,6 +146,7 @@ private async Task LegacyReceive(CancellationToken ct) {

if (Client.Available == 0) {
await Task.Delay(1, ct);
continue;
}

ReadFromStream(_networkStream, messageHeaderBuffer, 9);
Expand All @@ -160,6 +160,10 @@ private async Task LegacyReceive(CancellationToken ct) {
#endif

}
} catch (ObjectDisposedException) {
// We ignore DisposedExceptions, because it is clear, that the cancellation token already got cancelled
} catch (OperationCanceledException) {
// We ignore OperationCanceledExceptions, because we dont need to handle this, we just want to quit quietly
} catch (Exception ex) {
_logger.Error(ex.ToString());
Error?.Invoke(this, SocketError.SocketError);
Expand All @@ -179,6 +183,7 @@ private async Task Receive(CancellationToken ct) {

if (Client.Available == 0) {
await Task.Delay(1, ct);
continue;
}

ReadFromStream(_networkStream, messageHeaderBuffer, 5);
Expand All @@ -195,6 +200,10 @@ private async Task Receive(CancellationToken ct) {
// Reconnect if the bridge closes the connection.
// Pass on the cancellationToken from the creator of this class
this.Initialize((IPEndPoint)this.Client.Client.RemoteEndPoint, this._ct);
} catch (ObjectDisposedException) {
// We ignore DisposedExceptions, because it is clear, that the cancellation token already got cancelled
} catch (OperationCanceledException) {
// We ignore OperationCanceledExceptions, because we dont need to handle this, we just want to quit quietly
} catch (Exception ex) {
_logger.Error(ex.ToString());
Error?.Invoke(this, SocketError.SocketError);
Expand Down Expand Up @@ -229,10 +238,14 @@ protected virtual void Dispose(bool disposing) {
if (disposing) {
_cancellationTokenSource.Cancel();
Client?.Dispose();
foreach (var item in _messageQueues) {
if (item.Count != 0) {
foreach (var message in item) {
ArrayPool<byte>.Shared.Return(message);
if (_messageQueues != null) {
foreach (var item in _messageQueues) {
if (item != null) {
foreach (var message in item) {
if (message != null) {
ArrayPool<byte>.Shared.Return(message);
}
}
}
}
}
Expand Down
Loading