mirror of
https://github.com/nsnail/dot.git
synced 2025-06-17 21:13:21 +08:00
Merge branch 'tk' of https://github.com/nsnail/dot into tk
This commit is contained in:
commit
92881f6f66
@ -1,7 +1,6 @@
|
|||||||
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:s="clr-namespace:System;assembly=mscorlib"
|
xmlns:s="clr-namespace:System;assembly=mscorlib"
|
||||||
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve">
|
||||||
xml:space="preserve">
|
|
||||||
<s:String x:Key="/Default/CodeStyle/CSharpFileLayoutPatterns/Pattern/@EntryValue"><?xml version="1.0" encoding="utf-16"?>
|
<s:String x:Key="/Default/CodeStyle/CSharpFileLayoutPatterns/Pattern/@EntryValue"><?xml version="1.0" encoding="utf-16"?>
|
||||||
<Patterns xmlns="urn:schemas-jetbrains-com:member-reordering-patterns">
|
<Patterns xmlns="urn:schemas-jetbrains-com:member-reordering-patterns">
|
||||||
<TypePattern>
|
<TypePattern>
|
||||||
@ -11,6 +10,7 @@
|
|||||||
<Kind.Order>
|
<Kind.Order>
|
||||||
<DeclarationKind>Interface</DeclarationKind>
|
<DeclarationKind>Interface</DeclarationKind>
|
||||||
<DeclarationKind>Class</DeclarationKind>
|
<DeclarationKind>Class</DeclarationKind>
|
||||||
|
<DeclarationKind>Record</DeclarationKind>
|
||||||
<DeclarationKind>Enum</DeclarationKind>
|
<DeclarationKind>Enum</DeclarationKind>
|
||||||
<DeclarationKind>Struct</DeclarationKind>
|
<DeclarationKind>Struct</DeclarationKind>
|
||||||
<DeclarationKind>Delegate</DeclarationKind>
|
<DeclarationKind>Delegate</DeclarationKind>
|
||||||
|
9
src/Lang/Str.Designer.cs
generated
9
src/Lang/Str.Designer.cs
generated
@ -284,6 +284,15 @@ namespace Dot.Lang {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Looks up a localized string similar to NTP 标准网络时钟: {0}.
|
||||||
|
/// </summary>
|
||||||
|
public static string ServerTime {
|
||||||
|
get {
|
||||||
|
return ResourceManager.GetString("ServerTime", resourceCulture);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Looks up a localized string similar to 已读取:{0}/{1},处理:{2},跳过:{3}.
|
/// Looks up a localized string similar to 已读取:{0}/{1},处理:{2},跳过:{3}.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
196
src/Time/Main.cs
196
src/Time/Main.cs
@ -1,3 +1,4 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
@ -6,6 +7,13 @@ namespace Dot.Time;
|
|||||||
|
|
||||||
public sealed class Main : Tool<Option>
|
public sealed class Main : Tool<Option>
|
||||||
{
|
{
|
||||||
|
private record Server
|
||||||
|
{
|
||||||
|
public int ConsoleRowIndex;
|
||||||
|
public TimeSpan Offset;
|
||||||
|
public ServerStatues Status;
|
||||||
|
}
|
||||||
|
|
||||||
private enum ServerStatues : byte
|
private enum ServerStatues : byte
|
||||||
{
|
{
|
||||||
Ready
|
Ready
|
||||||
@ -27,41 +35,76 @@ public sealed class Main : Tool<Option>
|
|||||||
[FieldOffset(0)] public ushort wYear;
|
[FieldOffset(0)] public ushort wYear;
|
||||||
}
|
}
|
||||||
|
|
||||||
private const int _MAX_DEGREE_OF_PARALLELISM = 10;
|
private const int _MAX_DEGREE_OF_PARALLELISM = 10;
|
||||||
private const int _NTP_PORT = 123;
|
private const int _NTP_PORT = 123;
|
||||||
private readonly char[] _loading = { '-', '\\', '|', '/' };
|
private const string _OUTPUT_TEMP = "{0,-30} {1,20} {2,20}";
|
||||||
private int _procedCnt;
|
private static readonly object _lockObj = new();
|
||||||
private readonly int _serverCnt;
|
private int _procedCnt;
|
||||||
|
private readonly int _serverCnt;
|
||||||
|
|
||||||
private readonly Dictionary<string, Server> _serverDictionary;
|
private readonly string[] _srvAddr = {
|
||||||
|
"ntp.ntsc.ac.cn", "cn.ntp.org.cn", "edu.ntp.org.cn", "cn.pool.ntp.org"
|
||||||
|
, "time.pool.aliyun.com", "time1.aliyun.com", "time2.aliyun.com"
|
||||||
|
, "time3.aliyun.com", "time4.aliyun.com", "time5.aliyun.com"
|
||||||
|
, "time6.aliyun.com", "time7.aliyun.com", "time1.cloud.tencent.com"
|
||||||
|
, "time2.cloud.tencent.com", "time3.cloud.tencent.com"
|
||||||
|
, "time4.cloud.tencent.com", "time5.cloud.tencent.com", "ntp.sjtu.edu.cn"
|
||||||
|
, "ntp.neu.edu.cn", "ntp.bupt.edu.cn", "ntp.shu.edu.cn", "pool.ntp.org"
|
||||||
|
, "0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org"
|
||||||
|
, "asia.pool.ntp.org", "time1.google.com", "time2.google.com"
|
||||||
|
, "time3.google.com", "time4.google.com", "time.apple.com", "time1.apple.com"
|
||||||
|
, "time2.apple.com", "time3.apple.com", "time4.apple.com", "time5.apple.com"
|
||||||
|
, "time6.apple.com", "time7.apple.com", "time.windows.com", "time.nist.gov"
|
||||||
|
, "time-nw.nist.gov", "time-a.nist.gov", "time-b.nist.gov", "stdtime.gov.hk"
|
||||||
|
};
|
||||||
|
|
||||||
|
private readonly Dictionary<string, Server> _srvStatus;
|
||||||
private readonly string[] _serverDomains = {
|
private int _successCnt;
|
||||||
"ntp.ntsc.ac.cn", "cn.ntp.org.cn", "edu.ntp.org.cn"
|
|
||||||
, "cn.pool.ntp.org", "time.pool.aliyun.com", "time1.aliyun.com"
|
|
||||||
, "time2.aliyun.com", "time3.aliyun.com", "time4.aliyun.com"
|
|
||||||
, "time5.aliyun.com", "time6.aliyun.com", "time7.aliyun.com"
|
|
||||||
, "time1.cloud.tencent.com", "time2.cloud.tencent.com"
|
|
||||||
, "time3.cloud.tencent.com", "time4.cloud.tencent.com"
|
|
||||||
, "time5.cloud.tencent.com", "ntp.sjtu.edu.cn", "ntp.neu.edu.cn"
|
|
||||||
, "ntp.bupt.edu.cn", "ntp.shu.edu.cn", "pool.ntp.org", "0.pool.ntp.org"
|
|
||||||
, "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org"
|
|
||||||
, "asia.pool.ntp.org", "time1.google.com", "time2.google.com"
|
|
||||||
, "time3.google.com", "time4.google.com", "time.apple.com"
|
|
||||||
, "time1.apple.com", "time2.apple.com", "time3.apple.com"
|
|
||||||
, "time4.apple.com", "time5.apple.com", "time6.apple.com"
|
|
||||||
, "time7.apple.com", "time.windows.com", "time.nist.gov"
|
|
||||||
, "time-nw.nist.gov", "time-a.nist.gov", "time-b.nist.gov"
|
|
||||||
, "stdtime.gov.hk"
|
|
||||||
};
|
|
||||||
|
|
||||||
private int _successCnt;
|
|
||||||
|
|
||||||
|
|
||||||
public Main(Option opt) : base(opt)
|
public Main(Option opt) : base(opt)
|
||||||
{
|
{
|
||||||
_serverCnt = _serverDomains.Length;
|
_serverCnt = _srvAddr.Length;
|
||||||
_serverDictionary = _serverDomains.ToDictionary(x => x, _ => new Server { Status = ServerStatues.Ready });
|
var i = 0;
|
||||||
|
_srvStatus = _srvAddr.ToDictionary(
|
||||||
|
x => x, _ => new Server { Status = ServerStatues.Ready, ConsoleRowIndex = ++i });
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ChangeStatus(KeyValuePair<string, Server> server, ServerStatues status
|
||||||
|
, TimeSpan offset = default)
|
||||||
|
{
|
||||||
|
server.Value.Status = status;
|
||||||
|
server.Value.Offset = offset;
|
||||||
|
DrawTextInConsole(0, server.Value.ConsoleRowIndex
|
||||||
|
, string.Format(_OUTPUT_TEMP, server.Key, server.Value.Status
|
||||||
|
, status == ServerStatues.Succeed ? server.Value.Offset : string.Empty));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task DrawLoading()
|
||||||
|
{
|
||||||
|
char[] loading = { '-', '\\', '|', '/' };
|
||||||
|
var loadingIndex = 0;
|
||||||
|
while (true) {
|
||||||
|
if (Volatile.Read(ref _procedCnt) == _serverCnt) break;
|
||||||
|
await Task.Delay(100);
|
||||||
|
++loadingIndex;
|
||||||
|
for (var i = 0; i != _serverCnt; ++i)
|
||||||
|
DrawTextInConsole(
|
||||||
|
34, i + 1
|
||||||
|
, _srvStatus[_srvAddr[i]].Status is ServerStatues.Succeed or ServerStatues.Failed
|
||||||
|
? " "
|
||||||
|
: loading[loadingIndex % 4].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
Debug.WriteLine(Environment.CurrentManagedThreadId + ":" + DateTime.Now.ToString("O"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void DrawTextInConsole(int left, int top, string text)
|
||||||
|
{
|
||||||
|
lock (_lockObj) {
|
||||||
|
Console.SetCursorPosition(left, top);
|
||||||
|
Console.Write(text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -102,30 +145,35 @@ public sealed class Main : Tool<Option>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void Printing()
|
private void PrintTemplate()
|
||||||
{
|
{
|
||||||
const string outputTemp = "{0,-30}\t{1}\t{2,20}\t{3,20}";
|
|
||||||
var rolling = 0;
|
|
||||||
|
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
while (true) {
|
Console.CursorVisible = false;
|
||||||
await Task.Delay(100);
|
var row = //
|
||||||
Console.SetCursorPosition(0, 0);
|
_srvStatus.Select(x //
|
||||||
var row = //
|
=> string.Format(_OUTPUT_TEMP, x.Key, x.Value.Status
|
||||||
_serverDictionary.Select(x //
|
, x.Value.Offset == TimeSpan.Zero ? string.Empty : x.Value.Offset));
|
||||||
=> string.Format(outputTemp, x.Key
|
|
||||||
, x.Value.Status == ServerStatues.Connecting
|
|
||||||
? _loading[++rolling % 4]
|
|
||||||
: ' ', x.Value.Status
|
|
||||||
, x.Value.Offset == TimeSpan.Zero
|
|
||||||
? string.Empty
|
|
||||||
: x.Value.Offset));
|
|
||||||
|
|
||||||
|
|
||||||
Console.WriteLine(outputTemp, Str.Server, ' ', Str.Status, Str.LocalClockOffset);
|
Console.WriteLine(_OUTPUT_TEMP, Str.Server, Str.Status, Str.LocalClockOffset);
|
||||||
Console.WriteLine(string.Join(Environment.NewLine, row));
|
Console.WriteLine(string.Join(Environment.NewLine, row));
|
||||||
if (_procedCnt == _serverCnt) break;
|
}
|
||||||
|
|
||||||
|
private ValueTask ServerHandle(KeyValuePair<string, Server> server)
|
||||||
|
{
|
||||||
|
ChangeStatus(server, ServerStatues.Connecting);
|
||||||
|
var offset = GetNtpOffset(server.Key);
|
||||||
|
Interlocked.Increment(ref _procedCnt);
|
||||||
|
|
||||||
|
if (offset == TimeSpan.Zero) {
|
||||||
|
ChangeStatus(server, ServerStatues.Failed);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
Interlocked.Increment(ref _successCnt);
|
||||||
|
ChangeStatus(server, ServerStatues.Succeed, offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ValueTask.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -147,48 +195,42 @@ public sealed class Main : Tool<Option>
|
|||||||
SetLocalTime(timeToSet);
|
SetLocalTime(timeToSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
|
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
|
||||||
public override async Task Run()
|
public override async Task Run()
|
||||||
{
|
{
|
||||||
var tPrinting = Task.Run(Printing);
|
PrintTemplate();
|
||||||
|
var tLoading = DrawLoading();
|
||||||
|
|
||||||
await Parallel.ForEachAsync(_serverDictionary
|
|
||||||
|
await Parallel.ForEachAsync(_srvStatus
|
||||||
, new ParallelOptions { MaxDegreeOfParallelism = _MAX_DEGREE_OF_PARALLELISM }
|
, new ParallelOptions { MaxDegreeOfParallelism = _MAX_DEGREE_OF_PARALLELISM }
|
||||||
, (server, _) => {
|
, (server, _) => ServerHandle(server));
|
||||||
server.Value.Status = ServerStatues.Connecting;
|
|
||||||
var offset = GetNtpOffset(server.Key);
|
|
||||||
|
|
||||||
Interlocked.Increment(ref _procedCnt);
|
await tLoading;
|
||||||
|
|
||||||
if (offset == TimeSpan.Zero) {
|
|
||||||
server.Value.Status = ServerStatues.Failed;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
server.Value.Status = ServerStatues.Succeed;
|
|
||||||
Interlocked.Increment(ref _successCnt);
|
|
||||||
server.Value.Offset = offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ValueTask.CompletedTask;
|
var avgOffset = TimeSpan.FromTicks((long)_srvStatus //
|
||||||
});
|
|
||||||
|
|
||||||
tPrinting.Wait();
|
|
||||||
var avgOffset = TimeSpan.FromTicks((long)_serverDictionary //
|
|
||||||
.Where(x => x.Value.Status == ServerStatues.Succeed)
|
.Where(x => x.Value.Status == ServerStatues.Succeed)
|
||||||
.Average(x => x.Value.Offset.Ticks));
|
.Average(x => x.Value.Offset.Ticks));
|
||||||
|
|
||||||
|
Console.SetCursorPosition(0, _serverCnt + 1);
|
||||||
Console.WriteLine(Str.NtpReceiveDone, _successCnt, _serverCnt, avgOffset.TotalMilliseconds);
|
Console.WriteLine(Str.NtpReceiveDone, _successCnt, _serverCnt, avgOffset.TotalMilliseconds);
|
||||||
if (!Opt.Sync) return;
|
|
||||||
Console.WriteLine();
|
if (!Opt.Sync) {
|
||||||
|
var _ = Task.Run(async () => {
|
||||||
|
while (true) {
|
||||||
|
await Task.Delay(100);
|
||||||
|
Console.SetCursorPosition(0, Console.GetCursorPosition().Top);
|
||||||
|
Console.Write(Str.ServerTime, (DateTime.Now - avgOffset).ToString("O"));
|
||||||
|
Console.Write(@", {0}", Str.PressAnyKey);
|
||||||
|
}
|
||||||
|
// ReSharper disable once FunctionNeverReturns
|
||||||
|
});
|
||||||
|
Console.ReadKey();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
SetSysteTime(DateTime.Now - avgOffset);
|
SetSysteTime(DateTime.Now - avgOffset);
|
||||||
Console.WriteLine(Str.LocalTimeSyncDone);
|
Console.WriteLine(Str.LocalTimeSyncDone);
|
||||||
}
|
}
|
||||||
|
|
||||||
private record Server
|
|
||||||
{
|
|
||||||
public TimeSpan Offset;
|
|
||||||
public ServerStatues Status;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -6,6 +6,6 @@ public class Option : IOption
|
|||||||
[Option('s', "sync", HelpText = nameof(Str.SyncToLocalTime), Default = false, ResourceType = typeof(Str))]
|
[Option('s', "sync", HelpText = nameof(Str.SyncToLocalTime), Default = false, ResourceType = typeof(Str))]
|
||||||
public bool Sync { get; set; }
|
public bool Sync { get; set; }
|
||||||
|
|
||||||
[Option('t', "timeout", HelpText = nameof(Str.TimeoutMillSecs), Default = 3000, ResourceType = typeof(Str))]
|
[Option('t', "timeout", HelpText = nameof(Str.TimeoutMillSecs), Default = 2000, ResourceType = typeof(Str))]
|
||||||
public int Timeout { get; set; }
|
public int Timeout { get; set; }
|
||||||
}
|
}
|
@ -24,10 +24,10 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CommandLineParser" Version="2.9.1"/>
|
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
||||||
<PackageReference Include="NSExt" Version="1.0.6"/>
|
<PackageReference Include="NSExt" Version="1.0.6" />
|
||||||
<PackageReference Include="ShellProgressBar" Version="5.2.0"/>
|
<PackageReference Include="ShellProgressBar" Version="5.2.0" />
|
||||||
<PackageReference Include="TextCopy" Version="6.2.0"/>
|
<PackageReference Include="TextCopy" Version="6.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user