Skip to content

Commit

Permalink
feat: 支持Cli通过server info输出服务器信息
Browse files Browse the repository at this point in the history
fix: 未发送的消息仍会输出和触发计数器
  • Loading branch information
Zaitonn committed Nov 21, 2024
1 parent 66afcfb commit 930eec8
Show file tree
Hide file tree
Showing 70 changed files with 552 additions and 236 deletions.
112 changes: 63 additions & 49 deletions src/Serein.Cli/Services/Interaction/Handlers/ServerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,7 @@ public override void Invoke(IReadOnlyList<string> args)

if (args[1].Equals("list", StringComparison.InvariantCultureIgnoreCase))
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"当前共{_serverManager.Servers.Count}个服务器配置");
foreach (var kv in _serverManager.Servers)
{
stringBuilder.AppendLine($"▢ {kv.Value.Configuration.Name}");
stringBuilder.AppendLine($" ▫ Id: {kv.Key}");
stringBuilder.AppendLine($" ▫ 状态: " + (kv.Value.Status ? "运行中" : "已停止"));
stringBuilder.AppendLine(
" ▫ 启动命令行:"
+ kv.Value.Configuration.FileName
+ " "
+ kv.Value.Configuration.Argument
);
}

_logger.LogInformation(stringBuilder.ToString());
LogList();
return;
}

Expand Down Expand Up @@ -86,39 +71,7 @@ public override void Invoke(IReadOnlyList<string> args)
switch (args[1].ToLowerInvariant())
{
case "info" when args.Count <= 3:
var info = server.Info;

// var table = new Table();

// table
// .RoundedBorder()
// .AddColumns(
// new TableColumn("服务器状态") { Alignment = Justify.Center },
// new(
// server.Status switch
// {
// ServerStatus.Running => "[green3]●[/] 运行中",
// ServerStatus.Stopped => "[gray]●[/] 已关闭",
// ServerStatus.Unknown => "[gray]●[/] 未启动",
// _ => throw new NotSupportedException()
// }
// )
// {
// Alignment = Justify.Center
// }
// )
// .AddRow(
// "运行时长",
// info?.StartTime is null
// ? "-"
// : (DateTime.Now - info.StartTime).ToString() ?? "-"
// )
// .AddRow("CPU占用", (info?.CPUUsage ?? 0).ToString("N1") + "%")
// .AddRow("输入行数", info?.InputLines.ToString() ?? "-")
// .AddRow("输出行数", info?.OutputLines.ToString() ?? "-");

// AnsiConsole.Write(table);

LogServerInfo(server);
break;

case "start" when args.Count <= 3:
Expand Down Expand Up @@ -181,4 +134,65 @@ public override void Invoke(IReadOnlyList<string> args)
);
}
}

private void LogServerInfo(Server server)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"{server.Configuration.Name}({server.Id})");

if (server.Status)
{
stringBuilder.AppendLine($"▫ 状态: 运行中 (Pid={server.Pid})");
}
else
{
stringBuilder.AppendLine("▫ 状态: 已停止");
}

stringBuilder.AppendLine(
"▫ 启动命令行:" + server.Configuration.FileName + " " + server.Configuration.Argument
);

if (server.Info.StartTime is not null)
{
stringBuilder.AppendLine("▫ 启动时间:" + server.Info.StartTime.Value.ToString("G"));
}
else if (server.Info.ExitTime is not null)
{
stringBuilder.AppendLine("▫ 停止时间:" + server.Info.ExitTime.Value.ToString("G"));
}

if (server.Status && server.Info.Stat is not null && server.Info.Stat.ServerUp)
{
stringBuilder.AppendLine("▫ 版本:" + server.Info.Stat.Version);
stringBuilder.AppendLine(
"▫ 在线人数:"
+ server.Info.Stat.CurrentPlayers
+ "/"
+ server.Info.Stat.MaximumPlayers
);
}

_logger.LogInformation(stringBuilder.ToString());
}

private void LogList()
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"当前共{_serverManager.Servers.Count}个服务器配置");
foreach (var kv in _serverManager.Servers)
{
stringBuilder.AppendLine($"▢ {kv.Value.Configuration.Name}");
stringBuilder.AppendLine($" ▫ Id: {kv.Key}");
stringBuilder.AppendLine($" ▫ 状态: " + (kv.Value.Status ? "运行中" : "已停止"));
stringBuilder.AppendLine(
" ▫ 启动命令行:"
+ kv.Value.Configuration.FileName
+ " "
+ kv.Value.Configuration.Argument
);
}

_logger.LogInformation(stringBuilder.ToString());
}
}
4 changes: 3 additions & 1 deletion src/Serein.Cli/Services/Interaction/InputLoopService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public Task StartAsync(CancellationToken cancellationToken)
public Task StopAsync(CancellationToken cancellationToken)
{
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
return Task.CompletedTask;
}

Expand Down Expand Up @@ -88,7 +87,9 @@ out var server
var response = prompt.ReadLineAsync().Await();

