mirror of
https://gitee.com/ThingsGateway/ThingsGateway.git
synced 2025-10-29 06:33:58 +08:00
76 lines
2.6 KiB
C#
76 lines
2.6 KiB
C#
//------------------------------------------------------------------------------
|
||
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
|
||
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
|
||
// 源代码使用协议遵循本仓库的开源协议及附加协议
|
||
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
|
||
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
|
||
// 使用文档:https://thingsgateway.cn/
|
||
// QQ群:605534569
|
||
//------------------------------------------------------------------------------
|
||
|
||
using System.Buffers;
|
||
|
||
namespace ThingsGateway.Foundation;
|
||
|
||
/// <summary>
|
||
/// <inheritdoc/>
|
||
/// </summary>
|
||
public abstract class DDPMessage : MessageBase, IResultMessage
|
||
{
|
||
/// <inheritdoc/>
|
||
public override long HeaderLength => 4;
|
||
public byte Type = 0;
|
||
public string Id;
|
||
public override FilterResult CheckBody<TByteBlock>(ref TByteBlock byteBlock)
|
||
{
|
||
Id = byteBlock.ToString(byteBlock.BytesRead, 11).Replace("\0", "");
|
||
OperCode = 0;
|
||
|
||
Content = GetContent(ref byteBlock);
|
||
return FilterResult.Success;
|
||
}
|
||
|
||
public override bool CheckHead<TByteBlock>(ref TByteBlock byteBlock)
|
||
{
|
||
var code = ReaderExtension.ReadValue<TByteBlock, byte>(ref byteBlock);
|
||
Type = ReaderExtension.ReadValue<TByteBlock, byte>(ref byteBlock);
|
||
|
||
if (code != 0x7B)
|
||
{
|
||
return false;
|
||
}
|
||
else
|
||
{
|
||
BodyLength = GetBodyLength(ref byteBlock);
|
||
return true;
|
||
}
|
||
}
|
||
|
||
public abstract long GetBodyLength<TByteBlock>(ref TByteBlock byteBlock) where TByteBlock : IBytesReader;
|
||
public abstract byte[] GetContent<TByteBlock>(ref TByteBlock byteBlock) where TByteBlock : IBytesReader;
|
||
}
|
||
|
||
public class DDPTcpMessage : DDPMessage
|
||
{
|
||
public override long GetBodyLength<TByteBlock>(ref TByteBlock byteBlock)
|
||
{
|
||
return ReaderExtension.ReadValue<TByteBlock, ushort>(ref byteBlock, EndianType.Big) - 4;
|
||
}
|
||
|
||
public override byte[] GetContent<TByteBlock>(ref TByteBlock byteBlock)
|
||
{
|
||
return byteBlock.TotalSequence.Slice(byteBlock.BytesRead + 11, BodyLength - 12).ToArray();
|
||
}
|
||
}
|
||
|
||
public class DDPUdpMessage : DDPMessage
|
||
{
|
||
public override long GetBodyLength<TByteBlock>(ref TByteBlock byteBlock)
|
||
{
|
||
return (byteBlock.BytesRead + byteBlock.BytesRemaining - 4);
|
||
}
|
||
public override byte[] GetContent<TByteBlock>(ref TByteBlock byteBlock)
|
||
{
|
||
return byteBlock.TotalSequence.Slice(byteBlock.BytesRead + 12, BodyLength - 12).ToArray();
|
||
}
|
||
} |