DataBuff.cs 5.8 KB

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