private async Task SendFrameAsync(IList<ArraySegment<byte>> sendBuffers, CancellationToken cancellationToken)
{
bool sendFrameLockTaken = false;
try
{
await _sendFrameThrottle.WaitAsync(cancellationToken).SuppressContextFlow();
sendFrameLockTaken = true;
if (sendBuffers.Count > 1 &&
_innerStreamAsWebSocketStream != null &&
_innerStreamAsWebSocketStream.SupportsMultipleWrite)
{
await _innerStreamAsWebSocketStream.MultipleWriteAsync(sendBuffers,
cancellationToken).SuppressContextFlow();
}
else
{
foreach (ArraySegment<byte> buffer in sendBuffers)
{
await _innerStream.WriteAsync(buffer.Array,
buffer.Offset,
buffer.Count,
cancellationToken).SuppressContextFlow();
}
}
}
catch (ObjectDisposedException objectDisposedException)
{
throw new WebSocketException(WebSocketError.ConnectionClosedPrematurely, objectDisposedException);
}
catch (NotSupportedException notSupportedException)
{
throw new WebSocketException(WebSocketError.ConnectionClosedPrematurely, notSupportedException);
}
finally
{
if (sendFrameLockTaken)
{
_sendFrameThrottle.Release();
}
}
}