Files
KinginfoGateway/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Socket/Plugins/ReconnectionPlugin.cs
2023-10-17 23:06:21 +08:00

220 lines
7.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://diego2098.gitee.io/thingsgateway-docs/
// QQ群605534569
//------------------------------------------------------------------------------
#endregion
//------------------------------------------------------------------------------
// 此代码版权除特别声明或在XREF结尾的命名空间的代码归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议若本仓库没有设置则按MIT开源协议授权
// CSDN博客https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频https://space.bilibili.com/94253567
// Gitee源代码仓库https://gitee.com/RRQM_Home
// Github源代码仓库https://github.com/RRQM
// API首页http://rrqm_home.gitee.io/touchsocket/
// 交流QQ群234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
namespace ThingsGateway.Foundation.Sockets
{
/// <summary>
/// 重连插件
/// </summary>
[PluginOption(Singleton = true, NotRegister = true)]
public sealed class ReconnectionPlugin<TClient> : PluginBase where TClient : class, ITcpClient
{
/// <summary>
/// 重连插件
/// </summary>
public ReconnectionPlugin()
{
this.ActionForConnect = async (c) =>
{
try
{
await c.ConnectAsync();
return true;
}
catch
{
return false;
}
};
}
/// <inheritdoc/>
protected override void Loaded(IPluginsManager pluginsManager)
{
base.Loaded(pluginsManager);
pluginsManager.Add<object, ConfigEventArgs>(nameof(ILoadedConfigPlugin.OnLoadedConfig), this.OnLoadedConfig);
pluginsManager.Add<TClient, DisconnectEventArgs>(nameof(ITcpDisconnectedPlugin.OnTcpDisconnected), this.OnTcpDisconnected);
}
private bool m_polling;
/// <summary>
/// 每个周期可执行的委托。用于检验客户端活性。返回true表示存活返回
/// </summary>
public Func<TClient, int, Task<bool?>> ActionForCheck { get; set; }
/// <summary>
/// ActionForConnect
/// </summary>
public Func<TClient, Task<bool>> ActionForConnect { get; set; }
/// <summary>
/// 检验时间间隔
/// </summary>
public TimeSpan Tick { get; set; } = TimeSpan.FromSeconds(1);
private Task OnLoadedConfig(object sender, ConfigEventArgs e)
{
Task.Run(async () =>
{
if (!this.m_polling)
{
return;
}
if (sender is TClient client)
{
var failCount = 0;
while (true)
{
if (this.DisposedValue)
{
return;
}
await Task.Delay(this.Tick);
try
{
var b = await this.ActionForCheck.Invoke(client, failCount);
if (b == null)
{
continue;
}
else if (b == false)
{
if (await this.ActionForConnect.Invoke(client))
{
failCount = 0;
}
}
else
{
failCount = 0;
}
}
catch
{
}
}
}
});
return e.InvokeNext();
}
private Task OnTcpDisconnected(TClient client, DisconnectEventArgs e)
{
Task.Run(async () =>
{
if (e.Manual)
{
return;
}
while (true)
{
if (await this.ActionForConnect.Invoke(client))
{
return;
}
}
});
return e.InvokeNext();
}
/// <summary>
/// 每个周期可执行的委托。返回值为True标识客户端存活。返回False表示失活立即重连。返回null时表示跳过此次检验。
/// </summary>
/// <param name="actionForCheck"></param>
/// <returns></returns>
public ReconnectionPlugin<TClient> SetActionForCheck(Func<TClient, int, Task<bool?>> actionForCheck)
{
this.ActionForCheck = actionForCheck;
return this;
}
/// <summary>
/// 每个周期可执行的委托。返回值为True标识客户端存活。返回False表示失活立即重连。返回null时表示跳过此次检验。
/// </summary>
/// <param name="actionForCheck"></param>
/// <returns></returns>
public ReconnectionPlugin<TClient> SetActionForCheck(Func<TClient, int, bool?> actionForCheck)
{
this.ActionForCheck = async (c, i) =>
{
await EasyTask.CompletedTask;
return actionForCheck.Invoke(c, i);
};
return this;
}
/// <summary>
/// 设置连接动作
/// </summary>
/// <param name="tryConnect"></param>
/// <returns>无论如何只要返回True则结束本轮尝试</returns>
public ReconnectionPlugin<TClient> SetConnectAction(Func<TClient, Task<bool>> tryConnect)
{
this.ActionForConnect = tryConnect;
return this;
}
/// <summary>
/// 设置连接动作
/// </summary>
/// <param name="tryConnect"></param>
/// <returns>无论如何只要返回True则结束本轮尝试</returns>
public ReconnectionPlugin<TClient> SetConnectAction(Func<TClient, bool> tryConnect)
{
this.ActionForConnect = async (c) =>
{
await EasyTask.CompletedTask;
return tryConnect.Invoke(c);
};
return this;
}
/// <summary>
/// 检验时间间隔
/// </summary>
/// <param name="tick"></param>
/// <returns></returns>
public ReconnectionPlugin<TClient> SetTick(TimeSpan tick)
{
this.Tick = tick;
return this;
}
/// <summary>
/// 使用轮询保持活性。
/// </summary>
public ReconnectionPlugin<TClient> UsePolling()
{
this.ActionForCheck = (client, failCount) =>
{
return Task.FromResult(client?.Online);
};
this.m_polling = true;
return this;
}
}
}