build image cache in cpp dispEngine
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Hi.Disp;
|
||||
using Hi.Geom;
|
||||
using Hi.Native;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Hi.Webapi.Hubs;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Hi.Webapi.Services
|
||||
@@ -16,21 +17,23 @@ namespace Hi.Webapi.Services
|
||||
/// </summary>
|
||||
public ConcurrentDictionary<string, DispEngine> EngineDictionary { get; } = new();
|
||||
ILogger<RenderingService> Logger { get; }
|
||||
IHubContext<RenderingHub> HubContext { get; }
|
||||
private bool disposedValue;
|
||||
|
||||
public RenderingService(ILogger<RenderingService> logger)
|
||||
public RenderingService(ILogger<RenderingService> logger, IHubContext<RenderingHub> hubContext)
|
||||
{
|
||||
Logger = logger;
|
||||
HubContext = hubContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 創建或獲取一個 DispEngine 實例
|
||||
/// </summary>
|
||||
public DispEngine GetOrCreateEngine(string sessionId)
|
||||
/// <summary>
|
||||
/// Get Or Create an DispEngine.
|
||||
/// </summary>
|
||||
public DispEngine GetOrCreateEngine(string connectionId)
|
||||
{
|
||||
return EngineDictionary.GetOrAdd(sessionId, id =>
|
||||
return EngineDictionary.GetOrAdd(connectionId, id =>
|
||||
{
|
||||
Logger.LogInformation($"創建新的 DispEngine,SessionId: {id}");
|
||||
Logger.LogInformation($"Create new DispEngine,ConnectionId: {id}");
|
||||
var engine = new DispEngine();
|
||||
engine.BackgroundColor = new Vec3d(0.1, 0.1, 0.5);
|
||||
engine.BackgroundOpacity = 0.1;
|
||||
@@ -39,25 +42,38 @@ namespace Hi.Webapi.Services
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除指定的 DispEngine
|
||||
/// Send image to specific client.
|
||||
/// </summary>
|
||||
public bool RemoveEngine(string sessionId)
|
||||
public async Task SendImageToClient(string connectionId, byte[] compressedData, int originalLength, int width, int height)
|
||||
{
|
||||
if (EngineDictionary.TryRemove(sessionId, out var engine))
|
||||
try
|
||||
{
|
||||
Logger.LogInformation($"移除 DispEngine,SessionId: {sessionId}");
|
||||
await HubContext.Clients.Client(connectionId).SendAsync("ImageUpdate",
|
||||
compressedData, originalLength, width, height);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, $"Failed to send image to client: {connectionId}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove DispEngine
|
||||
/// </summary>
|
||||
public bool RemoveEngine(string connectionId)
|
||||
{
|
||||
if (EngineDictionary.TryRemove(connectionId, out var engine))
|
||||
{
|
||||
Logger.LogInformation($"Remove DispEngine,ConnectionId: {connectionId}");
|
||||
|
||||
try
|
||||
{
|
||||
// 停止渲染
|
||||
engine.IsVisible = false;
|
||||
|
||||
// 釋放資源
|
||||
engine.Dispose();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, $"清理 DispEngine 時發生錯誤,SessionId: {sessionId}");
|
||||
Logger.LogError(ex, $"清理 DispEngine 時發生錯誤,ConnectionId: {connectionId}");
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -65,10 +81,10 @@ namespace Hi.Webapi.Services
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 獲取當前活動的引擎數量
|
||||
/// </summary>
|
||||
public int GetActiveEngineCount() => EngineDictionary.Count;
|
||||
/// <summary>
|
||||
/// Get Active Engine Count.
|
||||
/// </summary>
|
||||
public int GetActiveEngineCount() => EngineDictionary.Count;
|
||||
/// <inheritdoc/>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
@@ -85,7 +101,7 @@ namespace Hi.Webapi.Services
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex, $"Dispose 時清理引擎錯誤,SessionId: {kvp.Key}");
|
||||
Logger.LogError(ex, $"Dispose DispEngine Error,ConnectionId: {kvp.Key}");
|
||||
}
|
||||
}
|
||||
EngineDictionary.Clear();
|
||||
@@ -102,4 +118,4 @@ namespace Hi.Webapi.Services
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user