292 lines
9.4 KiB
C#
292 lines
9.4 KiB
C#
#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
|
||
{
|
||
/// <summary>
|
||
/// TcpDmtpSocketClient
|
||
/// </summary>
|
||
public partial class TcpDmtpSocketClient : SocketClient, ITcpDmtpSocketClient
|
||
{
|
||
private DmtpActor m_smtpActor;
|
||
|
||
/// <summary>
|
||
/// TcpDmtpSocketClient
|
||
/// </summary>
|
||
public TcpDmtpSocketClient()
|
||
{
|
||
this.Protocol = DmtpUtility.DmtpProtocol;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public IDmtpActor DmtpActor { get => this.m_smtpActor; }
|
||
|
||
/// <inheritdoc cref="IDmtpActor.IsHandshaked"/>
|
||
public bool IsHandshaked => this.DmtpActor != null && this.DmtpActor.IsHandshaked;
|
||
|
||
/// <summary>
|
||
/// 验证超时时间,默认为3000ms
|
||
/// </summary>
|
||
public int VerifyTimeout => this.Config.GetValue(DmtpConfigExtension.VerifyTimeoutProperty);
|
||
|
||
/// <summary>
|
||
/// 连接令箭
|
||
/// </summary>
|
||
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 事件
|
||
|
||
/// <summary>
|
||
/// 当创建通道
|
||
/// </summary>
|
||
/// <param name="e"></param>
|
||
protected virtual void OnCreateChannel(CreateChannelEventArgs e)
|
||
{
|
||
}
|
||
|
||
/// <summary>
|
||
/// 在完成握手连接时
|
||
/// </summary>
|
||
/// <param name="e"></param>
|
||
protected virtual void OnHandshaked(DmtpVerifyEventArgs e)
|
||
{
|
||
}
|
||
|
||
/// <summary>
|
||
/// 在验证Token时
|
||
/// </summary>
|
||
/// <param name="e">参数</param>
|
||
protected virtual void OnHandshaking(DmtpVerifyEventArgs e)
|
||
{
|
||
}
|
||
|
||
/// <summary>
|
||
/// 在需要转发路由包时。
|
||
/// </summary>
|
||
/// <param name="e"></param>
|
||
protected virtual void OnRouting(PackageRouterEventArgs e)
|
||
{
|
||
}
|
||
|
||
#endregion 事件
|
||
|
||
/// <inheritdoc/>
|
||
public override void Close(string msg = "")
|
||
{
|
||
this.m_smtpActor.Close(true, msg);
|
||
}
|
||
|
||
#region ResetId
|
||
|
||
///<inheritdoc cref="IDmtpActor.ResetId(string)"/>
|
||
public override void ResetId(string id)
|
||
{
|
||
this.m_smtpActor.ResetId(id);
|
||
}
|
||
|
||
///<inheritdoc cref="IDmtpActor.ResetIdAsync(string)"/>
|
||
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;
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
protected override void Dispose(bool disposing)
|
||
{
|
||
this.DmtpActor.SafeDispose();
|
||
base.Dispose(disposing);
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
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);
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
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验证超时");
|
||
}
|
||
});
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
protected override async Task OnDisconnected(DisconnectEventArgs e)
|
||
{
|
||
this.DmtpActor.Close(false, e.Message);
|
||
await base.OnDisconnected(e);
|
||
}
|
||
|
||
private void ThisDmtpActorOutputSend(DmtpActor actor, ArraySegment<byte>[] transferBytes)
|
||
{
|
||
base.Send(transferBytes);
|
||
}
|
||
|
||
private void ThisOnResetId(DmtpActor rpcActor, WaitSetId waitSetId)
|
||
{
|
||
this.DirectResetId(waitSetId.NewId);
|
||
}
|
||
|
||
#region 发送
|
||
|
||
/// <summary>
|
||
/// 不允许直接发送
|
||
/// </summary>
|
||
/// <param name="buffer"></param>
|
||
/// <param name="offset"></param>
|
||
/// <param name="length"></param>
|
||
public override void Send(byte[] buffer, int offset, int length)
|
||
{
|
||
throw new Exception("不允许直接发送,请指定任意大于0的协议,然后发送。");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 不允许直接发送
|
||
/// </summary>
|
||
/// <param name="transferBytes"></param>
|
||
public override void Send(IList<ArraySegment<byte>> transferBytes)
|
||
{
|
||
throw new Exception("不允许直接发送,请指定任意大于0的协议,然后发送。");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 不允许直接发送
|
||
/// </summary>
|
||
/// <param name="buffer"></param>
|
||
/// <param name="offset"></param>
|
||
/// <param name="length"></param>
|
||
public override Task SendAsync(byte[] buffer, int offset, int length)
|
||
{
|
||
throw new Exception("不允许直接发送,请指定任意大于0的协议,然后发送。");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 不允许直接发送
|
||
/// </summary>
|
||
/// <param name="transferBytes"></param>
|
||
public override Task SendAsync(IList<ArraySegment<byte>> transferBytes)
|
||
{
|
||
throw new Exception("不允许直接发送,请指定任意大于0的协议,然后发送。");
|
||
}
|
||
|
||
#endregion 发送
|
||
}
|
||
} |