WebSocketDataReceiveFilter.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SuperSocket.Common;
  6. using SuperSocket.SocketBase;
  7. using SuperSocket.SocketBase.Command;
  8. using SuperSocket.SocketBase.Protocol;
  9. namespace SuperWebSocket.Protocol
  10. {
  11. class WebSocketDataReceiveFilter : WebSocketReceiveFilterBase
  12. {
  13. private byte? m_Type;
  14. private int m_TempLength;
  15. private int? m_Length;
  16. private const byte m_ClosingHandshakeType = 0xFF;
  17. public WebSocketDataReceiveFilter(WebSocketReceiveFilterBase prevFilter)
  18. : base(prevFilter)
  19. {
  20. }
  21. public override IWebSocketFragment Filter(byte[] readBuffer, int offset, int length, bool isReusableBuffer, out int rest)
  22. {
  23. rest = 0;
  24. var skipByteCount = 0;
  25. if (!m_Type.HasValue)
  26. {
  27. byte startByte = readBuffer[offset];
  28. skipByteCount = 1;
  29. m_Type = startByte;
  30. }
  31. //0xxxxxxx: Collect protocol data by end mark
  32. if ((m_Type.Value & 0x80) == 0x00)
  33. {
  34. byte lookForByte = 0xFF;
  35. int i;
  36. for (i = offset + skipByteCount; i < offset + length; i++)
  37. {
  38. if (readBuffer[i] == lookForByte)
  39. {
  40. rest = length - (i - offset + 1);
  41. if (BufferSegments.Count <= 0)
  42. {
  43. var commandInfo = new PlainFragment(Encoding.UTF8.GetString(readBuffer, offset + skipByteCount, i - offset - skipByteCount));
  44. Reset();
  45. return commandInfo;
  46. }
  47. else
  48. {
  49. AddArraySegment(readBuffer, offset + skipByteCount, i - offset - skipByteCount, false);
  50. var commandInfo = new PlainFragment(BufferSegments.Decode(Encoding.UTF8));
  51. Reset();
  52. return commandInfo;
  53. }
  54. }
  55. }
  56. AddArraySegment(readBuffer, offset + skipByteCount, length - skipByteCount, isReusableBuffer);
  57. return null;
  58. }
  59. else//10000000: Collect protocol data by length
  60. {
  61. while (!m_Length.HasValue)
  62. {
  63. if (length <= skipByteCount)
  64. {
  65. //No data to read
  66. return null;
  67. }
  68. byte lengthByte = readBuffer[skipByteCount];
  69. //Closing handshake
  70. if (lengthByte == 0x00 && m_Type.Value == m_ClosingHandshakeType)
  71. {
  72. Session.Close(CloseReason.ClientClosing);
  73. return null;
  74. }
  75. int thisLength = (int)(lengthByte & 0x7F);
  76. m_TempLength = m_TempLength * 128 + thisLength;
  77. skipByteCount++;
  78. if ((lengthByte & 0x80) != 0x80)
  79. {
  80. m_Length = m_TempLength;
  81. break;
  82. }
  83. }
  84. int requiredSize = m_Length.Value - BufferSegments.Count;
  85. int leftSize = length - skipByteCount;
  86. if (leftSize < requiredSize)
  87. {
  88. AddArraySegment(readBuffer, skipByteCount, length - skipByteCount, isReusableBuffer);
  89. return null;
  90. }
  91. else
  92. {
  93. rest = leftSize - requiredSize;
  94. if (BufferSegments.Count <= 0)
  95. {
  96. var commandInfo = new PlainFragment(Encoding.UTF8.GetString(readBuffer, offset + skipByteCount, requiredSize));
  97. Reset();
  98. return commandInfo;
  99. }
  100. else
  101. {
  102. AddArraySegment(readBuffer, offset + skipByteCount, requiredSize, false);
  103. var commandInfo = new PlainFragment(BufferSegments.Decode(Encoding.UTF8));
  104. Reset();
  105. return commandInfo;
  106. }
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// Resets this instance.
  112. /// </summary>
  113. public override void Reset()
  114. {
  115. base.Reset();
  116. m_Type = null;
  117. m_Length = null;
  118. m_TempLength = 0;
  119. }
  120. }
  121. }