Files
TouchSocket/examples/Adapter简单示例/AdapterConsoleApp/MyCustomBetweenAndDataHandlingAdapter.cs
若汝棋茗 e4819ff3ca 更新引用
2022-08-01 21:44:07 +08:00

53 lines
1.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.

using System.Text;
using TouchSocket.Sockets;
namespace AdapterConsoleApp
{
internal class MyCustomBetweenAndDataHandlingAdapter : CustomBetweenAndDataHandlingAdapter<MyBetweenAndRequestInfo>
{
public MyCustomBetweenAndDataHandlingAdapter()
{
this.MinSize = 5;//表示实际数据体不会小于5例如“**12##12##”数据解析后会解析成“12##12”
}
public override byte[] StartCode => Encoding.UTF8.GetBytes("**");//可以为0长度字节意味着没有起始标识。
public override byte[] EndCode => Encoding.UTF8.GetBytes("##");//必须为有效值。
public override bool CanSendRequestInfo => false;
protected override MyBetweenAndRequestInfo GetInstance()
{
return new MyBetweenAndRequestInfo();
}
protected override void PreviewSend(IRequestInfo requestInfo, bool isAsync)
{
throw new System.NotImplementedException();
}
}
/// <summary>
/// 以**12##12##Min=5为例。
/// </summary>
internal class MyBetweenAndRequestInfo : IBetweenAndRequestInfo
{
public byte[] Body { get; private set; }
public void OnParsingBody(byte[] body)
{
this.Body = body;
//这里的Body应该为12##12
}
public bool OnParsingEndCode(byte[] endCode)
{
return true;//该返回值决定是否执行Receive
}
public bool OnParsingStartCode(byte[] startCode)
{
return true;
}
}
}