You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.2 KiB
C#

3 months ago
using NewLife;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
namespace NDSD_Screwdriver.Tool
{
public class MyFixedHeaderRequestInfo : IFixedHeaderRequestInfo
{
/// <summary>
/// 接口实现,标识数据长度
/// </summary>
public int BodyLength { get; private set; }
/// <summary>
/// 自定义属性,标识数据类型
/// </summary>
public byte DataType { get; set; }
/// <summary>
/// 自定义属性,标识指令类型
/// </summary>
public byte OrderType { get; set; }
/// <summary>
/// 自定义属性,标识实际数据
/// </summary>
public byte[] Body { get; set; }
public string GetBody()
{
// 方法1: 使用 Array.Copy
byte[] copiedBytes = new byte[BodyLength - 2];
Array.Copy(Body, 0, copiedBytes, 0, BodyLength - 2);
Array.Reverse(copiedBytes);
return copiedBytes.ToHex();
}
public bool OnParsingBody(ReadOnlySpan<byte> body)
{
if (body.Length == this.BodyLength)
{
this.Body = body.ToArray();
return true;
}
return false;
}
public bool OnParsingHeader(ReadOnlySpan<byte> header)
{
//在该示例中第一个字节表示后续的所有数据长度但是header设置的是3所以后续还应当接收length-2个长度。
this.BodyLength = header[2] + 2;
this.DataType = header[0];
this.OrderType = header[1];
return true;
}
}
public class MyFixedHeaderCustomDataHandlingAdapter : CustomFixedHeaderDataHandlingAdapter<MyFixedHeaderRequestInfo>
{
/// <summary>
/// 接口实现,指示固定包头长度
/// </summary>
public override int HeaderLength => 3;
/// <summary>
/// 获取新实例
/// </summary>
/// <returns></returns>
protected override MyFixedHeaderRequestInfo GetInstance()
{
return new MyFixedHeaderRequestInfo();
}
}
}