Files
KinginfoGateway/src/Foundation/ThingsGateway.Foundation/Channel/TcpSessionClientChannel.cs
2248356998 qq.com e785f6660c 10.10.5
2025-08-01 21:53:47 +08:00

153 lines
5.0 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.

//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://thingsgateway.cn/
// QQ群605534569
//------------------------------------------------------------------------------
using System.Collections.Concurrent;
using ThingsGateway.NewLife;
namespace ThingsGateway.Foundation;
/// <summary>
/// Tcp终端通道
/// </summary>
public class TcpSessionClientChannel : TcpSessionClient, IClientChannel
{
/// <inheritdoc/>
public TcpSessionClientChannel()
{
}
public void ResetSign(int minSign = 0, int maxSign = ushort.MaxValue)
{
var pool = WaitHandlePool;
WaitHandlePool = new WaitHandlePool<MessageBase>(minSign, maxSign);
pool?.CancelAll();
pool?.SafeDispose();
}
/// <inheritdoc/>
public ChannelReceivedEventHandler ChannelReceived { get; } = new();
/// <inheritdoc/>
public IChannelOptions ChannelOptions { get; internal set; }
/// <inheritdoc/>
public ChannelTypeEnum ChannelType => ChannelOptions.ChannelType;
/// <inheritdoc/>
public ConcurrentList<IDevice> Collects { get; } = new();
/// <inheritdoc/>
public DataHandlingAdapter ReadOnlyDataHandlingAdapter => DataHandlingAdapter;
/// <inheritdoc/>
public ChannelEventHandler Started { get; } = new();
/// <inheritdoc/>
public ChannelEventHandler Starting { get; } = new();
/// <inheritdoc/>
public ChannelEventHandler Stoped { get; } = new();
/// <inheritdoc/>
public ChannelEventHandler Stoping { get; } = new();
/// <summary>
/// 等待池
/// </summary>
public WaitHandlePool<MessageBase> WaitHandlePool { get; private set; } = new();
/// <inheritdoc/>
public WaitLock WaitLock { get; internal set; } = new(nameof(TcpSessionClientChannel));
public virtual WaitLock GetLock(string key) => WaitLock;
/// <inheritdoc/>
public override Task<Result> CloseAsync(string msg, CancellationToken token)
{
WaitHandlePool.SafeDispose();
return base.CloseAsync(msg, token);
}
/// <inheritdoc/>
public Task ConnectAsync(int timeout, CancellationToken token) => Task.CompletedTask;
/// <inheritdoc/>
public void SetDataHandlingAdapter(DataHandlingAdapter adapter)
{
if (adapter is SingleStreamDataHandlingAdapter singleStreamDataHandlingAdapter)
SetAdapter(singleStreamDataHandlingAdapter);
}
/// <inheritdoc/>
public Task SetupAsync(TouchSocketConfig config) => Task.CompletedTask;
/// <inheritdoc/>
public ConcurrentDictionary<long, Func<IClientChannel, ReceivedDataEventArgs, bool, Task>> ChannelReceivedWaitDict { get; } = new();
/// <inheritdoc/>
public override string ToString()
{
return $"{IP}:{Port}:{Id}";
}
/// <inheritdoc/>
protected override void SafetyDispose(bool disposing)
{
WaitHandlePool.SafeDispose();
base.SafetyDispose(disposing);
}
/// <inheritdoc/>
protected override async Task OnTcpClosed(ClosedEventArgs e)
{
//Logger?.Debug($"{ToString()} Closed{(e.Message.IsNullOrEmpty() ? string.Empty : $"-{e.Message}")}");
await this.OnChannelEvent(Stoped).ConfigureAwait(false);
await base.OnTcpClosed(e).ConfigureAwait(false);
}
/// <inheritdoc/>
protected override async Task OnTcpClosing(ClosingEventArgs e)
{
//Logger?.Debug($"{ToString()} Closing{(e.Message.IsNullOrEmpty() ? string.Empty : $"-{e.Message}")}");
await this.OnChannelEvent(Stoping).ConfigureAwait(false);
await base.OnTcpClosing(e).ConfigureAwait(false);
}
/// <inheritdoc/>
protected override async Task OnTcpConnected(ConnectedEventArgs e)
{
await this.OnChannelEvent(Started).ConfigureAwait(false);
await base.OnTcpConnected(e).ConfigureAwait(false);
}
/// <inheritdoc/>
protected override async Task OnTcpConnecting(ConnectingEventArgs e)
{
await this.OnChannelEvent(Starting).ConfigureAwait(false);
await base.OnTcpConnecting(e).ConfigureAwait(false);
}
/// <inheritdoc/>
protected override async Task OnTcpReceived(ReceivedDataEventArgs e)
{
await base.OnTcpReceived(e).ConfigureAwait(false);
if (e.RequestInfo is MessageBase response)
{
if (ChannelReceivedWaitDict.TryRemove(response.Sign, out var func))
{
await func.Invoke(this, e, ChannelReceived.Count == 1).ConfigureAwait(false);
e.Handled = true;
}
}
if (e.Handled)
return;
await this.OnChannelReceivedEvent(e, ChannelReceived).ConfigureAwait(false);
}
}