if (response.IsSuccess)
{
ProcessInput(response.Text);
}
}
}
}
Expand All @@ -114,6 +115,7 @@ private void ProcessInput(string? input)
{
return;
}

if (!string.IsNullOrEmpty(_serverSwitcher.Value.CurrentId))
{
if (
Expand Down
4 changes: 4 additions & 0 deletions src/Serein.Cli/Services/Interaction/ServerSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.Extensions.Logging;

using Serein.Core.Models.Server;
using Serein.Core.Services.Data;
using Serein.Core.Services.Servers;

Expand Down Expand Up @@ -37,11 +38,14 @@ public void SwitchTo(string id)
{
throw new InvalidOperationException("选择的服务器不存在");
}

if (
!string.IsNullOrEmpty(CurrentId)
&& _serverManager.Servers.TryGetValue(CurrentId, out var oldServer)
)
{
oldServer.ServerOutput -= LogToConsole;
}

server.ServerOutput += LogToConsole;
CurrentId = id;
Expand Down
8 changes: 4 additions & 4 deletions src/Serein.Cli/Services/Loggers/ConnectionLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public void LogReceivedData(string data)
_logger.LogInformation("[Recv] {}", data);
}

public void LogSentPacket(string line)
public void LogSentMessage(string data)
{
_logger.LogInformation("[Sent] {}", line);
_logger.LogInformation("[Sent] {}", data);
}

public void LogSentData(string data)
public void LogSentData(string line)
{
_logger.LogInformation("[Sent] {}", data);
_logger.LogInformation("[Sent] {}", line);
}
}
4 changes: 4 additions & 0 deletions src/Serein.Cli/Utils/CliConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public static void WriteLine(LogLevel logLevel, string line)
);
}
else
{
Console.WriteLine($"{DateTime.Now:T} Debug {line}");
}
break;

case LogLevel.Information:
Expand All @@ -38,7 +40,9 @@ public static void WriteLine(LogLevel logLevel, string line)
);
}
else
{
Console.WriteLine($"{DateTime.Now:T} Info {line}");
}
break;

case LogLevel.Warning:
Expand Down
3 changes: 3 additions & 0 deletions src/Serein.Core/AppType.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace Serein.Core;

/// <summary>
/// 应用类型
/// </summary>
public enum AppType
{
Unknown,
Expand Down
6 changes: 6 additions & 0 deletions src/Serein.Core/Models/Commands/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,19 @@ var item in _exclusions.Split(
case "server":
MatchExclusion.Servers.Add(arg);
break;

case "group":
if (long.TryParse(arg, out var group))
{
MatchExclusion.Groups.Add(group);
}
break;

case "user":
if (long.TryParse(arg, out var user))
{
MatchExclusion.Users.Add(user);
}
break;
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/Serein.Core/Models/Output/IConnectionLogger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Text.Json.Nodes;

using Microsoft.Extensions.Logging;

namespace Serein.Core.Models.Output;
Expand All @@ -8,11 +6,11 @@ public interface IConnectionLogger
{
void Log(LogLevel level, string message);

void LogSentMessage(string data);

void LogReceivedMessage(string line);

void LogSentPacket(string line);
void LogSentData(string line);

void LogReceivedData(string data);

void LogSentData(string data);
}
9 changes: 8 additions & 1 deletion src/Serein.Core/Models/Plugins/Net/PluginBase.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,25 @@ internal Task Invoke(Event @event, params object[] args)

case Event.ServerOutput:
if (args.Length != 2)
{
ThrowArgumentException();
}

return OnServerOutput(args.First().OfType<Services.Servers.Server>(), args.Last().OfType<string>());

case Event.ServerRawOutput:
if (args.Length != 2)
{
ThrowArgumentException();
}

return OnServerRawOutput(args.First().OfType<Services.Servers.Server>(), args.Last().OfType<string>());

case Event.ServerInput:
if (args.Length != 2)
{
ThrowArgumentException();
}

return OnServerInput(args.First().OfType<Services.Servers.Server>(), args.Last().OfType<string>());

Expand All @@ -100,7 +106,9 @@ internal Task Invoke(Event @event, params object[] args)
|| args[1] is not int code
|| args[2] is not DateTime time
)
{
ThrowArgumentException();
}

return OnServerExited(server, code, time);

Expand All @@ -116,7 +124,6 @@ internal Task Invoke(Event @event, params object[] args)
case Event.PluginsUnloading:
return OnPluginsUnloading();


default:
throw new NotSupportedException();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Serein.Core/Models/Server/ServerInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Serein.Core.Models.Server;

public class ServerInfo : NotifyPropertyChangedModelBase, IServerInfo
internal class ServerInfo : NotifyPropertyChangedModelBase, IServerInfo
{
public string? FileName { get; internal set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Serein.Core.Services.Servers;
namespace Serein.Core.Models.Server;

public sealed class ServerOutputEventArgs(ServerOutputType outputType, string data) : EventArgs
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Serein.Core.Services.Servers;
namespace Serein.Core.Models.Server;

public enum ServerOutputType
{
Expand Down
Loading

0 comments on commit 930eec8

Please sign in to comment.