Skip to content

Commit c7fba82

Browse files
blackspherefollowerqdot
authored andcommitted
Reinforcing the client's disconnect method
I've seen some socket aborted exceptions thrown on disconnect. We should be handling these errors.
1 parent c52ec04 commit c7fba82

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

Buttplug.Client/ButtplugWSClient.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ public class ButtplugWSClient
6969
[CanBeNull]
7070
public event EventHandler<LogEventArgs> Log;
7171

72-
[CanBeNull]
73-
private bool _gotServerInfo;
74-
75-
[CanBeNull]
76-
private bool _gotError;
77-
7872
public uint nextMsgId
7973
{
8074
get
@@ -110,8 +104,6 @@ public async Task Connect(Uri aURL)
110104
_waitingMsgs.Clear();
111105
_devices.Clear();
112106
_counter = 1;
113-
_gotServerInfo = false;
114-
_gotError = false;
115107
await _ws.ConnectAsync(aURL, CancellationToken.None);
116108

117109
if (_ws.State != WebSocketState.Open)
@@ -149,16 +141,34 @@ public async Task Disconnect()
149141
_pingTimer = null;
150142
}
151143

152-
while (_ws != null && _ws.State != WebSocketState.Closed && _ws.State != WebSocketState.Aborted)
144+
try
153145
{
154-
if (_ws.State != WebSocketState.CloseSent && _ws.State != WebSocketState.Closed)
146+
while (_ws != null && _ws.State != WebSocketState.Closed && _ws.State != WebSocketState.Aborted)
155147
{
156-
await _ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Client shutdown", _tokenSource.Token);
148+
if (_ws.State != WebSocketState.CloseSent && _ws.State != WebSocketState.Closed)
149+
{
150+
await _ws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Client shutdown", _tokenSource.Token);
151+
}
157152
}
158153
}
154+
catch
155+
{
156+
// noop - something when wrong closing the socket, but we're
157+
// about to dispose of it anyway.
158+
}
159+
160+
try
161+
{
162+
_tokenSource.Cancel();
163+
_readThread.Wait();
164+
}
165+
catch
166+
{
167+
// noop - something when wrong closing the socket, but we're
168+
// about to dispose of it anyway.
169+
}
159170

160-
_tokenSource.Cancel();
161-
_readThread.Wait();
171+
_ws = null;
162172

163173
var max = 3;
164174
while (max-- > 0 && _waitingMsgs.Count != 0)

0 commit comments

Comments
 (0)