feature: Implement Dispose for types which have disposable data (#1171)

* Initial set of dispose implementations

Not handled yet:
- Discord.Net.Websocket/Entities/SocketGuild
- Discord.Net.Tests

* Refactor DiscordSocketClient init into ctor

This way we remove an IDisposableAnalyzer warning for not disposing
the client when we set the client variable.

* Dispose of clients when disposing sharded client

* Finish implementing IDisposable where appropriate

I opted to use NoWarn in the Tests project as it wasn't really necessary
considering that our tests only run once

* Tweak samples after feedback
This commit is contained in:
Monica S
2018-11-29 01:18:16 +00:00
committed by Christopher F
parent dca6c33da3
commit 7366cd4361
31 changed files with 406 additions and 154 deletions

View File

@@ -13,24 +13,29 @@ namespace Discord.Net.Udp
private readonly SemaphoreSlim _lock;
private UdpClient _udp;
private IPEndPoint _destination;
private CancellationTokenSource _cancelTokenSource;
private CancellationTokenSource _stopCancelTokenSource, _cancelTokenSource;
private CancellationToken _cancelToken, _parentToken;
private Task _task;
private bool _isDisposed;
public ushort Port => (ushort)((_udp?.Client.LocalEndPoint as IPEndPoint)?.Port ?? 0);
public DefaultUdpSocket()
{
_lock = new SemaphoreSlim(1, 1);
_cancelTokenSource = new CancellationTokenSource();
_stopCancelTokenSource = new CancellationTokenSource();
}
private void Dispose(bool disposing)
{
if (!_isDisposed)
{
if (disposing)
{
StopInternalAsync(true).GetAwaiter().GetResult();
_stopCancelTokenSource?.Dispose();
_cancelTokenSource?.Dispose();
_lock?.Dispose();
}
_isDisposed = true;
}
}
@@ -56,9 +61,14 @@ namespace Discord.Net.Udp
{
await StopInternalAsync().ConfigureAwait(false);
_cancelTokenSource = new CancellationTokenSource();
_cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
_stopCancelTokenSource?.Dispose();
_cancelTokenSource?.Dispose();
_stopCancelTokenSource = new CancellationTokenSource();
_cancelTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _stopCancelTokenSource.Token);
_cancelToken = _cancelTokenSource.Token;
_udp?.Dispose();
_udp = new UdpClient(0);
_task = RunAsync(_cancelToken);
@@ -77,7 +87,7 @@ namespace Discord.Net.Udp
}
public async Task StopInternalAsync(bool isDisposing = false)
{
try { _cancelTokenSource.Cancel(false); } catch { }
try { _stopCancelTokenSource.Cancel(false); } catch { }
if (!isDisposing)
await (_task ?? Task.Delay(0)).ConfigureAwait(false);
@@ -96,8 +106,11 @@ namespace Discord.Net.Udp
}
public void SetCancelToken(CancellationToken cancelToken)
{
_cancelTokenSource?.Dispose();
_parentToken = cancelToken;
_cancelToken = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _cancelTokenSource.Token).Token;
_cancelTokenSource = CancellationTokenSource.CreateLinkedTokenSource(_parentToken, _stopCancelTokenSource.Token);
_cancelToken = _cancelTokenSource.Token;
}
public async Task SendAsync(byte[] data, int index, int count)