Files
Ayay/SHH.MjpegPlayer/Program.cs

126 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Ayay.SerilogLogs;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
namespace SHH.MjpegPlayer
{
internal class Program
{
private static ILogger _sysLog = Log.ForContext("SourceContext", LogModules.Core);
static void Main(string[] args)
{
InitTemporaryLog();
_sysLog.Information("MjpegPlayer 正在初始化...");
var builder = WebApplication.CreateBuilder(args);
// 1. 注册 gRpc 服务
builder.Services.AddGrpc(options =>
{
options.MaxReceiveMessageSize = 10 * 1024 * 1024; // 针对工业视频流,建议放宽至 10MB
});
// 2. 注册业务单例(如你之前的 Handler
builder.Services.AddSingleton<DeviceConfigHandler>();
builder.Services.AddSingleton<DeviceStatusHandler>();
var app = builder.Build();
// 3. 映射服务路(将逻辑与端口绑定)
app.MapGrpcService<GatewayService>();
new Thread(StartServer).Start();
GrpcServerManager.Start();
// 4. 启动监听(代替 Console.ReadLine
// 建议端口与 CameraService 配置的 9002 保持一致
_sysLog.Information("MjpegPlayer gRPC 服务启动于端口 9002");
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
/// <summary>
/// 开启服务监听
/// </summary>
static void StartServer()
{
try
{
// 加载配置文件
var cfg = Bootstrapper.LoadConfig();
// 检查 IP 与端口
Bootstrapper.ValidateEnvironment();
// 开启 Wcf 服务
StartWcfServer();
_sysLog.Information("WCF 推流接口已就绪, 端口: {Port}", cfg.WcfPushImagePort);
// 会话开启
// [修复] 端口循环逻辑:同样改为 <= 以匹配检测逻辑
for (var i = cfg.SvrMjpegPortBegin; i <= cfg.SvrMjpegPortEnd; i++)
{
MjpegServer.Start(i);
}
_sysLog.Information("MJPEG 服务池已开启: {Begin} -> {End}", cfg.SvrMjpegPortBegin, cfg.SvrMjpegPortEnd);
// 开启 RTMP 服务
RtmpPushServer.Instance.Start();
}
catch (Exception ex)
{
//Logs.LogCritical<Program>(ex.Message, ex.StackTrace);
Console.WriteLine(ex.ToString());
Console.ReadLine();
// 退出应用
Bootstrapper.ExitApp("应用程序崩溃.");
}
}
#endregion
#region StartWcfServer
/// <summary>
/// 开启 Wcf 服务
/// </summary>
private static void StartWcfServer()
{
try
{
var cfg = MjpegStatics.Cfg;
Bootstrapper.StartWcfEngine(cfg);
}
catch (Exception ex)
{
_sysLog.Fatal(ex, "应用程序崩溃.");
// 退出应用
Bootstrapper.ExitApp("应用程序崩溃.");
}
}
#endregion
}
}