Files
KinginfoGateway/framework/Foundation/ThingsGateway.Foundation/TouchSocket/Dmtp/Features/Redis/RedisFeature.cs
2023-10-21 19:15:26 +08:00

122 lines
4.4 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.Dmtp.Redis
{
/// <summary>
/// RedisFeature
/// </summary>
public class RedisFeature : PluginBase, IDmtpHandshakingPlugin, IDmtpReceivedPlugin, IDmtpFeature
{
/// <summary>
/// RedisFeature
/// </summary>
public RedisFeature()
{
this.SetProtocolFlags(25);
}
/// <summary>
/// 定义元素的序列化和反序列化。
/// <para>注意Byte[]类型不用考虑。内部单独会做处理。</para>
/// </summary>
public BytesConverter Converter { get; set; } = new BytesConverter();
/// <summary>
/// 实际储存缓存。
/// </summary>
public ICache<string, byte[]> ICache { get; set; } = new MemoryCache<string, byte[]>();
/// <inheritdoc/>
public ushort ReserveProtocolSize => 5;
/// <inheritdoc/>
public ushort StartProtocol { get; set; }
/// <inheritdoc/>
public Task OnDmtpHandshaking(IDmtpActorObject client, DmtpVerifyEventArgs e)
{
var dmtpRedisActor = new DmtpRedisActor(client.DmtpActor)
{
ICache = this.ICache,
Converter = this.Converter
};
dmtpRedisActor.SetProtocolFlags(this.StartProtocol);
client.DmtpActor.SetStmpRedisActor(dmtpRedisActor);
return e.InvokeNext();
}
/// <inheritdoc/>
public async Task OnDmtpReceived(IDmtpActorObject client, DmtpMessageEventArgs e)
{
if (client.DmtpActor.GetDmtpRedisActor() is DmtpRedisActor redisClient)
{
if (await redisClient.InputReceivedData(e.DmtpMessage))
{
e.Handled = true;
return;
}
}
await e.InvokeNext();
}
/// <summary>
/// 设置实际储存缓存。默认使用<see cref="MemoryCache{TKey, TValue}"/>
/// </summary>
/// <param name="cache"></param>
public RedisFeature SetCache(ICache<string, byte[]> cache)
{
this.ICache = cache;
return this;
}
/// <summary>
/// 定义元素的序列化和反序列化。
/// <para>注意Byte[]类型不用考虑。内部单独会做处理。</para>
/// </summary>
/// <param name="converter"></param>
public RedisFeature SetConverter(BytesConverter converter)
{
this.Converter = converter;
return this;
}
/// <summary>
/// 设置<see cref="RedisFeature"/>的起始协议。
/// <para>
/// 默认起始为25保留5个协议长度。
/// </para>
/// </summary>
/// <param name="start"></param>
/// <returns></returns>
public RedisFeature SetProtocolFlags(ushort start)
{
this.StartProtocol = start;
return this;
}
}
}