2024-06-16 20:19:31 -04:00
|
|
|
using System.Text.Json;
|
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Serilog;
|
|
|
|
using Serilog.Context;
|
|
|
|
using TwitchChatTTS.OBS.Socket.Data;
|
|
|
|
using TwitchChatTTS.OBS.Socket.Manager;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.OBS.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class RequestBatchResponseHandler : IWebSocketHandler
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly IWebSocketHandler _requestResponseHandler;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
public int OperationCode { get; } = 9;
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
public RequestBatchResponseHandler(
|
|
|
|
[FromKeyedServices("obs-requestresponse")] IWebSocketHandler requestResponseHandler,
|
|
|
|
ILogger logger
|
|
|
|
)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_requestResponseHandler = requestResponseHandler;
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data data)
|
|
|
|
{
|
|
|
|
if (data is not RequestBatchResponseMessage message || message == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
using (LogContext.PushProperty("obsrid", message.RequestId))
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
var results = message.Results.ToList();
|
|
|
|
_logger.Debug($"Received request batch response of {results.Count} messages.");
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
int count = results.Count;
|
2024-06-16 20:19:31 -04:00
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
if (results[i] == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
try
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger.Debug($"Request response from OBS request batch #{i + 1}/{count}: {results[i]}");
|
|
|
|
var response = JsonSerializer.Deserialize<RequestResponseMessage>(results[i].ToString(), new JsonSerializerOptions()
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
PropertyNameCaseInsensitive = false,
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
|
|
});
|
|
|
|
if (response == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
await _requestResponseHandler.Execute(sender, response);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Error(ex, "Failed to process an item in a request batch message.");
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Debug($"Finished processing all request in this batch.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|