Files
ThingsGateway/src/Foundation/ThingsGateway.Foundation/Channel/DDP/DDPMessage.cs
2025-08-21 11:46:58 +00:00

76 lines
2.6 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.

//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人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();
}
}