Browse Source

Use better exceptions during websocket errors

tags/docs-0.9
Brandon Smith 9 years ago
parent
commit
e6be52e83b
3 changed files with 47 additions and 24 deletions
  1. +1
    -1
      src/Discord.Net/DiscordDataSocket.cs
  2. +11
    -9
      src/Discord.Net/DiscordVoiceSocket.cs
  3. +35
    -14
      src/Discord.Net/DiscordWebSocket.cs

+ 1
- 1
src/Discord.Net/DiscordDataSocket.cs View File

@@ -42,7 +42,7 @@ namespace Discord
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
throw new InvalidOperationException("Bad Token");
throw _disconnectReason;
} }
try { _connectWaitOnLogin2.Wait(cancelToken); } //Waiting on READY handler try { _connectWaitOnLogin2.Wait(cancelToken); } //Waiting on READY handler
catch (OperationCanceledException) { return; } catch (OperationCanceledException) { return; }


+ 11
- 9
src/Discord.Net/DiscordVoiceSocket.cs View File

@@ -109,7 +109,7 @@ namespace Discord
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
throw new InvalidOperationException("Bad Token");
throw _disconnectReason;
} }


SetConnected(); SetConnected();
@@ -129,8 +129,9 @@ namespace Discord
ProcessUdpMessage(result); ProcessUdpMessage(result);
} }
} }
catch { }
finally { cancelSource.Cancel(); }
catch (OperationCanceledException) { }
catch (Exception ex) { DisconnectInternal(ex); }
finally { DisconnectInternal(); }
} }


#if USE_THREAD #if USE_THREAD
@@ -156,7 +157,7 @@ namespace Discord
uint timestamp = 0; uint timestamp = 0;
double nextTicks = 0.0; double nextTicks = 0.0;
double ticksPerMillisecond = Stopwatch.Frequency / 1000.0; double ticksPerMillisecond = Stopwatch.Frequency / 1000.0;
double ticksPerFrame = ticksPerMillisecond * _encoder.FrameLength;
double ticksPerFrame = ticksPerMillisecond * _encoder.FrameLength;
double spinLockThreshold = 1.5 * ticksPerMillisecond; double spinLockThreshold = 1.5 * ticksPerMillisecond;
uint samplesPerFrame = (uint)_encoder.SamplesPerFrame; uint samplesPerFrame = (uint)_encoder.SamplesPerFrame;
Stopwatch sw = Stopwatch.StartNew(); Stopwatch sw = Stopwatch.StartNew();
@@ -208,11 +209,12 @@ namespace Discord
#endif #endif
} }
} }
catch { }
finally { cancelSource.Cancel(); }
}
catch (OperationCanceledException) { }
catch (Exception ex) { DisconnectInternal(ex); }
finally { DisconnectInternal(); }
}
#endif #endif
//Closes the UDP socket when _disconnectToken is triggered, since UDPClient doesn't allow passing a canceltoken
//Closes the UDP socket when _disconnectToken is triggered, since UDPClient doesn't allow passing a canceltoken
private async Task WatcherAsync() private async Task WatcherAsync()
{ {
var cancelToken = _disconnectToken.Token; var cancelToken = _disconnectToken.Token;
@@ -220,7 +222,7 @@ namespace Discord
{ {
await Task.Delay(-1, cancelToken); await Task.Delay(-1, cancelToken);
} }
catch (TaskCanceledException) { }
catch (OperationCanceledException) { }
finally { _udp.Close(); } finally { _udp.Close(); }
} }


+ 35
- 14
src/Discord.Net/DiscordWebSocket.cs View File

@@ -19,12 +19,13 @@ namespace Discord
private readonly ConcurrentQueue<byte[]> _sendQueue; private readonly ConcurrentQueue<byte[]> _sendQueue;


protected CancellationTokenSource _disconnectToken; protected CancellationTokenSource _disconnectToken;
protected string _host;
protected int _timeout, _heartbeatInterval;
protected Exception _disconnectReason;
private ClientWebSocket _webSocket; private ClientWebSocket _webSocket;
private DateTime _lastHeartbeat; private DateTime _lastHeartbeat;
private Task _task; private Task _task;
protected string _host;
protected int _timeout, _heartbeatInterval;
private bool _isConnected, _wasDisconnectedUnexpected;
private bool _isConnected, _wasDisconnectUnexpected;


