tune
This commit is contained in:
@@ -6,16 +6,21 @@ using System.Collections.Concurrent;
|
||||
namespace Hi.Webapi.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 管理 DispEngine 實例和渲染操作的服務
|
||||
/// Singleton Service for managing <see cref="DispEngine"/>.
|
||||
/// </summary>
|
||||
public class RenderingService : IDisposable
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, DispEngine> _engines = new();
|
||||
private readonly ILogger<RenderingService> _logger;
|
||||
/// <summary>
|
||||
/// Engine Dictionary.
|
||||
/// Key is sessionID.
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<string, DispEngine> EngineDictionary { get; } = new();
|
||||
ILogger<RenderingService> Logger { get; }
|
||||
private bool disposedValue;
|
||||
|
||||
public RenderingService(ILogger<RenderingService> logger)
|
||||
public RenderingService(ILogger<RenderingService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -23,9 +28,9 @@ namespace Hi.Webapi.Services
|
||||
/// </summary>
|
||||
public DispEngine GetOrCreateEngine(string sessionId)
|
||||
{
|
||||
return _engines.GetOrAdd(sessionId, id =>
|
||||
return EngineDictionary.GetOrAdd(sessionId, id =>
|
||||
{
|
||||
_logger.LogInformation($"創建新的 DispEngine,SessionId: {id}");
|
||||
Logger.LogInformation($"創建新的 DispEngine,SessionId: {id}");
|
||||
var engine = new DispEngine();
|
||||
engine.BackgroundColor = new Vec3d(0.1, 0.1, 0.5);
|
||||
engine.BackgroundOpacity = 0.1;
|
||||
@@ -38,9 +43,9 @@ namespace Hi.Webapi.Services
|
||||
/// </summary>
|
||||
public bool RemoveEngine(string sessionId)
|
||||
{
|
||||
if (_engines.TryRemove(sessionId, out var engine))
|
||||
if (EngineDictionary.TryRemove(sessionId, out var engine))
|
||||
{
|
||||
_logger.LogInformation($"移除 DispEngine,SessionId: {sessionId}");
|
||||
Logger.LogInformation($"移除 DispEngine,SessionId: {sessionId}");
|
||||
|
||||
try
|
||||
{
|
||||
@@ -52,7 +57,7 @@ namespace Hi.Webapi.Services
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"清理 DispEngine 時發生錯誤,SessionId: {sessionId}");
|
||||
Logger.LogError(ex, $"清理 DispEngine 時發生錯誤,SessionId: {sessionId}");
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -63,23 +68,38 @@ namespace Hi.Webapi.Services
|
||||
/// <summary>
|
||||
/// 獲取當前活動的引擎數量
|
||||
/// </summary>
|
||||
public int GetActiveEngineCount() => _engines.Count;
|
||||
public int GetActiveEngineCount() => EngineDictionary.Count;
|
||||
/// <inheritdoc/>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
foreach (var kvp in EngineDictionary)
|
||||
{
|
||||
try
|
||||
{
|
||||
kvp.Value.IsVisible = false;
|
||||
kvp.Value.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, $"Dispose 時清理引擎錯誤,SessionId: {kvp.Key}");
|
||||
}
|
||||
}
|
||||
EngineDictionary.Clear();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var kvp in _engines)
|
||||
{
|
||||
try
|
||||
{
|
||||
kvp.Value.IsVisible = false;
|
||||
kvp.Value.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"Dispose 時清理引擎錯誤,SessionId: {kvp.Key}");
|
||||
}
|
||||
}
|
||||
_engines.Clear();
|
||||
}
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user