PackageHandler.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. // RayMix Libs - RayMix's .Net Libs
  2. // Copyright 2018 Ray@raymix.net. All rights reserved.
  3. // https://www.raymix.net
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of RayMix.net. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. using CScript.App;
  31. using CScript.Entity;
  32. using CScript.Utilities;
  33. using OpenCVForUnity.CoreModule;
  34. using OpenCVForUnity.ImgcodecsModule;
  35. using OpenCVForUnity.ImgprocModule;
  36. using OpenCVForUnity.UnityUtils;
  37. using System;
  38. using System.Collections.Generic;
  39. using System.Diagnostics;
  40. using System.IO;
  41. using System.Linq;
  42. namespace CScript.Net
  43. {
  44. public struct PackageCheck
  45. {
  46. public string frameSymbols;
  47. public int frameLenght;
  48. public byte frameLastByteValue;
  49. }
  50. /// <summary>
  51. /// PackageHandler
  52. /// 数据包处理器
  53. /// </summary>
  54. public class PackageHandler : PackageHandler<object>
  55. {
  56. public PackageHandler(object sender) : base(sender)
  57. {
  58. }
  59. }
  60. /// <summary>
  61. /// PackageHandler
  62. /// 数据包处理器
  63. /// </summary>
  64. /// <typeparam name="T">消息发送者类型</typeparam>
  65. public class PackageHandler<T>
  66. {
  67. //private MemoryStream stream = new MemoryStream(1024*1024*8);
  68. // private MemoryStream stream = new MemoryStream(4096000);
  69. private MemoryStream stream = new MemoryStream(1024*1024*10);
  70. private int readOffset = 0;
  71. private T sender;
  72. NetMeshData netMeshData;
  73. private List<short> _shortBufferList;
  74. private List<byte> _byteBufferList;
  75. //private byte[] _byteBufferArray = new byte[232643];
  76. int totalByteCount;
  77. int lostCount;
  78. //private MemoryStream _streamDataBuffer = new MemoryStream(409600 * 10);
  79. private MemoryStream _streamDataBuffer = new MemoryStream(1024 * 1024 * 10);
  80. private bool _start = false;
  81. string packageTypeStr = string.Empty;
  82. Mat rgbMat;
  83. private float _decodeTime;
  84. //private Dictionary<int, PackageCheck> _packageCheckInfoDic;
  85. public PackageHandler(T sender)
  86. {
  87. this.sender = sender;
  88. netMeshData = new NetMeshData();
  89. _shortBufferList = new List<short>();
  90. _byteBufferList = new List<byte>();
  91. totalByteCount = 0;
  92. lostCount = 0;
  93. //_isNewData = App.AppManager.Instance.isNewMeshData;
  94. //_packageCheckInfoDic = new Dictionary<int, PackageCheck>();
  95. }
  96. /// <summary>
  97. /// 接收数据到PackageHandler
  98. /// </summary>
  99. /// <param name="data"></param>
  100. /// <param name="offset"></param>
  101. /// <param name="count"></param>
  102. public void ReceiveData(byte[] data, int offset, int count)
  103. {
  104. //UnityEngine.Debug.LogError(data.Length + " $$$" + count + " " + stream.Position);
  105. // UnityEngine.Debug.LogWarning(data.Length + " $$$" + count + " " + stream.Position + " offset: " + offset + " stream.Capacity:" + stream.Capacity);
  106. if (stream.Position + count > stream.Capacity)
  107. {
  108. throw new Exception("PackageHandler write buffer overflow");
  109. }
  110. stream.Write(data, offset, count);
  111. ParsePackageTCP();
  112. //ParsePackage();
  113. }
  114. public void ReceiveQuicData(byte[] data, int offset, int count)
  115. {
  116. if (_streamDataBuffer.Position + count > _streamDataBuffer.Capacity)
  117. {
  118. throw new Exception("PackageHandler write buffer overflow");
  119. }
  120. _streamDataBuffer.Write(data, offset, count);
  121. totalByteCount += count;
  122. netMeshData.Clear();
  123. //UnPack1();
  124. UnityEngine.Debug.LogWarning("_streamDataBuffer.len:"+_streamDataBuffer.Length);
  125. bool isSuccess = UnPack2();
  126. UnityEngine.Debug.LogWarning("lostCount: " + lostCount + "isSuccess:" + isSuccess);
  127. UnityEngine.Debug.LogWarning("netMeshData:"+netMeshData);
  128. if (isSuccess)
  129. {
  130. NetDistribute.Instance.Parsing(CScript.Utilities.AppUtil.Clone<NetMeshData>(netMeshData), rgbMat.clone(), totalByteCount, _decodeTime, lostCount);
  131. }
  132. netMeshData.Clear();
  133. _streamDataBuffer.Position = 0;
  134. _streamDataBuffer.SetLength(0);
  135. totalByteCount = 0;
  136. }
  137. public void Reset()
  138. {
  139. readOffset = 0;
  140. i = 0;
  141. stream.Position = 0;
  142. stream.SetLength(0);
  143. _streamDataBuffer.Position = 0;
  144. _streamDataBuffer.SetLength(0);
  145. _start = false;
  146. }
  147. /// <summary>
  148. /// 打包消息
  149. /// </summary>
  150. /// <param name="message"></param>
  151. /// <returns></returns>
  152. public static byte[] PackMessage()
  153. {
  154. byte[] package = null;
  155. using (MemoryStream ms = new MemoryStream())
  156. {
  157. //ProtoBuf.Serializer.Serialize(ms, message);
  158. package = new byte[ms.Length + 4];
  159. Buffer.BlockCopy(BitConverter.GetBytes(ms.Length), 0, package, 0, 4);
  160. Buffer.BlockCopy(ms.GetBuffer(), 0, package, 4, (int)ms.Length);
  161. }
  162. return package;
  163. }
  164. ///// <summary>
  165. ///// 提取消息
  166. ///// </summary>
  167. ///// <param name="packet"></param>
  168. ///// <param name="offset"></param>
  169. ///// <param name="length"></param>
  170. ///// <returns></returns>
  171. //public static SkillBridge.Message.NetMessage UnpackMessage(byte[] packet,int offset,int length)
  172. //{
  173. // SkillBridge.Message.NetMessage message = null;
  174. // using (MemoryStream ms = new MemoryStream(packet, offset, length))
  175. // {
  176. // message = ProtoBuf.Serializer.Deserialize<SkillBridge.Message.NetMessage>(ms);
  177. // }
  178. // return message;
  179. //}
  180. int i;
  181. private int _frameIndex;
  182. bool ParsePackageTCP()
  183. {
  184. if (!_start)
  185. {
  186. while (readOffset + 20 < stream.Position)
  187. {
  188. packageTypeStr = System.Text.Encoding.ASCII.GetString(stream.GetBuffer(), readOffset + 0, 1);
  189. packageTypeStr += stream.GetBuffer()[readOffset + 1].ToString();
  190. packageTypeStr += System.Text.Encoding.ASCII.GetString(stream.GetBuffer(), readOffset + 2, 1);
  191. packageTypeStr += System.Text.Encoding.ASCII.GetString(stream.GetBuffer(), readOffset + 3, 1);
  192. // UnityEngine.Debug.Log(packageTypeStr);
  193. if (packageTypeStr == "$1@@")
  194. {
  195. _start = true;
  196. break;
  197. }
  198. else
  199. {
  200. readOffset+=1;
  201. }
  202. }
  203. }
  204. else
  205. {
  206. if (readOffset + 20 < stream.Position)
  207. {
  208. int packageSize = BitConverter.ToInt32(stream.GetBuffer(), readOffset + 16);
  209. if (packageSize + readOffset + 20 <= stream.Position)
  210. {
  211. _start = false;
  212. _streamDataBuffer.Position = 0;
  213. _streamDataBuffer.SetLength(0);
  214. _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 20, packageSize);
  215. netMeshData.Clear();
  216. UnPack2();
  217. NetDistribute.Instance.Parsing(CScript.Utilities.AppUtil.Clone<NetMeshData>(netMeshData), rgbMat.clone(), totalByteCount, _decodeTime, 0);
  218. netMeshData.Clear();
  219. totalByteCount = 0;
  220. _frameIndex = 0;
  221. this.readOffset += (packageSize + 20);
  222. return ParsePackageTCP();
  223. }
  224. }
  225. }
  226. //未接收完/要结束了
  227. if (this.readOffset > 0)
  228. {
  229. long size = stream.Position - this.readOffset;
  230. if (this.readOffset < stream.Position)
  231. {
  232. Array.Copy(stream.GetBuffer(), this.readOffset, stream.GetBuffer(), 0, stream.Position - this.readOffset);
  233. }
  234. //Reset Stream
  235. this.readOffset = 0;
  236. stream.Position = size;
  237. stream.SetLength(size);
  238. //UnityEngine.Debug.LogWarning("stream.Position: "+stream.Position+" "+ stream.Length);
  239. netMeshData.Clear();
  240. }
  241. return true;
  242. }
  243. /// <summary>
  244. /// 数据包解析
  245. /// </summary>
  246. /// <returns></returns>
  247. bool ParsePackage()
  248. {
  249. if (!_start)
  250. {
  251. while (readOffset + 13 < stream.Position)
  252. {
  253. packageTypeStr = System.Text.Encoding.UTF8.GetString(stream.GetBuffer(), readOffset + 0, 1);
  254. if (packageTypeStr == "#")
  255. {
  256. _start = true;
  257. break;
  258. }
  259. else
  260. {
  261. readOffset++;
  262. }
  263. }
  264. }
  265. if (_start)
  266. {
  267. //UnityEngine.Debug.LogWarning("_start");
  268. if (readOffset + 13 < stream.Position)
  269. {
  270. packageTypeStr = System.Text.Encoding.UTF8.GetString(stream.GetBuffer(), readOffset + 0, 1);
  271. int packageSize = BitConverter.ToInt32(stream.GetBuffer(), readOffset + 9);
  272. //UnityEngine.Debug.LogWarning(readOffset + " " + stream.Position + " " + packageSize+" "+ packageTypeStr);
  273. if (packageSize + readOffset + 13 <= stream.Position)
  274. {
  275. if (packageTypeStr == "#")
  276. {
  277. // _packageCheckInfoDic.Clear();
  278. _frameIndex = 0;
  279. // UnityEngine.Debug.LogWarning("#");
  280. _streamDataBuffer.Position = 0;
  281. _streamDataBuffer.SetLength(0);
  282. //UnityEngine.Debug.LogWarning("#:"+packageSize);
  283. _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 13, packageSize);
  284. }
  285. else if (packageTypeStr == "@")
  286. {
  287. // UnityEngine.Debug.LogWarning("@");
  288. _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 13, packageSize);
  289. }
  290. else if (packageTypeStr == "$")
  291. {
  292. _start = false;
  293. _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 13, packageSize);
  294. //byte[] unzipData = lz4.Decompress(_streamDataBuffer.GetBuffer());
  295. i++;
  296. netMeshData.Clear();
  297. if (AppConfig.isNewMeshData)
  298. {
  299. UnPack2();
  300. }
  301. else
  302. {
  303. UnPack1();
  304. }
  305. NetDistribute.Instance.Parsing(CScript.Utilities.AppUtil.Clone<NetMeshData>(netMeshData), rgbMat.clone(), totalByteCount, _decodeTime,0);
  306. netMeshData.Clear();
  307. totalByteCount = 0;
  308. _frameIndex = 0;
  309. }
  310. this.readOffset += (packageSize + 13);
  311. return ParsePackage();
  312. }
  313. }
  314. }
  315. //未接收完/要结束了
  316. if (this.readOffset > 0)
  317. {
  318. long size = stream.Position - this.readOffset;
  319. if (this.readOffset < stream.Position)
  320. {
  321. Array.Copy(stream.GetBuffer(), this.readOffset, stream.GetBuffer(), 0, stream.Position - this.readOffset);
  322. }
  323. //Reset Stream
  324. this.readOffset = 0;
  325. stream.Position = size;
  326. stream.SetLength(size);
  327. //UnityEngine.Debug.LogWarning("stream.Position: "+stream.Position+" "+ stream.Length);
  328. netMeshData.Clear();
  329. }
  330. return true;
  331. }
  332. private void UnPack1()
  333. {
  334. int bufferOffset = 0;
  335. //包有效
  336. //总长度
  337. ulong totalLenght = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
  338. bufferOffset += sizeof(UInt64);
  339. // rgb size
  340. uint rgbSize = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset) * 6;
  341. bufferOffset += sizeof(UInt32);
  342. _shortBufferList.Clear();
  343. _shortBufferList.TrimExcess();
  344. _shortBufferList.Capacity = (int)rgbSize;
  345. // rgb 内容
  346. for (int i = 0; i < rgbSize; i++)
  347. {
  348. _shortBufferList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  349. bufferOffset += sizeof(Int16);
  350. }
  351. uint vertexCount = rgbSize / 6;
  352. netMeshData.netSideVertexList.Capacity = (int)vertexCount * 3;
  353. netMeshData.netColorList.Capacity = (int)vertexCount * 3;
  354. // 顶点数据和颜色
  355. for (int i = 0; i < vertexCount; i++)
  356. {
  357. netMeshData.netSideVertexList.Add(_shortBufferList[i * 6 + 0]);
  358. netMeshData.netSideVertexList.Add(_shortBufferList[i * 6 + 1]);
  359. netMeshData.netSideVertexList.Add(_shortBufferList[i * 6 + 2]);
  360. netMeshData.netColorList.Add(_shortBufferList[i * 6 + 3]);
  361. netMeshData.netColorList.Add(_shortBufferList[i * 6 + 4]);
  362. netMeshData.netColorList.Add(_shortBufferList[i * 6 + 5]);
  363. }
  364. // 三角形
  365. UInt64 indices_len_rgb = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
  366. bufferOffset += sizeof(UInt64);
  367. if (indices_len_rgb > Int32.MaxValue)
  368. {
  369. //UnityEngine.Debug.Log("totalLenght:"+ totalLenght+" indices_len_rgb:" + indices_len_rgb);
  370. UnityEngine.Debug.Log($"totalLenght:{totalLenght} rgbSize:{rgbSize} indices_len_rgb:{indices_len_rgb}");
  371. }
  372. netMeshData.netSideTriangles.Capacity = (int)indices_len_rgb;
  373. for (ulong i = 0; i < indices_len_rgb; i++)
  374. {
  375. netMeshData.netSideTriangles.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  376. bufferOffset += sizeof(Int16);
  377. }
  378. int ys_width_len = BitConverter.ToInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  379. bufferOffset += sizeof(Int32);
  380. int ys_height_len = BitConverter.ToInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  381. bufferOffset += sizeof(Int32);
  382. netMeshData.width = ys_width_len;
  383. netMeshData.height = ys_height_len;
  384. // 前面顶点长度
  385. UInt32 vertexes_len = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  386. if (vertexes_len > Int32.MaxValue)
  387. {
  388. UnityEngine.Debug.Log(" vertexes_len*5:" + vertexes_len * 5);
  389. }
  390. UInt32 xyzuvs_size = vertexes_len * 5;
  391. bufferOffset += sizeof(UInt32);
  392. _shortBufferList.Clear();
  393. _shortBufferList.TrimExcess();
  394. _shortBufferList.Capacity = (int)xyzuvs_size;
  395. for (int i = 0; i < xyzuvs_size; i++)
  396. {
  397. _shortBufferList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  398. bufferOffset += sizeof(Int16);
  399. }
  400. netMeshData.netUvList.Capacity = (int)vertexes_len * 2;
  401. netMeshData.netFrontVertexList.Capacity = (int)vertexes_len * 3;
  402. vertexCount = xyzuvs_size / 5;
  403. // 前面顶点 和 uv
  404. for (int i = 0; i < vertexCount; i++)
  405. {
  406. netMeshData.netFrontVertexList.Add(_shortBufferList[i * 5 + 0]);
  407. netMeshData.netFrontVertexList.Add(_shortBufferList[i * 5 + 1]);
  408. netMeshData.netFrontVertexList.Add(_shortBufferList[i * 5 + 2]);
  409. netMeshData.netUvList.Add(_shortBufferList[i * 5 + 3]);
  410. netMeshData.netUvList.Add(_shortBufferList[i * 5 + 4]);
  411. }
  412. _shortBufferList.Clear();
  413. _shortBufferList.TrimExcess();
  414. UInt64 indices_len = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
  415. bufferOffset += sizeof(UInt64);
  416. // 前面三角
  417. netMeshData.netFrontTriangles.Capacity = (int)indices_len;
  418. for (ulong i = 0; i < indices_len; i++)
  419. {
  420. netMeshData.netFrontTriangles.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  421. bufferOffset += sizeof(ushort);
  422. }
  423. // 贴图数据
  424. int imageYEncoderSize = BitConverter.ToInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  425. bufferOffset += sizeof(int);// y byte size
  426. //netMeshData.netTexture.Capacity = imageYEncoderSize;
  427. _byteBufferList.Clear();
  428. _byteBufferList.Capacity = imageYEncoderSize;
  429. //byte temp;
  430. for (int i = 0; i < imageYEncoderSize; i++)
  431. {
  432. _byteBufferList.Add(_streamDataBuffer.GetBuffer()[bufferOffset++]);
  433. }
  434. DecodeImageMat(_byteBufferList.ToArray(),out rgbMat);
  435. }
  436. private bool UnPack2()
  437. {
  438. int bufferOffset = 0;
  439. //总长度
  440. ulong totalLenght = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
  441. bufferOffset += sizeof(UInt64);
  442. int realCount = 0;
  443. // atlsaXyz size
  444. uint atlasXyzBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  445. if (atlasXyzBufferLenght > 1000000)
  446. {
  447. lostCount++;
  448. return false;
  449. }
  450. bufferOffset += sizeof(UInt32);
  451. realCount = (int)(atlasXyzBufferLenght / sizeof(Int16));
  452. //UnityEngine.Debug.LogWarning("0 realCount:" + realCount);
  453. netMeshData.netAtlasVertexList.Capacity = (int)realCount;
  454. for (int i = 0; i < realCount; i++)
  455. {
  456. netMeshData.netAtlasVertexList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  457. bufferOffset += sizeof(Int16);
  458. }
  459. //atlasUv size
  460. uint atlasUvBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  461. if (atlasUvBufferLenght > 1000000)
  462. {
  463. lostCount++;
  464. return false;
  465. }
  466. bufferOffset += sizeof(UInt32);
  467. realCount = (int)(atlasUvBufferLenght / sizeof(UInt16));
  468. //UnityEngine.Debug.LogWarning("1realCount:"+ realCount);
  469. netMeshData.netAtlasUVList.Capacity = (int)realCount;
  470. for (int i = 0; i < realCount; i++)
  471. {
  472. //UnityEngine.Debug.LogError(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  473. netMeshData.netAtlasUVList.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  474. bufferOffset += sizeof(UInt16);
  475. }
  476. //atlasDices size
  477. uint atlasIndicesBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  478. if (atlasIndicesBufferLenght > 1000000)
  479. {
  480. lostCount++;
  481. return false;
  482. }
  483. bufferOffset += sizeof(UInt32);
  484. realCount = (int)(atlasIndicesBufferLenght / sizeof(UInt16));
  485. //UnityEngine.Debug.LogWarning("2realCount:" + realCount);
  486. netMeshData.netTrianglesList.Capacity = (int)realCount;
  487. for (int i = 0; i < realCount; i++)
  488. {
  489. netMeshData.netTrianglesList.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  490. bufferOffset += sizeof(UInt16);
  491. }
  492. // 贴图数据
  493. uint atlasImageBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  494. if (atlasImageBufferLenght > 1000000)
  495. {
  496. lostCount++;
  497. return false;
  498. }
  499. bufferOffset += sizeof(UInt32);// y byte size
  500. //netMeshData.netTexture.Capacity = imageYEncoderSize;
  501. _byteBufferList.Clear();
  502. realCount = (int)(atlasImageBufferLenght / sizeof(byte));
  503. //UnityEngine.Debug.LogWarning("3 realCount:" + realCount);
  504. _byteBufferList.Capacity = (int)realCount;
  505. //byte temp;
  506. for (int i = 0; i < realCount; i++)
  507. {
  508. _byteBufferList.Add(_streamDataBuffer.GetBuffer()[bufferOffset++]);
  509. }
  510. DecodeImageMat(_byteBufferList.ToArray(), out rgbMat);
  511. //nosexyzrgb
  512. uint noseXyzrgbBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  513. if (noseXyzrgbBufferLenght > 1000000)
  514. {
  515. lostCount++;
  516. return false;
  517. }
  518. bufferOffset += sizeof(UInt32);
  519. realCount = (int)(noseXyzrgbBufferLenght / sizeof(short));
  520. _shortBufferList.Clear();
  521. _shortBufferList.TrimExcess();
  522. //UnityEngine.Debug.LogWarning("4 realCount:" + realCount);
  523. _shortBufferList.Capacity = (int)realCount;
  524. // nosexyzrgb 内容
  525. for (int i = 0; i < realCount; i++)
  526. {
  527. _shortBufferList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  528. bufferOffset += sizeof(Int16);
  529. }
  530. uint vertexCount = (uint)realCount / 6;
  531. netMeshData.netSideVertexList.Capacity = (int)vertexCount * 3;
  532. netMeshData.netColorList.Capacity = (int)vertexCount * 3;
  533. // 顶点数据和颜色
  534. for (int i = 0; i < vertexCount; i++)
  535. {
  536. netMeshData.netNoseVertexList.Add(_shortBufferList[i * 6 + 0]);
  537. netMeshData.netNoseVertexList.Add(_shortBufferList[i * 6 + 1]);
  538. netMeshData.netNoseVertexList.Add(_shortBufferList[i * 6 + 2]);
  539. netMeshData.netNoseColorList.Add(_shortBufferList[i * 6 + 3]);
  540. netMeshData.netNoseColorList.Add(_shortBufferList[i * 6 + 4]);
  541. netMeshData.netNoseColorList.Add(_shortBufferList[i * 6 + 5]);
  542. }
  543. //nosexyzrgb
  544. uint noseIndicesBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
  545. if (noseIndicesBufferLenght > 1000000)
  546. {
  547. lostCount++;
  548. return false;
  549. }
  550. bufferOffset += sizeof(UInt32);
  551. realCount = (int)(noseIndicesBufferLenght / sizeof(ushort));
  552. // UnityEngine.Debug.LogWarning("5 realCount:" + realCount);
  553. netMeshData.netNoseTriangles.Capacity = (int)realCount;
  554. for (int i = 0; i < realCount; i++)
  555. {
  556. netMeshData.netNoseTriangles.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
  557. bufferOffset += sizeof(UInt16);
  558. }
  559. return true;
  560. }
  561. private bool CheckSum(Dictionary<int, PackageCheck> packageCheckDic, ulong checksums)
  562. {
  563. UInt64 checkNumLocak = 0;
  564. packageCheckDic.Values.ToList().ForEach(pcidValue => { checkNumLocak += (ulong)pcidValue.frameLenght; checkNumLocak += (ulong)pcidValue.frameLastByteValue; });
  565. //UnityEngine.Debug.LogError(checkNumLocak);
  566. if (checkNumLocak == checksums)
  567. {
  568. return true;
  569. }
  570. return false;
  571. }
  572. Stopwatch watch = new Stopwatch();
  573. private void DecodeImageMat(byte[] imageRgbByte, out Mat imageRGBMat)
  574. {
  575. MatOfByte matOfByte = new MatOfByte(imageRgbByte);
  576. Mat yuv;
  577. // using (ProTimer p = new ProTimer("Imgcodecs.imdecode"))
  578. {
  579. //p._runTime
  580. watch.Start();
  581. yuv = Imgcodecs.imdecode(matOfByte, -1);
  582. watch.Stop();
  583. _decodeTime = watch.ElapsedMilliseconds;
  584. watch.Reset();
  585. }
  586. imageRGBMat = new Mat(yuv.rows(), yuv.cols(), CvType.CV_8UC3);
  587. Imgproc.cvtColor(yuv, imageRGBMat, Imgproc.COLOR_BGR2RGB);
  588. }
  589. }
  590. }