降低CPU使用率,处置好因降低CPU使用率带来的颜色偏差

This commit is contained in:
2026-01-31 10:43:41 +08:00
parent 6661edfc44
commit 4afbf06439
17 changed files with 360 additions and 64 deletions

View File

@@ -24,23 +24,41 @@ namespace SHH.MjpegPlayer
/// <returns></returns>
public static MjpegConfig LoadConfig()
{
// [修复] 路径处理脆弱性:使用 BaseDirectory 拼接,避免相对路径替换的风险
// 生产环境:强制使用绝对路径确保能找到配置文件
if (!Debugger.IsAttached)
try
{
JsonConfigUris.MjpegConfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(JsonConfigUris.MjpegConfig));
}
// [修复] 路径处理脆弱性:使用 BaseDirectory 拼接,避免相对路径替换的风险
// 生产环境:强制使用绝对路径确保能找到配置文件
if (!Debugger.IsAttached)
{
if (!string.IsNullOrEmpty(JsonConfigUris.MjpegConfig))
JsonConfigUris.MjpegConfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(JsonConfigUris.MjpegConfig));
}
// 加载配置文件
var cfg = JsonConfig.Load<MjpegConfig>(JsonConfigUris.MjpegConfig);
if (cfg == null)
{
cfg = new MjpegConfig();
JsonConfig.Save(cfg, JsonConfigUris.MjpegConfig, "MjpegServer配置项");
_sysLog.Warning("未找到配置文件,已生成默认配置: {Path}", JsonConfigUris.MjpegConfig);
// 加载配置文件
MjpegConfig? cfg = null;
if (!string.IsNullOrEmpty(JsonConfigUris.MjpegConfig))
{
cfg = JsonConfig.Load<MjpegConfig>(JsonConfigUris.MjpegConfig);
if (cfg == null)
{
cfg = new MjpegConfig();
JsonConfig.Save(cfg, JsonConfigUris.MjpegConfig, "MjpegServer配置项");
_sysLog.Warning("未找到配置文件,已生成默认配置: {Path}", JsonConfigUris.MjpegConfig);
}
MjpegStatics.Cfg = cfg;
}
if (cfg == null)
cfg = new MjpegConfig();
return cfg;
}
catch(Exception ex)
{
_sysLog.Error("加载配置文件失败.");
Console.ReadLine();
return new MjpegConfig();
}
MjpegStatics.Cfg = cfg;
return cfg;
}
#endregion

View File

@@ -10,11 +10,11 @@ public class MjpegConfig
= "0.0.0.0";
/// <summary>Mjpeg 服务端口开始</summary>
public int SvrMjpegPortBegin
public int SvrMjpegPortBegin { get; }
= 25031;
/// <summary>Mjpeg 服务端口结束</summary>
public int SvrMjpegPortEnd
public int SvrMjpegPortEnd { get; }
= 25300;
/// <summary>帧间隔, 单位毫秒 (值为 125, 每秒 8 帧)</summary>
@@ -22,7 +22,7 @@ public class MjpegConfig
= 125;
/// <summary>Mjpeg Wcf 接收图片接口</summary>
public int WcfPushImagePort
public int WcfPushImagePort { get; }
= 25030;
/// <summary>接收图片的服务器名称</summary>

View File

@@ -11,12 +11,15 @@ namespace SHH.MjpegPlayer
static void Main(string[] args)
{
InitTemporaryLog();
_sysLog.Information("MjpegPlayer 正在初始化...");
var builder = WebApplication.CreateBuilder(args);
// 1. 注册 gRpc 服务
builder.Services.AddGrpc(options => {
builder.Services.AddGrpc(options =>
{
options.MaxReceiveMessageSize = 10 * 1024 * 1024; // 针对工业视频流,建议放宽至 10MB
});
@@ -39,6 +42,19 @@ namespace SHH.MjpegPlayer
app.Run("http://0.0.0.0:9002");
}
/// <summary>
/// [新增] 临时日志初始化
/// </summary>
private static void InitTemporaryLog()
{
// 在未读取到 MjpegConfig 前,先使用默认参数启动日志
LogBootstrapper.Init(new LogOptions
{
AppId = "MjpegPlayer",
LogRootPath = @"D:\Logs",
ConsoleLevel = Serilog.Events.LogEventLevel.Information
});
}
#region StartServer
@@ -73,6 +89,9 @@ namespace SHH.MjpegPlayer
catch (Exception ex)
{
//Logs.LogCritical<Program>(ex.Message, ex.StackTrace);
Console.WriteLine(ex.ToString());
Console.ReadLine();
// 退出应用
Bootstrapper.ExitApp("应用程序崩溃.");
}

View File

@@ -5,6 +5,9 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PlatformTarget>x64</PlatformTarget>
<PackageIcon>notifyIcon.ico</PackageIcon>
<ApplicationIcon>notifyIcon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
@@ -13,6 +16,10 @@
<None Remove="Bat\**" />
</ItemGroup>
<ItemGroup>
<Content Include="notifyIcon.ico" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CoreWCF.Http" Version="1.8.0" />
<PackageReference Include="CoreWCF.Primitives" Version="1.8.0" />
@@ -25,4 +32,11 @@
<ProjectReference Include="..\SHH.Contracts.Grpc\SHH.Contracts.Grpc.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="notifyIcon.ico">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
</Project>

View File

@@ -1,4 +1,6 @@
using System.Net;
using Ayay.SerilogLogs;
using Serilog;
using System.Net;
using System.Net.Sockets;
namespace SHH.MjpegPlayer
@@ -8,6 +10,8 @@ namespace SHH.MjpegPlayer
/// </summary>
public class MjpegServer
{
private static readonly ILogger _sysLog = Log.ForContext("SourceContext", LogModules.Core);
// [修复] 静态列表管理监听器,支持优雅停止
private static readonly List<TcpListener> _listeners = new List<TcpListener>();
private static readonly object _lock = new object();
@@ -36,9 +40,10 @@ namespace SHH.MjpegPlayer
var server = new TcpListener(ipAddress, port);
lock (_lock) _listeners.Add(server);
server.Start();
// Logs.LogInformation...
_sysLog.Information($"启动服务成功,端口:{port}");
try
{

Binary file not shown.

After

Width:  |  Height:  |  Size: 133 KiB