public DiscordWebSocket(DiscordClient client, int timeout, int interval, bool isDebug) public DiscordWebSocket(DiscordClient client, int timeout, int interval, bool isDebug)
{ {
@@ -64,7 +65,7 @@ namespace Discord


_disconnectToken.Dispose(); _disconnectToken.Dispose();
_disconnectToken = null; _disconnectToken = null;
_wasDisconnectedUnexpected = false;
_wasDisconnectUnexpected = false;


//Clear send queue //Clear send queue
_heartbeatInterval = 0; _heartbeatInterval = 0;
@@ -77,7 +78,7 @@ namespace Discord
if (_isConnected) if (_isConnected)
{ {
_isConnected = false; _isConnected = false;
RaiseDisconnected(_wasDisconnectedUnexpected);
RaiseDisconnected(_wasDisconnectUnexpected);
} }


_task = null; _task = null;
@@ -89,10 +90,26 @@ namespace Discord
{ {
if (_task != null) if (_task != null)
{ {
try { _disconnectToken.Cancel(); } catch (NullReferenceException) { }
try { DisconnectInternal(new Exception("Disconnect requested by user.")); } catch (NullReferenceException) { }
try { await _task; } catch (NullReferenceException) { } try { await _task; } catch (NullReferenceException) { }
} }
} }

protected void DisconnectInternal()
{
_disconnectToken.Cancel();
}
protected void DisconnectInternal(Exception ex)
{
if (_disconnectReason == null)
{
if (ex == null)
ex = new Exception("Disconnect requested by user.");
_disconnectReason = ex;
_disconnectToken.Cancel();
}
}

protected virtual void OnConnect() { } protected virtual void OnConnect() { }
protected virtual void OnDisconnect() { } protected virtual void OnDisconnect() { }


@@ -131,17 +148,19 @@ namespace Discord


if (result.MessageType == WebSocketMessageType.Close) if (result.MessageType == WebSocketMessageType.Close)
{ {
_wasDisconnectedUnexpected = true;
_wasDisconnectUnexpected = true;
string msg = $"Got Close Message ({result.CloseStatus?.ToString() ?? "Unexpected"}, {result.CloseStatusDescription ?? "No Reason"})";
RaiseOnDebugMessage(DebugMessageType.Connection, msg);
DisconnectInternal(new Exception(msg));
await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
RaiseOnDebugMessage(DebugMessageType.Connection, $"Got Close Message ({result.CloseStatus?.ToString() ?? "Unexpected"}, {result.CloseStatusDescription ?? "No Reason"})");
return;
return;
} }
else else
builder.Append(Encoding.UTF8.GetString(buffer, 0, result.Count)); builder.Append(Encoding.UTF8.GetString(buffer, 0, result.Count));


} }
while (!result.EndOfMessage); while (!result.EndOfMessage);
#if DEBUG #if DEBUG
System.Diagnostics.Debug.WriteLine(">>> " + builder.ToString()); System.Diagnostics.Debug.WriteLine(">>> " + builder.ToString());
#endif #endif
@@ -150,8 +169,9 @@ namespace Discord
builder.Clear(); builder.Clear();
} }
} }
catch { }
finally { cancelSource.Cancel(); }
catch (OperationCanceledException) { }
catch (Exception ex) { DisconnectInternal(ex); }
finally { DisconnectInternal(); }
} }
private async Task SendAsync() private async Task SendAsync()
{ {
@@ -178,8 +198,9 @@ namespace Discord
await Task.Delay(_sendInterval, cancelToken); await Task.Delay(_sendInterval, cancelToken);
} }
} }
catch { }
finally { cancelSource.Cancel(); }
catch (OperationCanceledException) { }
catch (Exception ex) { DisconnectInternal(ex); }
finally { DisconnectInternal(); }
} }


protected abstract Task ProcessMessage(string json); protected abstract Task ProcessMessage(string json);


Loading…
Cancel
Save