#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.Dmtp { /// /// TcpDmtpSocketClient /// public partial class TcpDmtpSocketClient : SocketClient, ITcpDmtpSocketClient { private DmtpActor m_smtpActor; /// /// TcpDmtpSocketClient /// public TcpDmtpSocketClient() { this.Protocol = DmtpUtility.DmtpProtocol; } /// public IDmtpActor DmtpActor { get => this.m_smtpActor; } /// public bool IsHandshaked => this.DmtpActor != null && this.DmtpActor.IsHandshaked; /// /// 验证超时时间,默认为3000ms /// public int VerifyTimeout => this.Config.GetValue(DmtpConfigExtension.VerifyTimeoutProperty); /// /// 连接令箭 /// public string VerifyToken => this.Config.GetValue(DmtpConfigExtension.VerifyTokenProperty); #region 内部委托绑定 private void OnDmtpActorClose(DmtpActor actor, string msg) { base.Close(msg); } private void OnDmtpActorCreateChannel(DmtpActor actor, CreateChannelEventArgs e) { this.OnCreateChannel(e); if (e.Handled) { return; } this.PluginsManager.Raise(nameof(IDmtpCreateChannelPlugin.OnCreateChannel), this, e); } private void OnDmtpActorHandshaked(DmtpActor actor, DmtpVerifyEventArgs e) { this.OnHandshaked(e); if (e.Handled) { return; } if (this.PluginsManager.Enable && this.PluginsManager.Raise(nameof(IDmtpHandshakedPlugin.OnDmtpHandshaked), this, e)) { return; } } private void OnDmtpActorHandshaking(DmtpActor actor, DmtpVerifyEventArgs e) { if (e.Token == this.VerifyToken) { e.IsPermitOperation = true; } else { e.Message = "Token不受理"; } this.OnHandshaking(e); if (e.Handled) { return; } this.PluginsManager.Raise(nameof(IDmtpHandshakingPlugin.OnDmtpHandshaking), this, e); } private void OnDmtpActorRouting(DmtpActor actor, PackageRouterEventArgs e) { this.OnRouting(e); if (e.Handled) { return; } if (this.PluginsManager.Enable && this.PluginsManager.Raise(nameof(IDmtpRoutingPlugin.OnDmtpRouting), this, e)) { return; } } #endregion 内部委托绑定 #region 事件 /// /// 当创建通道 /// /// protected virtual void OnCreateChannel(CreateChannelEventArgs e) { } /// /// 在完成握手连接时 /// /// protected virtual void OnHandshaked(DmtpVerifyEventArgs e) { } /// /// 在验证Token时 /// /// 参数 protected virtual void OnHandshaking(DmtpVerifyEventArgs e) { } /// /// 在需要转发路由包时。 /// /// protected virtual void OnRouting(PackageRouterEventArgs e) { } #endregion 事件 /// public override void Close(string msg = "") { this.m_smtpActor.Close(true, msg); } #region ResetId /// public override void ResetId(string id) { this.m_smtpActor.ResetId(id); } /// public Task ResetIdAsync(string newId) { return this.m_smtpActor.ResetIdAsync(newId); } #endregion ResetId internal void SetDmtpActor(DmtpActor actor) { actor.OnResetId = this.ThisOnResetId; actor.OutputSend = this.ThisDmtpActorOutputSend; actor.Client = this; actor.OnClose = this.OnDmtpActorClose; actor.OnRouting = this.OnDmtpActorRouting; actor.OnHandshaked = this.OnDmtpActorHandshaked; actor.OnHandshaking = this.OnDmtpActorHandshaking; actor.OnCreateChannel = this.OnDmtpActorCreateChannel; actor.Logger = this.Logger; this.m_smtpActor = actor; } /// protected override void Dispose(bool disposing) { this.DmtpActor.SafeDispose(); base.Dispose(disposing); } /// protected override async Task ReceivedData(ReceivedDataEventArgs e) { var message = (DmtpMessage)e.RequestInfo; if (!this.m_smtpActor.InputReceivedData(message)) { await this.PluginsManager.RaiseAsync(nameof(IDmtpReceivedPlugin.OnDmtpReceived), this, new DmtpMessageEventArgs(message)); } await base.ReceivedData(e); } /// protected override async Task OnConnected(ConnectedEventArgs e) { this.m_smtpActor.Id = this.Id; await base.OnConnected(e); _ = Task.Run(async () => { await Task.Delay(this.VerifyTimeout); if (!this.IsHandshaked) { this.TryShutdown(); base.Close("Handshak验证超时"); } }); } /// protected override async Task OnDisconnected(DisconnectEventArgs e) { this.DmtpActor.Close(false, e.Message); await base.OnDisconnected(e); } private void ThisDmtpActorOutputSend(DmtpActor actor, ArraySegment[] transferBytes) { base.Send(transferBytes); } private void ThisOnResetId(DmtpActor rpcActor, WaitSetId waitSetId) { this.DirectResetId(waitSetId.NewId); } #region 发送 /// /// 不允许直接发送 /// /// /// /// public override void Send(byte[] buffer, int offset, int length) { throw new Exception("不允许直接发送,请指定任意大于0的协议,然后发送。"); } /// /// 不允许直接发送 /// /// public override void Send(IList> transferBytes) { throw new Exception("不允许直接发送,请指定任意大于0的协议,然后发送。"); } /// /// 不允许直接发送 /// /// /// /// public override Task SendAsync(byte[] buffer, int offset, int length) { throw new Exception("不允许直接发送,请指定任意大于0的协议,然后发送。"); } /// /// 不允许直接发送 /// /// public override Task SendAsync(IList> transferBytes) { throw new Exception("不允许直接发送,请指定任意大于0的协议,然后发送。"); } #endregion 发送 } }