借助MMF实现StarServer快速查询
This commit is contained in:
parent
e587ae1a65
commit
4605c479bd
|
@ -1,20 +1,24 @@
|
|||
using System;
|
||||
using System.IO.MemoryMappedFiles;
|
||||
using System.Linq;
|
||||
using NewLife;
|
||||
using NewLife.Serialization;
|
||||
using NewLife.Threading;
|
||||
using Stardust;
|
||||
using Stardust.Models;
|
||||
|
||||
namespace StarAgent
|
||||
{
|
||||
internal class InfoService
|
||||
{
|
||||
/// <summary>本地应用服务管理</summary>
|
||||
public ServiceManager Manager { get; set; }
|
||||
|
||||
MemoryMappedFile _mmf;
|
||||
TimerX _timer2;
|
||||
public void Start()
|
||||
{
|
||||
var mapName = "MMF_Star";
|
||||
_mmf = MemoryMappedFile.CreateNew(mapName, 1024 * 1024);
|
||||
_mmf = MemoryMappedFile.CreateOrOpen("MMF_Star", 1024);
|
||||
|
||||
_timer2 = new TimerX(DoWork, null, 0, 5_000);
|
||||
}
|
||||
|
@ -27,11 +31,19 @@ namespace StarAgent
|
|||
|
||||
private void DoWork(Object state)
|
||||
{
|
||||
var set = StarSetting.Current;
|
||||
var inf = AgentInfo.GetLocal();
|
||||
var buf = Binary.FastWrite(inf).ReadBytes();
|
||||
inf.Server = set.Server;
|
||||
inf.Services = Manager?.Services.Select(e => e.Name).ToArray();
|
||||
|
||||
var view = _mmf.CreateViewAccessor();
|
||||
view.WriteArray(0, buf, 0, buf.Length);
|
||||
//var buf = Binary.FastWrite(inf).ReadBytes();
|
||||
|
||||
//var view = _mmf.CreateViewAccessor();
|
||||
//view.Write(0, buf.Length);
|
||||
//view.WriteArray(4, buf, 0, buf.Length);
|
||||
|
||||
var ms = _mmf.CreateViewStream(0, 1024, MemoryMappedFileAccess.Write);
|
||||
Binary.FastWrite(inf, ms);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -208,7 +208,7 @@ namespace StarAgent
|
|||
|
||||
_Manager.Start();
|
||||
|
||||
_infoService = new InfoService();
|
||||
_infoService = new InfoService { Manager = _Manager };
|
||||
_infoService.Start();
|
||||
|
||||
base.StartWork(reason);
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\StarAgent\InfoService.cs" Link="InfoService.cs" />
|
||||
<Compile Include="..\StarAgent\Program.cs" Link="Program.cs" />
|
||||
<Compile Include="..\StarAgent\Setting.cs" Link="Setting.cs" />
|
||||
<Compile Include="..\StarAgent\StarService.cs" Link="StarService.cs" />
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
|||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.IO.MemoryMappedFiles;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
|
@ -13,12 +14,9 @@ using NewLife.Http;
|
|||
using NewLife.Log;
|
||||
using NewLife.Messaging;
|
||||
using NewLife.Net;
|
||||
using NewLife.Reflection;
|
||||
using NewLife.Remoting;
|
||||
using NewLife.Serialization;
|
||||
using Stardust.Models;
|
||||
#if !NET40
|
||||
using TaskEx = System.Threading.Tasks.Task;
|
||||
#endif
|
||||
|
||||
namespace Stardust
|
||||
{
|
||||
|
@ -34,6 +32,7 @@ namespace Stardust
|
|||
|
||||
private readonly AgentInfo _local;
|
||||
private ApiClient _client;
|
||||
private MemoryMappedFile _mmf;
|
||||
#endregion
|
||||
|
||||
#region 构造
|
||||
|
@ -58,12 +57,21 @@ namespace Stardust
|
|||
|
||||
var set = StarSetting.Current;
|
||||
if (set.Debug) _client.EncoderLog = XTrace.Log;
|
||||
|
||||
var mapName = "MMF_Star";
|
||||
_mmf = MemoryMappedFile.CreateOrOpen(mapName, 1024 * 1024);
|
||||
}
|
||||
|
||||
/// <summary>获取信息</summary>
|
||||
/// <returns></returns>
|
||||
public AgentInfo GetInfo()
|
||||
{
|
||||
Init();
|
||||
|
||||
var ms = _mmf.CreateViewStream(0, 1024, MemoryMappedFileAccess.Read);
|
||||
var inf = Binary.FastRead<AgentInfo>(ms);
|
||||
if (inf != null && inf.ProcessId > 0) return inf;
|
||||
|
||||
var task = GetInfoAsync();
|
||||
if (task.Wait(500)) return task.Result;
|
||||
|
||||
|
@ -331,7 +339,7 @@ namespace Stardust
|
|||
/// <param name="target">目标目录</param>
|
||||
public static Task ProbeAsync(String url = null, String version = null, String target = null)
|
||||
{
|
||||
return TaskEx.Run(() =>
|
||||
return Task.Run(() =>
|
||||
{
|
||||
var client = new LocalStarClient();
|
||||
client.ProbeAndInstall(url, version, target);
|
||||
|
|
|
@ -104,7 +104,9 @@ namespace Test
|
|||
//}
|
||||
|
||||
var client = new LocalStarClient();
|
||||
client.ProbeAndInstall(null, "1.6");
|
||||
//client.ProbeAndInstall(null, "1.6");
|
||||
var inf = client.GetInfo();
|
||||
XTrace.WriteLine(inf.ToJson(true));
|
||||
|
||||
//var p = Process.GetCurrentProcess();
|
||||
//var name = p.MainModule.FileName;
|
||||
|
|
Loading…
Reference in New Issue