From 18b0ab307d56dd785a64b5b35558572e591542da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Thu, 18 Jul 2024 13:33:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=85=A8=E5=B1=80=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E8=B0=83=E5=BA=A6=E5=99=A8=E7=9A=84=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E8=80=85=EF=BC=8C=E5=80=9F=E5=8A=A9=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E6=97=B6=E9=97=B4=E5=B7=AE=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E8=8E=B7=E5=BE=97=E6=9B=B4=E5=87=86=E7=A1=AE=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E3=80=82=E9=81=BF=E5=85=8D=E6=9C=AC=E5=9C=B0=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=81=8F=E5=B7=AE=E5=AF=BC=E8=87=B4=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E6=89=A7=E8=A1=8C=E6=97=B6=E9=97=B4=E4=B8=8D?= =?UTF-8?q?=E5=87=86=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Samples/TestA/TestA.csproj | 2 +- Samples/TestB/TestB.csproj | 2 +- Stardust.Server/Controllers/NodeController.cs | 7 ++----- Stardust.Server/Services/NodeService.cs | 4 ++-- .../Areas/Registry/Controllers/AppController.cs | 2 +- Stardust/AppClient.cs | 4 +--- Stardust/Services/StarTimeProvider.cs | 12 ++++++++++++ Stardust/StarFactory.cs | 8 ++++++++ Stardust/Stardust.csproj | 4 ++-- Test/Test.csproj | 2 +- 10 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 Stardust/Services/StarTimeProvider.cs diff --git a/Samples/TestA/TestA.csproj b/Samples/TestA/TestA.csproj index 5941a00e..0277992b 100644 --- a/Samples/TestA/TestA.csproj +++ b/Samples/TestA/TestA.csproj @@ -18,7 +18,7 @@ - + diff --git a/Samples/TestB/TestB.csproj b/Samples/TestB/TestB.csproj index c518ca57..17baa4fc 100644 --- a/Samples/TestB/TestB.csproj +++ b/Samples/TestB/TestB.csproj @@ -18,7 +18,7 @@ - + diff --git a/Stardust.Server/Controllers/NodeController.cs b/Stardust.Server/Controllers/NodeController.cs index 99f16a80..36a4ed7f 100644 --- a/Stardust.Server/Controllers/NodeController.cs +++ b/Stardust.Server/Controllers/NodeController.cs @@ -203,8 +203,7 @@ public Int32 PostEvents(EventModel[] events) [HttpPost(nameof(Report))] public async Task Report(Int32 id) { - var node = _node; - if (node == null) throw new ApiException(401, "节点未登录"); + var node = _node ?? throw new ApiException(401, "节点未登录"); var cmd = NodeCommand.FindById(id); if (cmd != null && cmd.NodeID == node.ID) @@ -283,9 +282,7 @@ public async Task Notify() private async Task Handle(WebSocket socket, String token, String ip) { var (node, error) = _nodeService.DecodeToken(token, _setting.TokenSecret); - if (node == null) throw new ApiException(401, $"未登录![ip={ip}]"); - - _node = node; + _node = node ?? throw new ApiException(401, $"未登录![ip={ip}]"); if (error != null) throw error; var sid = Rand.Next(); diff --git a/Stardust.Server/Services/NodeService.cs b/Stardust.Server/Services/NodeService.cs index e060d3d3..fb55be59 100644 --- a/Stardust.Server/Services/NodeService.cs +++ b/Stardust.Server/Services/NodeService.cs @@ -191,8 +191,8 @@ private static Node CheckNode(Node node, NodeInfo di, String productCode, String // 网卡地址 if (di.Macs != node.MACs) { - var dims = di.Macs?.Split(",") ?? new String[0]; - var nodems = node.MACs?.Split(",") ?? new String[0]; + var dims = di.Macs?.Split(",") ?? []; + var nodems = node.MACs?.Split(",") ?? []; // 任意网卡匹配则通过 if (!nodems.Any(e => dims.Contains(e))) { diff --git a/Stardust.Web/Areas/Registry/Controllers/AppController.cs b/Stardust.Web/Areas/Registry/Controllers/AppController.cs index 63556874..106feab9 100644 --- a/Stardust.Web/Areas/Registry/Controllers/AppController.cs +++ b/Stardust.Web/Areas/Registry/Controllers/AppController.cs @@ -113,7 +113,7 @@ protected override IEnumerable Search(Pager p) if (id > 0) { var node = App.FindById(id); - if (node != null) return new[] { node }; + if (node != null) return [node]; } var projectId = p["projectId"].ToInt(-1); diff --git a/Stardust/AppClient.cs b/Stardust/AppClient.cs index e973a6aa..703d5909 100644 --- a/Stardust/AppClient.cs +++ b/Stardust/AppClient.cs @@ -58,8 +58,6 @@ public class AppClient : ClientBase, IRegistry private readonly ConcurrentDictionary _consumeServices = new(); private readonly ConcurrentDictionary _consumes = new(); private readonly ConcurrentDictionary> _consumeEvents = new(); - private readonly ConcurrentQueue _fails = new(); - private readonly ICache _cache = new MemoryCache(); #endregion #region 构造 @@ -439,7 +437,7 @@ public PublishServiceInfo Register(String serviceName, Func addressCall /// 回调方法 public void Bind(String serviceName, ServiceChangedCallback callback) { - var list = _consumeEvents.GetOrAdd(serviceName, k => new List()); + var list = _consumeEvents.GetOrAdd(serviceName, k => []); list.Add(callback); StartTimer(); diff --git a/Stardust/Services/StarTimeProvider.cs b/Stardust/Services/StarTimeProvider.cs new file mode 100644 index 00000000..f5c270f3 --- /dev/null +++ b/Stardust/Services/StarTimeProvider.cs @@ -0,0 +1,12 @@ +using NewLife.Remoting.Clients; + +namespace Stardust.Services; + +#if !NET40 +internal class StarTimeProvider : TimeProvider +{ + public ClientBase Client { get; set; } = null!; + + public override DateTimeOffset GetUtcNow() => Client != null ? Client.GetNow().ToUniversalTime() : base.GetUtcNow(); +} +#endif \ No newline at end of file diff --git a/Stardust/StarFactory.cs b/Stardust/StarFactory.cs index 7244c40a..4f978f39 100644 --- a/Stardust/StarFactory.cs +++ b/Stardust/StarFactory.cs @@ -14,10 +14,13 @@ using NewLife.Remoting.Clients; using NewLife.Remoting.Models; using NewLife.Security; +using NewLife.Threading; using Stardust.Configs; using Stardust.Models; using Stardust.Monitors; using Stardust.Registry; +using Stardust.Services; + #if NET45_OR_GREATER || NETCOREAPP || NETSTANDARD using TaskEx = System.Threading.Tasks.Task; #endif @@ -290,6 +293,11 @@ private Boolean Valid() Log = Log, }; +#if !NET40 + // 设置全局定时调度器的时间提供者,借助服务器时间差,以获得更准确的时间。避免本地时间偏差导致定时任务执行时间不准确 + TimerScheduler.GlobalTimeProvider = new StarTimeProvider { Client = client }; +#endif + //var set = StarSetting.Current; //if (set.Debug) client.Log = XTrace.Log; client.WriteInfoEvent("应用启动", $"pid={Process.GetCurrentProcess().Id}"); diff --git a/Stardust/Stardust.csproj b/Stardust/Stardust.csproj index 945f701d..e0a3484b 100644 --- a/Stardust/Stardust.csproj +++ b/Stardust/Stardust.csproj @@ -112,10 +112,10 @@ - + - + diff --git a/Test/Test.csproj b/Test/Test.csproj index 0ebd5be2..97fc8833 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -30,7 +30,7 @@ - +