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 @@ -28,36 +28,74 @@ public sealed partial class InworldSpeechToTextStreamRealtimeClient
yield break;
}

global::System.Net.WebSockets.WebSocketReceiveResult result;
using var __messageBuffer = new global::System.IO.MemoryStream();
var __receivedTextMessage = false;

try
while (true)
{
result = await _clientWebSocket.ReceiveAsync(arraySegment, cancellationToken).ConfigureAwait(false);
}
catch (global::System.Net.WebSockets.WebSocketException)
{
yield break;
global::System.Net.WebSockets.WebSocketReceiveResult result;

try
{
result = await _clientWebSocket.ReceiveAsync(arraySegment, cancellationToken).ConfigureAwait(false);
}
catch (global::System.Net.WebSockets.WebSocketException exception)
{
var rethrow = false;
OnReceiveException(exception, ref rethrow);
if (rethrow)
{
throw;
}

yield break;
}
catch (global::System.OperationCanceledException exception)
{
var rethrow = false;
OnReceiveException(exception, ref rethrow);
if (rethrow)
{
throw;
}

yield break;
}

if (result.MessageType == global::System.Net.WebSockets.WebSocketMessageType.Close)
{
await _clientWebSocket.CloseAsync(
closeStatus: global::System.Net.WebSockets.WebSocketCloseStatus.NormalClosure,
statusDescription: "Closing",
cancellationToken: cancellationToken).ConfigureAwait(false);
yield break;
}

if (result.MessageType == global::System.Net.WebSockets.WebSocketMessageType.Text)
{
__receivedTextMessage = true;

if (result.Count > 0)
{
__messageBuffer.Write(buffer, 0, result.Count);
}
}

if (result.EndOfMessage)
{
break;
}
}
catch (global::System.OperationCanceledException)

if (!__receivedTextMessage)
{
yield break;
continue;
}

if (result.MessageType == global::System.Net.WebSockets.WebSocketMessageType.Text)
{
string json = global::System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count);
string json = global::System.Text.Encoding.UTF8.GetString(__messageBuffer.ToArray());
var @event = (global::Inworld.Realtime.SpeechToTextStreamServerEvent)global::System.Text.Json.JsonSerializer.Deserialize(json, typeof(global::Inworld.Realtime.SpeechToTextStreamServerEvent), JsonSerializerContext)!;

