Skip to content

Commit

Permalink
opti: 添加 WantEnable 字段以实现重试逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Mar 30, 2024
1 parent d96795f commit 6bc72a3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
15 changes: 12 additions & 3 deletions CSharp-OpenBMCLAPI/Modules/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Cluster : RunnableBase
public Guid guid;
private SocketIOClient.SocketIO socket;
public bool IsEnabled { get; set; }
public bool WantEnable { get; set; }
private Task? _keepAlive;
protected IStorage storage;
protected AccessCounter counter;
Expand Down Expand Up @@ -174,9 +175,7 @@ private void InitializeService()
application.MapGet("/measure/{size}", (context) => HttpServiceProvider.LogAndRun(context,
() => HttpServiceProvider.Measure(context).Wait()
));
application.MapPost("/api/{name}", (HttpContext context, string name) => HttpServiceProvider.LogAndRun(context,
() => HttpServiceProvider.Api(context, name, this).Wait()
));
application.MapPost("/api/{name}", (HttpContext context, string name) => HttpServiceProvider.Api(context, name, this).Wait());
application.MapGet("/", (context) => HttpServiceProvider.LogAndRun(context,
() =>
{
Expand Down Expand Up @@ -232,6 +231,11 @@ public void Connect()
{
SharedData.Logger.LogWarn($"与主控断开连接:{r}");
this.IsEnabled = false;
if (this.WantEnable)
{
this.Connect();
this.Enable().Wait();
}
});
}

Expand All @@ -249,6 +253,7 @@ public async Task Enable()
await socket.EmitAsync("enable", (SocketIOResponse resp) =>
{
this.IsEnabled = true;
this.WantEnable = true;
Utils.PrintResponseMessage(resp);
// Debugger.Break();
SharedData.Logger.LogSystem($"启用成功");
Expand Down Expand Up @@ -285,6 +290,10 @@ await socket.EmitAsync("disable", (SocketIOResponse resp) =>
SharedData.Logger.LogSystem($"禁用成功");
this.IsEnabled = false;
});
if (this.WantEnable)
{
await this.Enable();
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public int Connections
get
{
IPGlobalProperties properti = IPGlobalProperties.GetIPGlobalProperties();
var tcps = properti.GetActiveTcpConnections().ToList();
var tcps = properti.GetActiveTcpConnections();

var list = tcps.Where(f => f.LocalEndPoint.Port == SharedData.Config.PORT);

Expand Down
1 change: 1 addition & 0 deletions CSharp-OpenBMCLAPI/Modules/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public static void PrintJsonElement(JsonElement element)
/// <returns></returns>
public static async Task ExitCluster(Cluster cluster)
{
cluster.WantEnable = false;
cluster.cancellationSrc.Cancel();
await cluster.KeepAlive();
Thread.Sleep(1000);
Expand Down

0 comments on commit 6bc72a3

Please sign in to comment.