//------------------------------------------------------------------------------ // 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充 // 此代码版权(除特别声明外的代码)归作者本人Diego所有 // 源代码使用协议遵循本仓库的开源协议及附加协议 // Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway // Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway // 使用文档:https://thingsgateway.cn/ // QQ群:605534569 //------------------------------------------------------------------------------ namespace ThingsGateway.Foundation; /// /// Tcp终端通道 /// public class TcpSessionClientChannel : TcpSessionClient, IClientChannel { /// public TcpSessionClientChannel() { WaitHandlePool.MaxSign = ushort.MaxValue; } public int MaxSign { get => WaitHandlePool.MaxSign; set => WaitHandlePool.MaxSign = value; } /// public ChannelReceivedEventHandler ChannelReceived { get; set; } = new(); /// public ChannelTypeEnum ChannelType => ChannelTypeEnum.TcpService; /// public ConcurrentList Collects { get; } = new(); /// public DataHandlingAdapter ReadOnlyDataHandlingAdapter => DataHandlingAdapter; /// public ChannelEventHandler Started { get; set; } = new(); /// public ChannelEventHandler Starting { get; set; } = new(); /// public ChannelEventHandler Stoped { get; set; } = new(); /// public ChannelEventHandler Stoping { get; set; } = new(); /// /// 等待池 /// public WaitHandlePool WaitHandlePool { get; private set; } = new(); /// public WaitLock WaitLock { get; } = new WaitLock(); /// public void Close(string msg) { CloseAsync(msg).ConfigureAwait(false); } /// public override Task CloseAsync(string msg) { WaitHandlePool.SafeDispose(); return base.CloseAsync(msg); } /// public void Connect(int millisecondsTimeout = 3000, CancellationToken token = default) => throw new NotSupportedException(); /// public Task ConnectAsync(int timeout, CancellationToken token) => throw new NotImplementedException(); /// public void SetDataHandlingAdapter(DataHandlingAdapter adapter) { if (adapter is SingleStreamDataHandlingAdapter singleStreamDataHandlingAdapter) SetAdapter(singleStreamDataHandlingAdapter); } /// public Task SetupAsync(TouchSocketConfig config) { return EasyTask.CompletedTask; } /// public override string ToString() { return $"{IP}:{Port}:{Id}"; } /// protected override void Dispose(bool disposing) { if (DisposedValue) return; WaitHandlePool.SafeDispose(); base.Dispose(disposing); } /// 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); } /// 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); } /// protected override async Task OnTcpConnected(ConnectedEventArgs e) { //Logger?.Debug($"{ToString()}{FoundationConst.Connected}"); await this.OnChannelEvent(Started).ConfigureAwait(false); await base.OnTcpConnected(e).ConfigureAwait(false); } /// protected override async Task OnTcpConnecting(ConnectingEventArgs e) { await this.OnChannelEvent(Starting).ConfigureAwait(false); await base.OnTcpConnecting(e).ConfigureAwait(false); } /// protected override async Task OnTcpReceived(ReceivedDataEventArgs e) { await base.OnTcpReceived(e).ConfigureAwait(false); await this.OnChannelReceivedEvent(e, ChannelReceived).ConfigureAwait(false); } }