2024-10-29 16:28:14 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2024-09-30 17:10:43 +08:00
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
|
2024-11-13 17:50:52 +08:00
|
|
|
|
namespace JiShe.CollectBus.Network.Plugins
|
2024-09-30 17:10:43 +08:00
|
|
|
|
{
|
2024-11-13 17:50:52 +08:00
|
|
|
|
public partial class ServerMonitor(ILogger<ServerMonitor> logger) : PluginBase
|
2024-09-30 17:10:43 +08:00
|
|
|
|
{
|
2024-10-22 20:57:26 +08:00
|
|
|
|
[GeneratorPlugin(typeof(IServerStartedPlugin))]
|
2024-09-30 17:10:43 +08:00
|
|
|
|
public Task OnServerStarted(IServiceBase sender, ServiceStateEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (sender)
|
|
|
|
|
|
{
|
|
|
|
|
|
case ITcpService service:
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var item in service.Monitors)
|
|
|
|
|
|
{
|
2024-10-29 16:28:14 +08:00
|
|
|
|
logger.LogInformation($"TCP {item.Option.IpHost}");
|
2024-09-30 17:10:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
case UdpSession session:
|
2024-10-29 16:28:14 +08:00
|
|
|
|
logger.LogInformation($"UDP {session.Monitor.IPHost}");
|
2024-09-30 17:10:43 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-29 16:28:14 +08:00
|
|
|
|
logger.LogInformation(e.ServerState == ServerState.Running
|
2024-09-30 17:10:43 +08:00
|
|
|
|
? $"服务器成功启动"
|
|
|
|
|
|
: $"服务器启动失败,状态:{e.ServerState},异常:{e.Exception}");
|
|
|
|
|
|
return e.InvokeNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-22 20:57:26 +08:00
|
|
|
|
[GeneratorPlugin(typeof(IServerStopedPlugin))]
|
2024-10-29 16:28:14 +08:00
|
|
|
|
public Task OnServerStoped(IServiceBase sender,ServiceStateEventArgs e)
|
2024-09-30 17:10:43 +08:00
|
|
|
|
{
|
2024-10-29 16:28:14 +08:00
|
|
|
|
logger.LogInformation("服务已停止");
|
2024-09-30 17:10:43 +08:00
|
|
|
|
return e.InvokeNext();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|