TCPStream.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace NetCore31BACNetTransfor
  5. {
  6. public class TCPStream
  7. {
  8. private bool separateBytes;
  9. private byte prefix;
  10. private byte suffix;
  11. private byte[] prefixBytes;
  12. private byte[] suffixBytes;
  13. private List<byte> contents = new List<byte>();
  14. private bool content_valid;
  15. private List<byte> buffer = new List<byte>();
  16. public TCPStream(byte prefix, byte suffix)
  17. {
  18. this.separateBytes = false;
  19. this.prefix = prefix;
  20. this.suffix = suffix;
  21. }
  22. public TCPStream(byte[] prefixBytes, byte[] suffixBytes)
  23. {
  24. this.separateBytes = true;
  25. this.prefixBytes = prefixBytes;
  26. this.suffixBytes = suffixBytes;
  27. }
  28. public List<byte[]> Process(byte[] bytes, int length)
  29. {
  30. List<byte[]> list = new List<byte[]>();
  31. if (this.separateBytes)
  32. {
  33. for (int i = 0; i < length; i++)
  34. {
  35. this.buffer.Add(bytes[i]);
  36. }
  37. while (true)
  38. {
  39. int num = this.FindIndexForward(this.buffer, this.buffer.Count, this.suffixBytes);
  40. if (num == -1)
  41. {
  42. break;
  43. }
  44. int num2 = this.FindIndexBackward(this.buffer, num, this.prefixBytes);
  45. if (num2 != -1)
  46. {
  47. int num3 = num - num2 - this.prefixBytes.Length;
  48. if (num3 > 0)
  49. {
  50. byte[] array = new byte[num3];
  51. for (int j = 0; j < num3; j++)
  52. {
  53. array[j] = this.buffer[num2 + this.prefixBytes.Length + j];
  54. }
  55. list.Add(array);
  56. }
  57. }
  58. for (int k = 0; k < num + this.suffixBytes.Length; k++)
  59. {
  60. this.buffer.RemoveAt(0);
  61. }
  62. }
  63. }
  64. else
  65. {
  66. for (int l = 0; l < length; l++)
  67. {
  68. byte b = bytes[l];
  69. if (b == this.suffix)
  70. {
  71. if (this.content_valid && this.contents.Count > 0)
  72. {
  73. byte[] array2 = new byte[this.contents.Count];
  74. for (int m = 0; m < this.contents.Count; m++)
  75. {
  76. array2[m] = this.contents[m];
  77. }
  78. list.Add(array2);
  79. }
  80. this.contents.Clear();
  81. this.content_valid = false;
  82. }
  83. else if (b == this.prefix)
  84. {
  85. this.contents.Clear();
  86. this.content_valid = true;
  87. }
  88. else if (this.content_valid)
  89. {
  90. this.contents.Add(b);
  91. }
  92. }
  93. }
  94. return list;
  95. }
  96. private int FindIndexForward(List<byte> byteList, int length, byte[] bytes)
  97. {
  98. int result = -1;
  99. for (int i = 0; i <= length - bytes.Length; i++)
  100. {
  101. if (this.Match(byteList, i, bytes))
  102. {
  103. result = i;
  104. break;
  105. }
  106. }
  107. return result;
  108. }
  109. private int FindIndexBackward(List<byte> byteList, int length, byte[] bytes)
  110. {
  111. int result = -1;
  112. for (int i = length - bytes.Length; i >= 0; i--)
  113. {
  114. if (this.Match(byteList, i, bytes))
  115. {
  116. result = i;
  117. break;
  118. }
  119. }
  120. return result;
  121. }
  122. private bool Match(List<byte> byteList, int index, byte[] bytes)
  123. {
  124. bool result = true;
  125. for (int i = 0; i < bytes.Length; i++)
  126. {
  127. if (byteList[index + i] != bytes[i])
  128. {
  129. result = false;
  130. break;
  131. }
  132. }
  133. return result;
  134. }
  135. }
  136. }