tune design pattern of IProjectService
This commit is contained in:
parent
feb25e5871
commit
b87779ade7
@ -14,7 +14,6 @@ namespace Hi.Webapi.Hubs
|
|||||||
private readonly RenderingService _renderingService;
|
private readonly RenderingService _renderingService;
|
||||||
private readonly ILogger<RenderingHub> _logger;
|
private readonly ILogger<RenderingHub> _logger;
|
||||||
private readonly Dictionary<string, byte[]> _lastFrameCache = [];
|
private readonly Dictionary<string, byte[]> _lastFrameCache = [];
|
||||||
private readonly Dictionary<string, DateTime> _lastFrameTime = [];
|
|
||||||
private readonly Dictionary<string, Mat4d> _sketchViewCache = [];
|
private readonly Dictionary<string, Mat4d> _sketchViewCache = [];
|
||||||
|
|
||||||
public RenderingHub(RenderingService renderingService, ILogger<RenderingHub> logger)
|
public RenderingHub(RenderingService renderingService, ILogger<RenderingHub> logger)
|
||||||
@ -42,7 +41,6 @@ namespace Hi.Webapi.Hubs
|
|||||||
var clientProxy = Clients.Caller;
|
var clientProxy = Clients.Caller;
|
||||||
var logger = _logger;
|
var logger = _logger;
|
||||||
var frameCount = 0;
|
var frameCount = 0;
|
||||||
byte[] preImageRgba = null;
|
|
||||||
|
|
||||||
// 設置渲染回調
|
// 設置渲染回調
|
||||||
unsafe
|
unsafe
|
||||||
@ -52,17 +50,6 @@ namespace Hi.Webapi.Hubs
|
|||||||
if (bgra == null)
|
if (bgra == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// 幀率限制 - 最多每秒30幀
|
|
||||||
if (_lastFrameTime.TryGetValue(sessionId, out var lastTime))
|
|
||||||
{
|
|
||||||
var elapsed = DateTime.Now - lastTime;
|
|
||||||
if (elapsed.TotalMilliseconds < 33) // 30 FPS
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_lastFrameTime[sessionId] = DateTime.Now;
|
|
||||||
|
|
||||||
frameCount++;
|
frameCount++;
|
||||||
if (frameCount <= 5) // 只記錄前5幀
|
if (frameCount <= 5) // 只記錄前5幀
|
||||||
{
|
{
|
||||||
@ -89,17 +76,21 @@ namespace Hi.Webapi.Hubs
|
|||||||
rgba[i * 4 + 3] = bgra[i * 4 + 3];
|
rgba[i * 4 + 3] = bgra[i * 4 + 3];
|
||||||
}
|
}
|
||||||
|
|
||||||
// 檢查圖像是否有變化
|
if (_lastFrameCache.TryGetValue(
|
||||||
if (preImageRgba != null && preImageRgba.SequenceEqual(rgba))
|
sessionId, out byte[] preImageRgba))
|
||||||
|
{
|
||||||
|
if (preImageRgba != null &&
|
||||||
|
preImageRgba.SequenceEqual(rgba))
|
||||||
return;
|
return;
|
||||||
preImageRgba = rgba;
|
}
|
||||||
|
_lastFrameCache[sessionId] = rgba;
|
||||||
|
|
||||||
|
|
||||||
if (frameCount <= 5)
|
if (frameCount <= 5)
|
||||||
{
|
{
|
||||||
logger.LogInformation($"Frame {frameCount} sending - Size: {w}x{h}, Data: {rgba.Length} bytes");
|
logger.LogInformation($"Frame {frameCount} sending - Size: {w}x{h}, Data: {rgba.Length} bytes");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 壓縮數據
|
|
||||||
using MemoryStream dstMemoryStream = new MemoryStream();
|
using MemoryStream dstMemoryStream = new MemoryStream();
|
||||||
using GZipStream gZipStream = new GZipStream(dstMemoryStream, CompressionMode.Compress);
|
using GZipStream gZipStream = new GZipStream(dstMemoryStream, CompressionMode.Compress);
|
||||||
using MemoryStream srcMemoryStream = new MemoryStream(rgba);
|
using MemoryStream srcMemoryStream = new MemoryStream(rgba);
|
||||||
@ -447,7 +438,6 @@ namespace Hi.Webapi.Hubs
|
|||||||
|
|
||||||
// 清理緩存
|
// 清理緩存
|
||||||
_lastFrameCache.Remove(sessionId);
|
_lastFrameCache.Remove(sessionId);
|
||||||
_lastFrameTime.Remove(sessionId);
|
|
||||||
_sketchViewCache.Remove(sessionId);
|
_sketchViewCache.Remove(sessionId);
|
||||||
|
|
||||||
_renderingService.RemoveEngine(sessionId);
|
_renderingService.RemoveEngine(sessionId);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user