Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize Factory, Add CancelToken Option to Send Functions #347

Merged
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
2 changes: 1 addition & 1 deletion Deepgram/Abstractions/v2/AbstractRestClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -772,7 +772,7 @@
}

Log.Verbose(module, $"Deepgram Generic Exception thrown");
throw new DeepgramException(errMsg);

Check warning on line 775 in Deepgram/Abstractions/v2/AbstractRestClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'errMsg' in 'DeepgramException.DeepgramException(string errMsg)'.

Check warning on line 775 in Deepgram/Abstractions/v2/AbstractRestClient.cs

View workflow job for this annotation

GitHub Actions / test (7.0.x)

Possible null reference argument for parameter 'errMsg' in 'DeepgramException.DeepgramException(string errMsg)'.

Check warning on line 775 in Deepgram/Abstractions/v2/AbstractRestClient.cs

View workflow job for this annotation

GitHub Actions / test (8.0.x)

Possible null reference argument for parameter 'errMsg' in 'DeepgramException.DeepgramException(string errMsg)'.
}

internal static string GetUri(IDeepgramClientOptions options, string path)
Expand Down
24 changes: 15 additions & 9 deletions Deepgram/Abstractions/v2/AbstractWebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public async Task<bool> Subscribe(EventHandler<ErrorResponse> eventHandler)
/// <summary>
/// Sends a Close message to Deepgram
/// </summary>
public virtual Task SendClose(bool nullByte = false)
public virtual Task SendClose(bool nullByte = false, CancellationTokenSource? _cancellationToken = null)
{
throw new DeepgramException("Unimplemented");
}
Expand All @@ -271,24 +271,27 @@ public virtual void SendMessage(byte[] data, int length = Constants.UseArrayLeng

/// <summary>
/// This method sends a binary message over the WebSocket connection immediately without queueing.
/// </summary>
public virtual async Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend)
/// </summary>,
public virtual async Task SendBinaryImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null)
{
if (!IsConnected())
{
Log.Debug("SendBinaryImmediately", "WebSocket is not connected. Exiting...");
return;
}

await _mutexSend.WaitAsync(_cancellationTokenSource.Token);
// provide a cancellation token, or use the one in the class
var _cancelToken = _cancellationToken ?? _cancellationTokenSource;

await _mutexSend.WaitAsync(_cancelToken.Token);
davidvonthenen marked this conversation as resolved.
Show resolved Hide resolved
try
{
Log.Verbose("SendBinaryImmediately", "Sending binary message immediately...");
if (length == Constants.UseArrayLengthForSend)
{
length = data.Length;
}
await _clientWebSocket.SendAsync(new ArraySegment<byte>(data, 0, length), WebSocketMessageType.Binary, true, _cancellationTokenSource.Token)
await _clientWebSocket.SendAsync(new ArraySegment<byte>(data, 0, length), WebSocketMessageType.Binary, true, _cancelToken.Token)
.ConfigureAwait(false);
}
finally
Expand All @@ -300,23 +303,26 @@ await _clientWebSocket.SendAsync(new ArraySegment<byte>(data, 0, length), WebSoc
/// <summary>
/// This method sends a text message over the WebSocket connection immediately without queueing.
/// </summary>
public virtual async Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend)
public virtual async Task SendMessageImmediately(byte[] data, int length = Constants.UseArrayLengthForSend, CancellationTokenSource? _cancellationToken = null)
davidvonthenen marked this conversation as resolved.
Show resolved Hide resolved
{
if (!IsConnected())
{
Log.Debug("SendBinaryImmediately", "WebSocket is not connected. Exiting...");
return;
}

await _mutexSend.WaitAsync(_cancellationTokenSource.Token);
// provide a cancellation token, or use the one in the class
var _cancelToken = _cancellationToken ?? _cancellationTokenSource;

await _mutexSend.WaitAsync(_cancelToken.Token);
try
{
Log.Verbose("SendMessageImmediately", "Sending text message immediately...");
if (length == Constants.UseArrayLengthForSend)
{
length = data.Length;
}
await _clientWebSocket.SendAsync(new ArraySegment<byte>(data, 0, length), WebSocketMessageType.Text, true, _cancellationTokenSource.Token)
await _clientWebSocket.SendAsync(new ArraySegment<byte>(data, 0, length), WebSocketMessageType.Text, true, _cancelToken.Token)
.ConfigureAwait(false);
}
finally
Expand Down Expand Up @@ -613,7 +619,7 @@ public async Task<bool> Stop(CancellationTokenSource? cancelToken = null, bool n
if (_clientWebSocket!.State == WebSocketState.Open)
{
Log.Debug("Stop", "Sending Close message...");
await SendClose(nullByte);
await SendClose(nullByte, cancelToken);
}

// small delay to wait for any final transcription
Expand Down
2 changes: 1 addition & 1 deletion Deepgram/Abstractions/v2/Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

Expand Down
2 changes: 1 addition & 1 deletion Deepgram/Abstractions/v2/WebSocketMessage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Copyright 2024 Deepgram .NET SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT

Expand Down
Loading
Loading