init
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using Hi.Disp;
|
||||
using Hi.Geom;
|
||||
using Hi.Native;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Hi.Webapi.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 管理 DispEngine 實例和渲染操作的服務
|
||||
/// </summary>
|
||||
public class RenderingService : IDisposable
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, DispEngine> _engines = new();
|
||||
private readonly ILogger<RenderingService> _logger;
|
||||
|
||||
public RenderingService(ILogger<RenderingService> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 創建或獲取一個 DispEngine 實例
|
||||
/// </summary>
|
||||
public DispEngine GetOrCreateEngine(string sessionId)
|
||||
{
|
||||
return _engines.GetOrAdd(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;
|
||||
return engine;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除指定的 DispEngine
|
||||
/// </summary>
|
||||
public bool RemoveEngine(string sessionId)
|
||||
{
|
||||
if (_engines.TryRemove(sessionId, out var engine))
|
||||
{
|
||||
_logger.LogInformation($"移除 DispEngine,SessionId: {sessionId}");
|
||||
|
||||
try
|
||||
{
|
||||
// 停止渲染
|
||||
engine.IsVisible = false;
|
||||
|
||||
// 釋放資源
|
||||
engine.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"清理 DispEngine 時發生錯誤,SessionId: {sessionId}");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 獲取當前活動的引擎數量
|
||||
/// </summary>
|
||||
public int GetActiveEngineCount() => _engines.Count;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user