|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using TouchSocket.Core;
|
|
|
|
|
using TouchSocket.Sockets;
|
|
|
|
|
|
|
|
|
|
namespace Admin.Core.Socket.TSocket
|
|
|
|
|
{
|
|
|
|
|
public class MyFixedHeaderDataHandlingAdapter : CustomFixedHeaderDataHandlingAdapter<MyRequestInfo>
|
|
|
|
|
{
|
|
|
|
|
public override int HeaderLength => 3;
|
|
|
|
|
|
|
|
|
|
public override bool CanSendRequestInfo => false;
|
|
|
|
|
|
|
|
|
|
protected override MyRequestInfo GetInstance()
|
|
|
|
|
{
|
|
|
|
|
return new MyRequestInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PreviewSend(IRequestInfo requestInfo)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public class MyRequestInfo : IFixedHeaderRequestInfo
|
|
|
|
|
{
|
|
|
|
|
public DataType DataType { get; set; }
|
|
|
|
|
public byte[] Data { get; set; }
|
|
|
|
|
|
|
|
|
|
public int BodyLength { get; private set; }
|
|
|
|
|
|
|
|
|
|
public bool OnParsingBody(byte[] body)
|
|
|
|
|
{
|
|
|
|
|
if (body.Length == this.BodyLength)
|
|
|
|
|
{
|
|
|
|
|
this.Data = body;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool OnParsingHeader(byte[] header)
|
|
|
|
|
{
|
|
|
|
|
if (header.Length == 3)
|
|
|
|
|
{
|
|
|
|
|
this.BodyLength = TouchSocketBitConverter.Default.ToUInt16(header, 0) - 1;
|
|
|
|
|
this.DataType = (DataType)header[2];
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Package(ByteBlock byteBlock)
|
|
|
|
|
{
|
|
|
|
|
byteBlock.Write((ushort)((this.Data == null ? 0 : this.Data.Length) + 1));
|
|
|
|
|
byteBlock.Write((byte)this.DataType);
|
|
|
|
|
if (Data != null)
|
|
|
|
|
{
|
|
|
|
|
byteBlock.Write(Data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte[] PackageAsBytes()
|
|
|
|
|
{
|
|
|
|
|
using ByteBlock byteBlock = new ByteBlock();
|
|
|
|
|
this.Package(byteBlock);
|
|
|
|
|
return byteBlock.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"数据类型={this.DataType},数据={(this.Data == null ? "null" : Encoding.UTF8.GetString(this.Data))}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public enum DataType : byte
|
|
|
|
|
{
|
|
|
|
|
Ping,
|
|
|
|
|
Pong,
|
|
|
|
|
Data
|
|
|
|
|
}
|
|
|
|
|
}
|