yield return @event;
}
else if (result.MessageType == global::System.Net.WebSockets.WebSocketMessageType.Close)
{
await _clientWebSocket.CloseAsync(
closeStatus: global::System.Net.WebSockets.WebSocketCloseStatus.NormalClosure,
statusDescription: "Closing",
cancellationToken: cancellationToken).ConfigureAwait(false);
yield break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,83 @@ public InworldSpeechToTextStreamRealtimeClient(




private void ApplyConnectionOptions(
global::System.Collections.Generic.IDictionary<string, string>? additionalHeaders,
global::System.Collections.Generic.IEnumerable<string>? additionalSubProtocols,
global::System.TimeSpan? keepAliveInterval)
{
if (keepAliveInterval is not null)
{
_clientWebSocket.Options.KeepAliveInterval = keepAliveInterval.Value;
}

if (additionalHeaders is not null)
{
foreach (var header in additionalHeaders)
{
_clientWebSocket.Options.SetRequestHeader(header.Key, header.Value);
}
}

if (additionalSubProtocols is not null)
{
foreach (var subProtocol in additionalSubProtocols)
{
_clientWebSocket.Options.AddSubProtocol(subProtocol);
}
}
}

private async global::System.Threading.Tasks.Task ConnectAsyncCore(
global::System.Uri uri,
global::System.TimeSpan? connectTimeout,
global::System.Threading.CancellationToken cancellationToken)
{
global::System.Threading.CancellationTokenSource? __timeoutCancellationTokenSource = null;
var __effectiveCancellationToken = cancellationToken;

if (connectTimeout is not null)
{
__timeoutCancellationTokenSource = global::System.Threading.CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
__timeoutCancellationTokenSource.CancelAfter(connectTimeout.Value);
__effectiveCancellationToken = __timeoutCancellationTokenSource.Token;
}

try
{
await _clientWebSocket.ConnectAsync(uri, __effectiveCancellationToken).ConfigureAwait(false);
}
finally
{
__timeoutCancellationTokenSource?.Dispose();
}
}

/// <inheritdoc cref="global::System.Net.WebSockets.ClientWebSocket.ConnectAsync(global::System.Uri, global::System.Threading.CancellationToken)"/>
public async global::System.Threading.Tasks.Task ConnectAsync(
global::System.Uri? uri = null,
global::System.Collections.Generic.IDictionary<string, string>? additionalHeaders = null,
global::System.Collections.Generic.IEnumerable<string>? additionalSubProtocols = null,
global::System.TimeSpan? keepAliveInterval = null,
global::System.TimeSpan? connectTimeout = null,
global::System.Threading.CancellationToken cancellationToken = default)
{
uri ??= new global::System.Uri(DefaultBaseUrl);
global::System.Uri __uri;
if (uri is not null)
{
__uri = uri;
}
else
{
var __pathBuilder = new global::Inworld.Realtime.PathBuilder(
path: DefaultBaseUrl);

__uri = new global::System.Uri(__pathBuilder.ToString());
}

await _clientWebSocket.ConnectAsync(uri, cancellationToken).ConfigureAwait(false);
ApplyConnectionOptions(additionalHeaders, additionalSubProtocols, keepAliveInterval);
await ConnectAsyncCore(__uri, connectTimeout, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -165,5 +234,8 @@ partial void ProcessResponseContent(
global::System.Net.WebSockets.ClientWebSocket client,
global::System.Net.Http.HttpResponseMessage response,
ref string content);
partial void OnReceiveException(
global::System.Exception exception,
ref bool rethrow);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,74 @@ public sealed partial class InworldTextToSpeechStreamRealtimeClient
yield break;
}

global::System.Net.WebSockets.WebSocketReceiveResult result;
using var __messageBuffer = new global::System.IO.MemoryStream();
var __receivedTextMessage = false;

try
while (true)
{
result = await _clientWebSocket.ReceiveAsync(arraySegment, cancellationToken).ConfigureAwait(false);
}
catch (global::System.Net.WebSockets.WebSocketException)
{
yield break;
global::System.Net.WebSockets.WebSocketReceiveResult result;

try
{
result = await _clientWebSocket.ReceiveAsync(arraySegment, cancellationToken).ConfigureAwait(false);
}
catch (global::System.Net.WebSockets.WebSocketException exception)
{
var rethrow = false;
OnReceiveException(exception, ref rethrow);
if (rethrow)
{
throw;
}

yield break;
}
catch (global::System.OperationCanceledException exception)
{
var rethrow = false;
OnReceiveException(exception, ref rethrow);
if (rethrow)
{
throw;
}

yield break;
}

if (result.MessageType == global::System.Net.WebSockets.WebSocketMessageType.Close)
{
await _clientWebSocket.CloseAsync(
closeStatus: global::System.Net.WebSockets.WebSocketCloseStatus.NormalClosure,
statusDescription: "Closing",
cancellationToken: cancellationToken).ConfigureAwait(false);
yield break;
}

if (result.MessageType == global::System.Net.WebSockets.WebSocketMessageType.Text)
{
__receivedTextMessage = true;

if (result.Count > 0)
{
__messageBuffer.Write(buffer, 0, result.Count);
}
}

if (result.EndOfMessage)
{
break;
}
}
catch (global::System.OperationCanceledException)

if (!__receivedTextMessage)
{
yield break;
continue;
}

if (result.MessageType == global::System.Net.WebSockets.WebSocketMessageType.Text)
{
string json = global::System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count);
string json = global::System.Text.Encoding.UTF8.GetString(__messageBuffer.ToArray());
var @event = (global::Inworld.Realtime.TextToSpeechStreamServerEvent)global::System.Text.Json.JsonSerializer.Deserialize(json, typeof(global::Inworld.Realtime.TextToSpeechStreamServerEvent), JsonSerializerContext)!;

yield return @event;
}
else if (result.MessageType == global::System.Net.WebSockets.WebSocketMessageType.Close)
{
await _clientWebSocket.CloseAsync(
closeStatus: global::System.Net.WebSockets.WebSocketCloseStatus.NormalClosure,
statusDescription: "Closing",
cancellationToken: cancellationToken).ConfigureAwait(false);
yield break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,83 @@ public InworldTextToSpeechStreamRealtimeClient(




private void ApplyConnectionOptions(
global::System.Collections.Generic.IDictionary<string, string>? additionalHeaders,
global::System.Collections.Generic.IEnumerable<string>? additionalSubProtocols,
global::System.TimeSpan? keepAliveInterval)
{
if (keepAliveInterval is not null)
{
_clientWebSocket.Options.KeepAliveInterval = keepAliveInterval.Value;
}

if (additionalHeaders is not null)
{
foreach (var header in additionalHeaders)
{
_clientWebSocket.Options.SetRequestHeader(header.Key, header.Value);
}
}

if (additionalSubProtocols is not null)
{
foreach (var subProtocol in additionalSubProtocols)
{
_clientWebSocket.Options.AddSubProtocol(subProtocol);
}
}
}

private async global::System.Threading.Tasks.Task ConnectAsyncCore(
global::System.Uri uri,
global::System.TimeSpan? connectTimeout,
global::System.Threading.CancellationToken cancellationToken)
{
global::System.Threading.CancellationTokenSource? __timeoutCancellationTokenSource = null;
var __effectiveCancellationToken = cancellationToken;

if (connectTimeout is not null)
{
__timeoutCancellationTokenSource = global::System.Threading.CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
__timeoutCancellationTokenSource.CancelAfter(connectTimeout.Value);
__effectiveCancellationToken = __timeoutCancellationTokenSource.Token;
}

try
{
await _clientWebSocket.ConnectAsync(uri, __effectiveCancellationToken).ConfigureAwait(false);
}
finally
{
__timeoutCancellationTokenSource?.Dispose();
}
}

/// <inheritdoc cref="global::System.Net.WebSockets.ClientWebSocket.ConnectAsync(global::System.Uri, global::System.Threading.CancellationToken)"/>
public async global::System.Threading.Tasks.Task ConnectAsync(
global::System.Uri? uri = null,
global::System.Collections.Generic.IDictionary<string, string>? additionalHeaders = null,
global::System.Collections.Generic.IEnumerable<string>? additionalSubProtocols = null,
global::System.TimeSpan? keepAliveInterval = null,
global::System.TimeSpan? connectTimeout = null,
global::System.Threading.CancellationToken cancellationToken = default)
{
uri ??= new global::System.Uri(DefaultBaseUrl);
global::System.Uri __uri;
if (uri is not null)
{
__uri = uri;
}
else
{
var __pathBuilder = new global::Inworld.Realtime.PathBuilder(
path: DefaultBaseUrl);

__uri = new global::System.Uri(__pathBuilder.ToString());
}

await _clientWebSocket.ConnectAsync(uri, cancellationToken).ConfigureAwait(false);
ApplyConnectionOptions(additionalHeaders, additionalSubProtocols, keepAliveInterval);
await ConnectAsyncCore(__uri, connectTimeout, cancellationToken).ConfigureAwait(false);
}

/// <summary>
Expand Down Expand Up @@ -165,5 +234,8 @@ partial void ProcessResponseContent(
global::System.Net.WebSockets.ClientWebSocket client,
global::System.Net.Http.HttpResponseMessage response,
ref string content);
partial void OnReceiveException(
global::System.Exception exception,
ref bool rethrow);
}
}