Skip to content

Commit

Permalink
Merge pull request #989 from Denrage/feat/arcdpsV2ExceptionHandling
Browse files Browse the repository at this point in the history
Added more error handling in ArcDpsClient
  • Loading branch information
dlamkins authored Sep 29, 2024
2 parents 2a3a2cd + f757afd commit 35c95fc
Showing 1 changed file with 18 additions and 5 deletions.
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

0 comments on commit 35c95fc

Please sign in to comment.