From cff40522a406f031b13e72a94e6d0beacab29ab4 Mon Sep 17 00:00:00 2001 From: Tino Donderwinkel Date: Mon, 19 Feb 2024 11:20:32 +0100 Subject: [PATCH] Version 2.0.2 --- OBSClient/OBSClient.csproj | 2 +- OBSClient/ObsClient.cs | 79 +++++++++++++++++++++++++++++++++----- README.md | 4 +- 3 files changed, 72 insertions(+), 13 deletions(-) diff --git a/OBSClient/OBSClient.csproj b/OBSClient/OBSClient.csproj index 2a5f00a..1af2d9d 100644 --- a/OBSClient/OBSClient.csproj +++ b/OBSClient/OBSClient.csproj @@ -15,7 +15,7 @@ git obs LICENSE - 2.0.1 + 2.0.2 True diff --git a/OBSClient/ObsClient.cs b/OBSClient/ObsClient.cs index 92e363a..4a26697 100644 --- a/OBSClient/ObsClient.cs +++ b/OBSClient/ObsClient.cs @@ -74,6 +74,10 @@ public partial class ObsClient : INotifyPropertyChanged, IDisposable private int _sessionMessagesSent = 0; + private int _maxRequestRetries = 10; + + private int _requestRetryInterval = 500; + /// /// Gets or sets the maximum amount of time, in milliseconds, the to wait for an OBS Studio response after making a request. /// @@ -269,6 +273,47 @@ public int SessionMessagesReceived } } + /// + /// Gets or sets the maximum number of times the should retry a request when OBS Studio is not ready to perform the request. + /// + /// + /// This typically occurs when OBS Studio is not ready to perform the request, e.g. when it is still starting up. It will authenticate the client, but not yet accept requests. + /// + public int MaxRequestRetries + { + get + { + return this._maxRequestRetries; + } + set + { + if (this._maxRequestRetries != value && value >= 0) + { + this._maxRequestRetries = value; + this.OnPropertyChanged(); + } + } + } + + /// + /// Gets or sets the interval, in milliseconds, the should wait before retrying a request when OBS Studio is not ready to perform the request. + /// + public int RequestRetryInterval + { + get + { + return this._requestRetryInterval; + } + set + { + if (this._requestRetryInterval != value && value >= 0) + { + this._requestRetryInterval = value; + this.OnPropertyChanged(); + } + } + } + /// /// Asynchronous event, triggered when the connection with OBS Studio is closed. /// @@ -462,6 +507,7 @@ protected virtual void Dispose(bool disposing) { this.Disconnect(); this._client.Dispose(); + this._receiver.Dispose(); } this._requests.Clear(); @@ -778,23 +824,36 @@ private async Task SendRequestAsync(object? requestData = null, [CallerMem private async Task SendRequestAndWaitAsync(string requestType, object? requestData = null) { - var requestId = Guid.NewGuid().ToString(); - var d = new { requestType, requestId, requestData }; - var op = (int)OpCode.Request; - var result = await this.SendAndWaitAsync(new { d, op }); - if (result is RequestResponseMessage requestResponseData) + int retryCount = 0; + while (true) { - if (requestResponseData.RequestStatus.Result) + var requestId = Guid.NewGuid().ToString(); + var d = new { requestType, requestId, requestData }; + var op = (int)OpCode.Request; + var result = await this.SendAndWaitAsync(new { d, op }); + if (result is RequestResponseMessage requestResponseData) { - return requestResponseData; + if (requestResponseData.RequestStatus.Result) + { + return requestResponseData; + } + else + { + if (requestResponseData.RequestStatus.Code == RequestStatusCode.NotReady && retryCount++ < this._maxRequestRetries) + { + Thread.Sleep(this._requestRetryInterval); + } + else + { + throw new ObsResponseException(requestResponseData.RequestStatus); + } + } } else { - throw new ObsResponseException(requestResponseData.RequestStatus); + throw new ObsClientException($"Unexpected response type {result?.GetType().Name} for request {requestId} in {MethodBase.GetCurrentMethod()?.Name}"); } } - - throw new ObsClientException($"Unexpected response type {result?.GetType().Name} for request {requestId} in {MethodBase.GetCurrentMethod()?.Name}"); } private void ReconnectTimerCallback(object? state) diff --git a/README.md b/README.md index 331e63e..23c9afe 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ Our intent is to create a complete and easy to use client ## Installation Install from the [NuGet Gallery](https://www.nuget.org/packages/OBSClient) -Or through the NuGet CLI: `NuGet\Install-Package OBSClient -Version 2.0.1` -From the command line: `dotnet add package OBSClient --version 2.0.1` +Or through the NuGet CLI: `NuGet\Install-Package OBSClient -Version 2.0.2` +From the command line: `dotnet add package OBSClient --version 2.0.2` ## Sample usage