Skip to content

Commit

Permalink
feat: 星尘代理本地服务StarService增加SetServer,在StarAgent没有设置服务端地址时,支持本地通过接口设置。
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Sep 2, 2023
1 parent 389f2c1 commit 3793848
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions StarAgent/StarService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using NewLife;
using NewLife.Agent;
using NewLife.Log;
using NewLife.Model;
using NewLife.Net;
using NewLife.Remoting;
using NewLife.Serialization;
Expand Down Expand Up @@ -71,27 +73,9 @@ protected override void Dispose(Boolean disposing)
[Api(nameof(Info))]
public AgentInfo Info(AgentInfo info)
{
//using var span = Manager?.Tracer?.NewSpan("ApiInfo");
XTrace.WriteLine("Info<={0}", info.ToJson());

var set = StarSetting;
//// 使用对方送过来的星尘服务端地址
//if (set.Server.IsNullOrEmpty() && !info.Server.IsNullOrEmpty())
//{
// set.Server = info.Server;
// set.Save();

// XTrace.WriteLine("StarAgent使用应用[{0}]送过来的星尘服务端地址:{1}", info.ProcessId, info.Server);

// if (Provider?.GetService<ServiceBase>() is MyService svc)
// {
// ThreadPool.QueueUserWorkItem(s =>
// {
// svc.StartFactory();
// svc.StartClient();
// });
// }
//}

var ai = _agentInfo ??= AgentInfo.GetLocal(true);
ai.Server = set.Server;
Expand All @@ -101,6 +85,34 @@ public AgentInfo Info(AgentInfo info)
return ai;
}

/// <summary>设置星尘服务端地址</summary>
/// <returns></returns>
[Api(nameof(SetServer))]
public String SetServer(String server)
{
var set = StarSetting;
if (set.Server.IsNullOrEmpty() && !server.IsNullOrEmpty())
{
set.Server = server;
set.Save();

XTrace.WriteLine("StarAgent使用[{0}]送过来的星尘服务端地址:{1}", Session, server);

if (Provider?.GetService<ServiceBase>() is MyService svc)
{
ThreadPool.QueueUserWorkItem(s =>
{
Thread.Sleep(1000);

svc.StartFactory();
svc.StartClient();
});
}
}

return set.Server;
}

private void DoRefreshLocal(Object state)
{
var ai = AgentInfo.GetLocal(true);
Expand Down

0 comments on commit 3793848

Please sign in to comment.