diff --git a/src/libs/Inworld/Generated/Inworld.Realtime.InworldSpeechToTextStreamRealtimeClient.ReceiveUpdates.g.cs b/src/libs/Inworld/Generated/Inworld.Realtime.InworldSpeechToTextStreamRealtimeClient.ReceiveUpdates.g.cs index e00c41a..a4b99d6 100644 --- a/src/libs/Inworld/Generated/Inworld.Realtime.InworldSpeechToTextStreamRealtimeClient.ReceiveUpdates.g.cs +++ b/src/libs/Inworld/Generated/Inworld.Realtime.InworldSpeechToTextStreamRealtimeClient.ReceiveUpdates.g.cs @@ -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; - } } } } diff --git a/src/libs/Inworld/Generated/Inworld.Realtime.InworldSpeechToTextStreamRealtimeClient.g.cs b/src/libs/Inworld/Generated/Inworld.Realtime.InworldSpeechToTextStreamRealtimeClient.g.cs index f7419b9..f8fdfe7 100644 --- a/src/libs/Inworld/Generated/Inworld.Realtime.InworldSpeechToTextStreamRealtimeClient.g.cs +++ b/src/libs/Inworld/Generated/Inworld.Realtime.InworldSpeechToTextStreamRealtimeClient.g.cs @@ -70,14 +70,83 @@ public InworldSpeechToTextStreamRealtimeClient( + + private void ApplyConnectionOptions( + global::System.Collections.Generic.IDictionary? additionalHeaders, + global::System.Collections.Generic.IEnumerable? 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(); + } + } + /// public async global::System.Threading.Tasks.Task ConnectAsync( global::System.Uri? uri = null, + global::System.Collections.Generic.IDictionary? additionalHeaders = null, + global::System.Collections.Generic.IEnumerable? 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); } /// @@ -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); } } \ No newline at end of file diff --git a/src/libs/Inworld/Generated/Inworld.Realtime.InworldTextToSpeechStreamRealtimeClient.ReceiveUpdates.g.cs b/src/libs/Inworld/Generated/Inworld.Realtime.InworldTextToSpeechStreamRealtimeClient.ReceiveUpdates.g.cs index bd09791..445b13d 100644 --- a/src/libs/Inworld/Generated/Inworld.Realtime.InworldTextToSpeechStreamRealtimeClient.ReceiveUpdates.g.cs +++ b/src/libs/Inworld/Generated/Inworld.Realtime.InworldTextToSpeechStreamRealtimeClient.ReceiveUpdates.g.cs @@ -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; - } } } } diff --git a/src/libs/Inworld/Generated/Inworld.Realtime.InworldTextToSpeechStreamRealtimeClient.g.cs b/src/libs/Inworld/Generated/Inworld.Realtime.InworldTextToSpeechStreamRealtimeClient.g.cs index 2dbaec4..2a556e8 100644 --- a/src/libs/Inworld/Generated/Inworld.Realtime.InworldTextToSpeechStreamRealtimeClient.g.cs +++ b/src/libs/Inworld/Generated/Inworld.Realtime.InworldTextToSpeechStreamRealtimeClient.g.cs @@ -70,14 +70,83 @@ public InworldTextToSpeechStreamRealtimeClient( + + private void ApplyConnectionOptions( + global::System.Collections.Generic.IDictionary? additionalHeaders, + global::System.Collections.Generic.IEnumerable? 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(); + } + } + /// public async global::System.Threading.Tasks.Task ConnectAsync( global::System.Uri? uri = null, + global::System.Collections.Generic.IDictionary? additionalHeaders = null, + global::System.Collections.Generic.IEnumerable? 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); } /// @@ -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); } } \ No newline at end of file