Files
TouchSocket/examples/Adapter简单示例/AdapterConsoleApp/MyFixedHeaderCustomDataHandlingAdapter.cs
2023-02-07 19:53:23 +08:00

98 lines
2.5 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.

using TouchSocket.Sockets;
namespace AdapterConsoleApp
{
/// <summary>
/// 模板解析“固定包头”数据适配器
/// </summary>
public class MyFixedHeaderCustomDataHandlingAdapter : CustomFixedHeaderDataHandlingAdapter<MyFixedHeaderRequestInfo>
{
public MyFixedHeaderCustomDataHandlingAdapter()
{
this.MaxPackageSize = 1024;
}
/// <summary>
/// 接口实现,指示固定包头长度
/// </summary>
public override int HeaderLength => 3;
public override bool CanSendRequestInfo => false;
/// <summary>
/// 获取新实例
/// </summary>
/// <returns></returns>
protected override MyFixedHeaderRequestInfo GetInstance()
{
return new MyFixedHeaderRequestInfo();
}
protected override void PreviewSend(IRequestInfo requestInfo)
{
throw new System.NotImplementedException();
}
}
public class MyFixedHeaderRequestInfo : IFixedHeaderRequestInfo
{
private int bodyLength;
/// <summary>
/// 接口实现,标识数据长度
/// </summary>
public int BodyLength
{
get { return bodyLength; }
}
private byte dataType;
/// <summary>
/// 自定义属性,标识数据类型
/// </summary>
public byte DataType
{
get { return dataType; }
}
private byte orderType;
/// <summary>
/// 自定义属性,标识指令类型
/// </summary>
public byte OrderType
{
get { return orderType; }
}
private byte[] body;
/// <summary>
/// 自定义属性,标识实际数据
/// </summary>
public byte[] Body
{
get { return body; }
}
public bool OnParsingBody(byte[] body)
{
if (body.Length == this.bodyLength)
{
this.body = body;
return true;
}
return false;
}
public bool OnParsingHeader(byte[] header)
{
//在该示例中第一个字节表示后续的所有数据长度但是header设置的是3所以后续还应当接收length-2个长度。
this.bodyLength = header[0] - 2;
this.dataType = header[1];
this.orderType = header[2];
return true;
}
}
}