DataBuff.cs 5.7 KB

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