Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,25 @@ public Task ConnectAsync(Uri uri, CancellationToken cancellationToken)
return ConnectAsyncCore(uri, cancellationToken);
}

private Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken)
private async Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken)
{
_innerWebSocket = new WebSocketHandle();

// Change internal state to 'connected' to enable the other methods
if ((InternalState)Interlocked.CompareExchange(ref _state, (int)InternalState.Connected, (int)InternalState.Connecting) != InternalState.Connecting)
try
{
await _innerWebSocket.ConnectAsync(uri, cancellationToken, Options).ConfigureAwait(false);
}
catch
{
return Task.FromException(new ObjectDisposedException(nameof(ClientWebSocket))); // Aborted/Disposed during connect.
Dispose();
throw;
}

return _innerWebSocket.ConnectAsync(uri, cancellationToken, Options);
if ((InternalState)Interlocked.CompareExchange(ref _state, (int)InternalState.Connected, (int)InternalState.Connecting) != InternalState.Connecting)
{
Debug.Assert(_state == (int)InternalState.Disposed);
throw new ObjectDisposedException(GetType().FullName);
}
}

public override Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public async Task ConnectAsync_NotWebSocketServer_ThrowsWebSocketExceptionWithMe
}
Assert.Equal(WebSocketState.Closed, cws.State);
Assert.Equal(exceptionMessage, ex.Message);

// Other operations throw after failed connect
await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.ReceiveAsync(new byte[1], default));
await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.SendAsync(new byte[1], WebSocketMessageType.Binary, true, default));
await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.CloseAsync(WebSocketCloseStatus.NormalClosure, null, default));
await Assert.ThrowsAsync<ObjectDisposedException>(() => cws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, default));
}
}

Expand Down