Skip to content

Commit

Permalink
Reorganize Factory, Add CancelToken Option to Send Functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dvonthenen committed Nov 1, 2024
1 parent ba0f717 commit b52d203
Show file tree
Hide file tree
Showing 32 changed files with 198 additions and 293 deletions.
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
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);
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)
{
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

0 comments on commit b52d203

Please sign in to comment.