DataBuff.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. namespace ProtoDataBuff
  3. {
  4. //常量数据
  5. public class Constants
  6. {
  7. //消息:数据总长度(4byte) + 数据类型(2byte) + 数据(N byte)
  8. public static int HEAD_DATA_LEN = 4;
  9. public static int HEAD_TYPE_LEN = 2;
  10. public static int HEAD_LEN//6byte
  11. {
  12. get { return HEAD_DATA_LEN + HEAD_TYPE_LEN; }
  13. }
  14. }
  15. /// <summary>
  16. /// 网络数据结构
  17. /// </summary>
  18. [Serializable]
  19. public struct sSocketData
  20. {
  21. public byte[] _data;
  22. public eProtocalCommand _protocallType;
  23. public int _buffLength;
  24. public int _dataLength;
  25. /// <summary>
  26. /// 网络结构转数据
  27. /// </summary>
  28. /// <param name="tmpSocketData"></param>
  29. /// <returns></returns>
  30. public byte[] ToBytes()
  31. {
  32. byte[] _tmpBuff = new byte[_buffLength];
  33. byte[] _tmpBuffLength = BitConverter.GetBytes(_buffLength);
  34. byte[] _tmpDataLenght = BitConverter.GetBytes((UInt16)_protocallType);
  35. Array.Copy(_tmpBuffLength, 0, _tmpBuff, 0, Constants.HEAD_DATA_LEN);//缓存总长度
  36. Array.Copy(_tmpDataLenght, 0, _tmpBuff, Constants.HEAD_DATA_LEN, Constants.HEAD_TYPE_LEN);//协议类型
  37. Array.Copy(_data, 0, _tmpBuff, Constants.HEAD_LEN, _dataLength);//协议数据
  38. return _tmpBuff;
  39. }
  40. /// <summary>
  41. /// 数据转网络结构
  42. /// </summary>
  43. /// <param name="_protocalType"></param>
  44. /// <param name="_data"></param>
  45. /// <returns></returns>
  46. public static sSocketData FromBytes(eProtocalCommand _protocalType, byte[] _data)
  47. {
  48. sSocketData tmpSocketData = new();
  49. tmpSocketData._buffLength = Constants.HEAD_LEN + _data.Length;
  50. tmpSocketData._dataLength = _data.Length;
  51. tmpSocketData._protocallType = _protocalType;
  52. tmpSocketData._data = _data;
  53. return tmpSocketData;
  54. }
  55. }
  56. /// <summary>
  57. /// 网络数据缓存器,//自动大小数据缓存器
  58. /// </summary>
  59. [Serializable]
  60. public class DataBuffer
  61. {
  62. private int _minBuffLen;
  63. private byte[] _buff;
  64. private int _curBuffPosition;
  65. private int _buffLength = 0;
  66. private int _dataLength;
  67. private UInt16 _protocalType;
  68. /// <summary>
  69. /// 构造函数
  70. /// </summary>
  71. /// <param name="_minBuffLen">最小缓冲区大小</param>
  72. public DataBuffer(int _minBuffLen = 1024)
  73. {
  74. if (_minBuffLen <= 0)
  75. {
  76. this._minBuffLen = 1024;
  77. }
  78. else
  79. {
  80. this._minBuffLen = _minBuffLen;
  81. }
  82. _buff = new byte[this._minBuffLen];
  83. }
  84. /// <summary>
  85. /// 添加缓存数据
  86. /// </summary>
  87. /// <param name="_data"></param>
  88. /// <param name="_dataLen"></param>
  89. public void AddBuffer(byte[] _data, int _dataLen)
  90. {
  91. if (_dataLen > _buff.Length - _curBuffPosition)//超过当前缓存
  92. {
  93. byte[] _tmpBuff = new byte[_curBuffPosition + _dataLen];
  94. Array.Copy(_buff, 0, _tmpBuff, 0, _curBuffPosition);
  95. Array.Copy(_data, 0, _tmpBuff, _curBuffPosition, _dataLen);
  96. _buff = _tmpBuff;
  97. _tmpBuff = null;
  98. }
  99. else
  100. {
  101. Array.Copy(_data, 0, _buff, _curBuffPosition, _dataLen);
  102. }
  103. _curBuffPosition += _dataLen;//修改当前数据标记
  104. }
  105. /// <summary>
  106. /// 更新数据长度
  107. /// </summary>
  108. public void UpdateDataLength()
  109. {
  110. if (_dataLength == 0 && _curBuffPosition >= Constants.HEAD_LEN)
  111. {
  112. byte[] tmpDataLen = new byte[Constants.HEAD_DATA_LEN];
  113. Array.Copy(_buff, 0, tmpDataLen, 0, Constants.HEAD_DATA_LEN);
  114. _buffLength = BitConverter.ToInt32(tmpDataLen, 0);
  115. byte[] tmpProtocalType = new byte[Constants.HEAD_TYPE_LEN];
  116. Array.Copy(_buff, Constants.HEAD_DATA_LEN, tmpProtocalType, 0, Constants.HEAD_TYPE_LEN);
  117. _protocalType = BitConverter.ToUInt16(tmpProtocalType, 0);
  118. _dataLength = _buffLength - Constants.HEAD_LEN;
  119. }
  120. }
  121. /// <summary>
  122. /// 获取一条可用数据,返回值标记是否有数据
  123. /// </summary>
  124. /// <param name="_tmpSocketData"></param>
  125. /// <returns></returns>
  126. public bool GetData(out sSocketData _tmpSocketData)
  127. {
  128. _tmpSocketData = new sSocketData();
  129. if (_buffLength <= 0)
  130. {
  131. UpdateDataLength();
  132. }
  133. if (_buffLength > 0 && _curBuffPosition >= _buffLength)
  134. {
  135. _tmpSocketData._buffLength = _buffLength;
  136. _tmpSocketData._dataLength = _dataLength;
  137. _tmpSocketData._protocallType = (eProtocalCommand)_protocalType;
  138. _tmpSocketData._data = new byte[_dataLength];
  139. Array.Copy(_buff, Constants.HEAD_LEN, _tmpSocketData._data, 0, _dataLength);
  140. _curBuffPosition -= _buffLength;
  141. byte[] _tmpBuff = new byte[_curBuffPosition < _minBuffLen ? _minBuffLen : _curBuffPosition];
  142. Array.Copy(_buff, _buffLength, _tmpBuff, 0, _curBuffPosition);
  143. _buff = _tmpBuff;
  144. _buffLength = 0;
  145. _dataLength = 0;
  146. _protocalType = 0;
  147. return true;
  148. }
  149. return false;
  150. }
  151. }
  152. }