说明

有时候,我们需要解析的数据是非固定包头的,例如:HTTP数据格式,其数据头和数据体由“\r\n”隔开,而数据头又因为请求者的请求信息的不同,头部数据量不固定,而数据体的长度,也是由数据头的ContentLength的值显式指定的,所以,可以考虑使用CustomUnfixedHeaderDataHandlingAdapter解析。

CustomUnfixedHeaderDataHandlingAdapter的使用,和CustomFixedHeaderDataHandlingAdapter基本一致,只是在解析头部时,框架会投递byteBlock的不固定长度的数据。

其余操作和固定包头一致,此处不再赘述。

  1. public bool OnParsingHeader(ByteBlock byteBlock, int length)
  2. {
  3. int index = byteBlock.Buffer.IndexOfFirst(byteBlock.Pos, length, terminatorCode);
  4. if (index > 0)
  5. {
  6. int headerLength = index - byteBlock.Pos;
  7. this.ReadHeaders(byteBlock.Buffer, byteBlock.Pos, headerLength);
  8. byteBlock.Pos += headerLength;
  9. return true;
  10. }
  11. else
  12. {
  13. return false;
  14. }
  15. }