DataFrame.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using IFramework.Packets;
  2. using System;
  3. namespace IFramework.Net.WebSocket
  4. {
  5. internal class DataFrame
  6. {
  7. /// <summary>
  8. /// 如果为true则该消息为消息尾部,如果false为零则还有后续数据包;
  9. /// </summary>
  10. public bool IsEof { get; set; } = true;
  11. /// <summary>
  12. /// RSV1,RSV2,RSV3,各1位,用于扩展定义的,如果没有扩展约定的情况则必须为0
  13. /// </summary>
  14. public bool Rsv1 { get; set; }
  15. public bool Rsv2 { get; set; }
  16. public bool Rsv3 { get; set; }
  17. /// <summary>
  18. ///0x0表示附加数据帧
  19. ///0x1表示文本数据帧
  20. ///0x2表示二进制数据帧
  21. ///0x3-7暂时无定义,为以后的非控制帧保留
  22. ///0x8表示连接关闭
  23. ///0x9表示ping
  24. ///0xA表示pong
  25. ///0xB-F暂时无定义,为以后的控制帧保留
  26. /// </summary>
  27. public byte OpCode { get; set; } = 0x01;
  28. /// <summary>
  29. /// true使用掩码解析消息
  30. /// </summary>
  31. public bool Mask { get; set; } = false;
  32. public long PayloadLength { get; set; }
  33. //public int Continued { get; set; }
  34. public byte[] MaskKey { get; set; }
  35. //public UInt16 MaskKeyContinued { get; set; }
  36. public SegmentOffset Payload { get; set; }
  37. public byte[] EncodingToBytes()
  38. {
  39. if (Payload == null
  40. || Payload.buffer.LongLength != PayloadLength)
  41. throw new Exception("payload buffer error");
  42. if (Payload.size > 0)
  43. {
  44. PayloadLength = Payload.size;
  45. }
  46. long headLen = (Mask ? 6 : 2);
  47. if (PayloadLength < 126)
  48. { }
  49. else if (PayloadLength >= 126 && PayloadLength < 127)
  50. {
  51. headLen += 2;
  52. }
  53. else if (PayloadLength >= 127)
  54. {
  55. headLen += 8;
  56. }
  57. byte[] buffer = new byte[headLen + PayloadLength];
  58. int pos = 0;
  59. buffer[pos] = (byte)(IsEof ? 128 : 0);
  60. buffer[pos] += OpCode;
  61. buffer[++pos] = (byte)(Mask ? 128 : 0);
  62. if (PayloadLength < 0x7e)//126
  63. {
  64. buffer[pos] += (byte)PayloadLength;
  65. }
  66. else if (PayloadLength < 0xffff)//65535
  67. {
  68. buffer[++pos] = 126;
  69. buffer[++pos] = (byte)(buffer.Length >> 8);
  70. buffer[++pos] = (byte)(buffer.Length);
  71. }
  72. else
  73. {
  74. var payLengthBytes = ((ulong)PayloadLength).ToBytes();
  75. buffer[++pos] = 127;
  76. buffer[++pos] = payLengthBytes[0];
  77. buffer[++pos] = payLengthBytes[1];
  78. buffer[++pos] = payLengthBytes[2];
  79. buffer[++pos] = payLengthBytes[3];
  80. buffer[++pos] = payLengthBytes[4];
  81. buffer[++pos] = payLengthBytes[5];
  82. buffer[++pos] = payLengthBytes[6];
  83. buffer[++pos] = payLengthBytes[7];
  84. }
  85. if (Mask)
  86. {
  87. buffer[++pos] = MaskKey[0];
  88. buffer[++pos] = MaskKey[1];
  89. buffer[++pos] = MaskKey[2];
  90. buffer[++pos] = MaskKey[3];
  91. for (long i = 0; i < PayloadLength; ++i)
  92. {
  93. buffer[headLen + i] = (byte)(Payload.buffer[i+Payload.offset] ^ MaskKey[i % 4]);
  94. }
  95. }
  96. else
  97. {
  98. for (long i = 0; i < PayloadLength; ++i)
  99. {
  100. buffer[headLen + i] = Payload.buffer[i+Payload.offset];
  101. }
  102. }
  103. return buffer;
  104. }
  105. public bool DecodingFromBytes(SegmentOffset data, bool isMaskResolve = true)
  106. {
  107. if (data.size < 4) return false;
  108. int pos = data.offset;
  109. IsEof = (data.buffer[pos] >> 7) == 1;
  110. OpCode = (byte)(data.buffer[pos] & 0xf);
  111. Mask = (data.buffer[++pos] >> 7) == 1;
  112. PayloadLength = (data.buffer[pos] & 0x7f);
  113. //校验截取长度
  114. if (PayloadLength >= data.size) return false;
  115. ++pos;
  116. //数据包长度超过126,需要解析附加数据
  117. if (PayloadLength < 126)
  118. {
  119. //直接等于消息长度
  120. }
  121. if (PayloadLength == 126)
  122. {
  123. PayloadLength = data.buffer.ToUInt16(pos);// BitConverter.ToUInt16(segOffset.buffer, pos);
  124. pos += 2;
  125. }
  126. else if(PayloadLength==127)
  127. {
  128. PayloadLength = (long)data.buffer.ToUInt64(pos);
  129. pos += 8;
  130. }
  131. Payload = new SegmentOffset()
  132. {
  133. offset = pos,
  134. buffer = data.buffer,
  135. size = (int)PayloadLength
  136. };
  137. //数据体
  138. if (Mask)
  139. {
  140. //获取掩码密钥
  141. MaskKey = new byte[4];
  142. MaskKey[0] = data.buffer[pos];
  143. MaskKey[1] = data.buffer[pos + 1];
  144. MaskKey[2] = data.buffer[pos + 2];
  145. MaskKey[3] = data.buffer[pos + 3];
  146. pos += 4;
  147. Payload.buffer = data.buffer;
  148. Payload.offset = pos;
  149. if (isMaskResolve)
  150. {
  151. long p = 0;
  152. for (long i = 0; i < PayloadLength; ++i)
  153. {
  154. p = (long)pos + i;
  155. Payload.buffer[p] = (byte)(Payload.buffer[p] ^ MaskKey[i % 4]);
  156. }
  157. }
  158. }
  159. else
  160. {
  161. Payload.buffer = data.buffer;
  162. Payload.offset = pos;
  163. }
  164. return true;
  165. }
  166. }
  167. }