DataBuff.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.IO;
  2. using System;
  3. using System.Net;
  4. using UnityEngine;
  5. using pb = Google.Protobuf;
  6. /// <summary>
  7. /// socket通讯-常量数据
  8. /// </summary>
  9. public class Csts
  10. {
  11. //消息:数据总长度(4byte) + 数据类型(2byte) + 数据(N byte)
  12. public static int HEAD_DATA_LEN = 4;
  13. public static int HEAD_TYPE_LEN = 2;
  14. public static int HEAD_LEN//6byte
  15. {
  16. get { return HEAD_DATA_LEN + HEAD_TYPE_LEN; }
  17. }
  18. }
  19. /// <summary>
  20. /// 网络数据结构
  21. /// </summary>
  22. [Serializable]
  23. public struct sSocketData
  24. {
  25. /// <summary>
  26. /// 包长度
  27. /// </summary>
  28. public Int32 PackLen { get; private set; }
  29. /// <summary>
  30. /// 消息类型
  31. /// </summary>
  32. public eProtocalCommand ProtocallType { get; private set; }
  33. /// <summary>
  34. /// 包负载
  35. /// </summary>
  36. public byte[] Data { get; private set; }
  37. public static sSocketData FromIMsg(eProtocalCommand _protocalType, pb.IMessage msg)
  38. {
  39. return FromBytes(_protocalType, msg.ToBytes());
  40. }
  41. /// <summary>
  42. /// 网络结构转数据
  43. /// </summary>
  44. /// <param name="tmpSocketData"></param>
  45. /// <returns></returns>
  46. public byte[] ToBytes()
  47. {
  48. var retArr = new byte[PackLen];
  49. byte[] btsPackLen = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(PackLen));
  50. byte[] btsPackType = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((Int16)ProtocallType));
  51. Buffer.BlockCopy(btsPackLen, 0, retArr, 0, Csts.HEAD_DATA_LEN); //缓存总长度
  52. Buffer.BlockCopy(btsPackType, 0, retArr, Csts.HEAD_DATA_LEN, Csts.HEAD_TYPE_LEN); //协议类型
  53. Buffer.BlockCopy(Data, 0, retArr, Csts.HEAD_LEN, Data.Length); //协议数据
  54. return retArr;
  55. }
  56. /// <summary>
  57. /// 数据转网络结构
  58. /// </summary>
  59. /// <param name="_protocalType"></param>
  60. /// <param name="_data"></param>
  61. /// <returns></returns>
  62. public static sSocketData FromBytes(eProtocalCommand _protocalType, byte[] _data)
  63. {
  64. var tmpSocketData = new sSocketData()
  65. {
  66. PackLen = Csts.HEAD_LEN + _data.Length,
  67. ProtocallType = _protocalType,
  68. Data = _data
  69. };
  70. return tmpSocketData;
  71. }
  72. }
  73. /// <summary>
  74. /// 网络数据缓存器,//自动大小数据缓存器
  75. /// </summary>
  76. [Serializable]
  77. public class DataBuffer
  78. {
  79. private byte[] buff = new byte[0];
  80. private int nextPackLen = 0;
  81. /// <summary>
  82. /// 添加缓存数据
  83. /// </summary>
  84. /// <param name="_data"></param>
  85. /// <param name="_dataLen"></param>
  86. public void AddBuffer(byte[] _data, int _dataLen)
  87. {
  88. var lp = buff.Length;
  89. Array.Resize<byte>(ref buff, lp + _dataLen);
  90. Buffer.BlockCopy(_data, 0, buff, lp, _dataLen);
  91. }
  92. /// <summary>
  93. /// 更新数据长度
  94. /// </summary>
  95. public void UpdateDataLength()
  96. {
  97. if (buff.Length >= Csts.HEAD_LEN)
  98. {
  99. nextPackLen = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buff[0..Csts.HEAD_DATA_LEN], 0));
  100. }
  101. }
  102. /// <summary>
  103. /// 获取一条可用数据,返回值标记是否有数据
  104. /// </summary>
  105. /// <param name="SocketData"></param>
  106. /// <returns></returns>
  107. public bool GetData(out sSocketData SocketData)
  108. {
  109. if (nextPackLen <= 0)
  110. {
  111. UpdateDataLength();
  112. }
  113. if (nextPackLen > 0 && buff.Length >= nextPackLen)
  114. {
  115. var _protocalType = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buff[Csts.HEAD_DATA_LEN..Csts.HEAD_LEN], 0));
  116. SocketData = sSocketData.FromBytes((eProtocalCommand)_protocalType, buff[Csts.HEAD_LEN..nextPackLen]);
  117. buff = buff[nextPackLen..];
  118. nextPackLen = 0;
  119. return true;
  120. }
  121. SocketData = default;
  122. return false;
  123. }
  124. }