Skip to content

Commit

Permalink
ReceiveCommand增加命令来源
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed May 18, 2024
1 parent 6b47143 commit bf5b945
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 54 deletions.
10 changes: 7 additions & 3 deletions NewLife.Remoting/Clients/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public virtual LoginRequest BuildLoginRequest()
{
foreach (var model in rs.Commands)
{
await ReceiveCommand(model);
await ReceiveCommand(model, "Pong");
}
}
}
Expand Down Expand Up @@ -385,7 +385,11 @@ protected virtual void StopTimer()
/// <returns></returns>
protected virtual Task OnPing(Object state) => Ping();

async Task ReceiveCommand(CommandModel model)
/// <summary>收到命令</summary>
/// <param name="model"></param>
/// <param name="source"></param>
/// <returns></returns>
protected async Task ReceiveCommand(CommandModel model, String source)
{
if (model == null) return;

Expand All @@ -399,7 +403,7 @@ async Task ReceiveCommand(CommandModel model)
{
//todo 有效期判断可能有隐患,现在只是假设服务器和客户端在同一个时区,如果不同,可能会出现问题
var now = GetNow();
XTrace.WriteLine("Got Command: {0}", model.ToJson());
XTrace.WriteLine("[{0}] Got Command: {1}", source, model.ToJson());
if (model.Expire.Year < 2000 || model.Expire > now)
{
// 延迟执行
Expand Down
52 changes: 1 addition & 51 deletions NewLife.Remoting/Clients/HttpClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using NewLife.Caching;
using NewLife.Log;
using NewLife.Remoting.Models;
using NewLife.Threading;
using NewLife.Serialization;

#if NETCOREAPP
Expand Down Expand Up @@ -259,56 +258,7 @@ protected virtual async Task OnReceive(String message)
else
{
var model = message.ToJsonEntity<CommandModel>();
if (model != null) await ReceiveCommand(model);
}
}

async Task ReceiveCommand(CommandModel model)
{
if (model == null) return;

// 去重,避免命令被重复执行
if (!_cache.Add($"cmd:{model.Id}", model, 3600)) return;

// 建立追踪链路
using var span = Tracer?.NewSpan("cmd:" + model.Command, model);
if (model.TraceId != null) span?.Detach(model.TraceId);
try
{
//todo 有效期判断可能有隐患,现在只是假设服务器和客户端在同一个时区,如果不同,可能会出现问题
//WriteLog("Got Service: {0}", model.ToJson());
var now = GetNow();
if (model.Expire.Year < 2000 || model.Expire > now)
{
// 延迟执行
var ts = model.StartTime - now;
if (ts.TotalMilliseconds > 0)
{
TimerX.Delay(s =>
{
_ = OnReceiveCommand(model);
}, (Int32)ts.TotalMilliseconds);

var reply = new CommandReplyModel
{
Id = model.Id,
Status = CommandStatus.处理中,
Data = $"已安排计划执行 {model.StartTime.ToFullString()}"
};
await CommandReply(reply);
}
else
await OnReceiveCommand(model);
}
else
{
var rs = new CommandReplyModel { Id = model.Id, Status = CommandStatus.取消 };
await CommandReply(rs);
}
}
catch (Exception ex)
{
span?.SetError(ex, null);
if (model != null) await ReceiveCommand(model, "WebSocket");
}
}
#endregion
Expand Down

0 comments on commit bf5b945

Please sign in to comment.