123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641 |
-
- using CScript.App;
- using CScript.Entity;
- using CScript.Utilities;
- using OpenCVForUnity.CoreModule;
- using OpenCVForUnity.ImgcodecsModule;
- using OpenCVForUnity.ImgprocModule;
- using OpenCVForUnity.UnityUtils;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- namespace CScript.Net
- {
- public struct PackageCheck
- {
- public string frameSymbols;
- public int frameLenght;
- public byte frameLastByteValue;
- }
-
-
-
-
- public class PackageHandler : PackageHandler<object>
- {
- public PackageHandler(object sender) : base(sender)
- {
- }
- }
-
-
-
-
-
- public class PackageHandler<T>
- {
-
- private MemoryStream stream = new MemoryStream(4096000);
- private int readOffset = 0;
- private T sender;
- NetMeshData netMeshData;
- private List<short> _shortBufferList;
- private List<byte> _byteBufferList;
-
- int totalByteCount;
- int lostCount;
- private MemoryStream _streamDataBuffer = new MemoryStream(409600 * 10);
-
- private bool _start = false;
- string packageTypeStr = string.Empty;
- Mat rgbMat;
- private float _decodeTime;
-
- public PackageHandler(T sender)
- {
- this.sender = sender;
- netMeshData = new NetMeshData();
- _shortBufferList = new List<short>();
- _byteBufferList = new List<byte>();
- totalByteCount = 0;
- lostCount = 0;
-
-
- }
-
-
-
-
-
-
- public void ReceiveData(byte[] data, int offset, int count)
- {
-
-
- if (stream.Position + count > stream.Capacity)
- {
- throw new Exception("PackageHandler write buffer overflow");
- }
- stream.Write(data, offset, count);
- ParsePackageTCP();
-
- }
- public void ReceiveQuicData(byte[] data, int offset, int count)
- {
- if (_streamDataBuffer.Position + count > _streamDataBuffer.Capacity)
- {
- throw new Exception("PackageHandler write buffer overflow");
- }
- _streamDataBuffer.Write(data, offset, count);
- totalByteCount += count;
- netMeshData.Clear();
-
- UnityEngine.Debug.LogWarning("_streamDataBuffer.len:"+_streamDataBuffer.Length);
- bool isSuccess = UnPack2();
- UnityEngine.Debug.LogWarning("lostCount: " + lostCount + "isSuccess:" + isSuccess);
- UnityEngine.Debug.LogWarning("netMeshData:"+netMeshData);
- if (isSuccess)
- {
- NetDistribute.Instance.Parsing(CScript.Utilities.AppUtil.Clone<NetMeshData>(netMeshData), rgbMat.clone(), totalByteCount, _decodeTime, lostCount);
- }
- netMeshData.Clear();
- _streamDataBuffer.Position = 0;
- _streamDataBuffer.SetLength(0);
- totalByteCount = 0;
- }
- public void Reset()
- {
- readOffset = 0;
- i = 0;
- stream.Position = 0;
- stream.SetLength(0);
- _streamDataBuffer.Position = 0;
- _streamDataBuffer.SetLength(0);
- _start = false;
- }
-
-
-
-
-
- public static byte[] PackMessage()
- {
- byte[] package = null;
- using (MemoryStream ms = new MemoryStream())
- {
-
- package = new byte[ms.Length + 4];
- Buffer.BlockCopy(BitConverter.GetBytes(ms.Length), 0, package, 0, 4);
- Buffer.BlockCopy(ms.GetBuffer(), 0, package, 4, (int)ms.Length);
- }
- return package;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- int i;
- private int _frameIndex;
- bool ParsePackageTCP()
- {
- if (!_start)
- {
- while (readOffset + 20 < stream.Position)
- {
- packageTypeStr = System.Text.Encoding.ASCII.GetString(stream.GetBuffer(), readOffset + 0, 1);
- packageTypeStr += stream.GetBuffer()[readOffset + 1].ToString();
- packageTypeStr += System.Text.Encoding.ASCII.GetString(stream.GetBuffer(), readOffset + 2, 1);
- packageTypeStr += System.Text.Encoding.ASCII.GetString(stream.GetBuffer(), readOffset + 3, 1);
-
- if (packageTypeStr == "$1@@")
- {
- _start = true;
- break;
- }
- else
- {
- readOffset+=1;
- }
- }
-
- }
- else
- {
- if (readOffset + 20 < stream.Position)
- {
- int packageSize = BitConverter.ToInt32(stream.GetBuffer(), readOffset + 16);
- if (packageSize + readOffset + 20 <= stream.Position)
- {
- _start = false;
- _streamDataBuffer.Position = 0;
- _streamDataBuffer.SetLength(0);
- _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 20, packageSize);
- netMeshData.Clear();
- UnPack2();
- NetDistribute.Instance.Parsing(CScript.Utilities.AppUtil.Clone<NetMeshData>(netMeshData), rgbMat.clone(), totalByteCount, _decodeTime, 0);
- netMeshData.Clear();
- totalByteCount = 0;
- _frameIndex = 0;
- this.readOffset += (packageSize + 20);
- return ParsePackageTCP();
- }
- }
- }
-
- if (this.readOffset > 0)
- {
- long size = stream.Position - this.readOffset;
- if (this.readOffset < stream.Position)
- {
- Array.Copy(stream.GetBuffer(), this.readOffset, stream.GetBuffer(), 0, stream.Position - this.readOffset);
- }
-
- this.readOffset = 0;
- stream.Position = size;
- stream.SetLength(size);
-
- netMeshData.Clear();
- }
- return true;
- }
-
-
-
-
- bool ParsePackage()
- {
- if (!_start)
- {
- while (readOffset + 13 < stream.Position)
- {
- packageTypeStr = System.Text.Encoding.UTF8.GetString(stream.GetBuffer(), readOffset + 0, 1);
- if (packageTypeStr == "#")
- {
- _start = true;
- break;
- }
- else
- {
- readOffset++;
- }
- }
- }
- if (_start)
- {
-
- if (readOffset + 13 < stream.Position)
- {
- packageTypeStr = System.Text.Encoding.UTF8.GetString(stream.GetBuffer(), readOffset + 0, 1);
- int packageSize = BitConverter.ToInt32(stream.GetBuffer(), readOffset + 9);
-
- if (packageSize + readOffset + 13 <= stream.Position)
- {
- if (packageTypeStr == "#")
- {
-
- _frameIndex = 0;
-
- _streamDataBuffer.Position = 0;
- _streamDataBuffer.SetLength(0);
-
- _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 13, packageSize);
- }
- else if (packageTypeStr == "@")
- {
-
- _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 13, packageSize);
- }
- else if (packageTypeStr == "$")
- {
- _start = false;
- _streamDataBuffer.Write(stream.GetBuffer(), readOffset + 13, packageSize);
-
- i++;
- netMeshData.Clear();
- if (AppConfig.isNewMeshData)
- {
- UnPack2();
- }
- else
- {
- UnPack1();
- }
- NetDistribute.Instance.Parsing(CScript.Utilities.AppUtil.Clone<NetMeshData>(netMeshData), rgbMat.clone(), totalByteCount, _decodeTime,0);
- netMeshData.Clear();
- totalByteCount = 0;
- _frameIndex = 0;
- }
- this.readOffset += (packageSize + 13);
- return ParsePackage();
- }
- }
- }
-
- if (this.readOffset > 0)
- {
- long size = stream.Position - this.readOffset;
- if (this.readOffset < stream.Position)
- {
- Array.Copy(stream.GetBuffer(), this.readOffset, stream.GetBuffer(), 0, stream.Position - this.readOffset);
- }
-
- this.readOffset = 0;
- stream.Position = size;
- stream.SetLength(size);
-
- netMeshData.Clear();
- }
- return true;
- }
- private void UnPack1()
- {
- int bufferOffset = 0;
-
-
- ulong totalLenght = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
- bufferOffset += sizeof(UInt64);
-
- uint rgbSize = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset) * 6;
- bufferOffset += sizeof(UInt32);
- _shortBufferList.Clear();
- _shortBufferList.TrimExcess();
- _shortBufferList.Capacity = (int)rgbSize;
-
- for (int i = 0; i < rgbSize; i++)
- {
- _shortBufferList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
- bufferOffset += sizeof(Int16);
- }
- uint vertexCount = rgbSize / 6;
- netMeshData.netSideVertexList.Capacity = (int)vertexCount * 3;
- netMeshData.netColorList.Capacity = (int)vertexCount * 3;
-
- for (int i = 0; i < vertexCount; i++)
- {
- netMeshData.netSideVertexList.Add(_shortBufferList[i * 6 + 0]);
- netMeshData.netSideVertexList.Add(_shortBufferList[i * 6 + 1]);
- netMeshData.netSideVertexList.Add(_shortBufferList[i * 6 + 2]);
- netMeshData.netColorList.Add(_shortBufferList[i * 6 + 3]);
- netMeshData.netColorList.Add(_shortBufferList[i * 6 + 4]);
- netMeshData.netColorList.Add(_shortBufferList[i * 6 + 5]);
- }
-
- UInt64 indices_len_rgb = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
- bufferOffset += sizeof(UInt64);
- if (indices_len_rgb > Int32.MaxValue)
- {
-
- UnityEngine.Debug.Log($"totalLenght:{totalLenght} rgbSize:{rgbSize} indices_len_rgb:{indices_len_rgb}");
- }
- netMeshData.netSideTriangles.Capacity = (int)indices_len_rgb;
- for (ulong i = 0; i < indices_len_rgb; i++)
- {
- netMeshData.netSideTriangles.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
- bufferOffset += sizeof(Int16);
- }
- int ys_width_len = BitConverter.ToInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
- bufferOffset += sizeof(Int32);
- int ys_height_len = BitConverter.ToInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
- bufferOffset += sizeof(Int32);
- netMeshData.width = ys_width_len;
- netMeshData.height = ys_height_len;
-
- UInt32 vertexes_len = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
- if (vertexes_len > Int32.MaxValue)
- {
- UnityEngine.Debug.Log(" vertexes_len*5:" + vertexes_len * 5);
- }
- UInt32 xyzuvs_size = vertexes_len * 5;
- bufferOffset += sizeof(UInt32);
- _shortBufferList.Clear();
- _shortBufferList.TrimExcess();
- _shortBufferList.Capacity = (int)xyzuvs_size;
- for (int i = 0; i < xyzuvs_size; i++)
- {
- _shortBufferList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
- bufferOffset += sizeof(Int16);
- }
- netMeshData.netUvList.Capacity = (int)vertexes_len * 2;
- netMeshData.netFrontVertexList.Capacity = (int)vertexes_len * 3;
- vertexCount = xyzuvs_size / 5;
-
- for (int i = 0; i < vertexCount; i++)
- {
- netMeshData.netFrontVertexList.Add(_shortBufferList[i * 5 + 0]);
- netMeshData.netFrontVertexList.Add(_shortBufferList[i * 5 + 1]);
- netMeshData.netFrontVertexList.Add(_shortBufferList[i * 5 + 2]);
- netMeshData.netUvList.Add(_shortBufferList[i * 5 + 3]);
- netMeshData.netUvList.Add(_shortBufferList[i * 5 + 4]);
- }
- _shortBufferList.Clear();
- _shortBufferList.TrimExcess();
- UInt64 indices_len = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
- bufferOffset += sizeof(UInt64);
-
- netMeshData.netFrontTriangles.Capacity = (int)indices_len;
- for (ulong i = 0; i < indices_len; i++)
- {
- netMeshData.netFrontTriangles.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
- bufferOffset += sizeof(ushort);
- }
-
- int imageYEncoderSize = BitConverter.ToInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
- bufferOffset += sizeof(int);
-
- _byteBufferList.Clear();
- _byteBufferList.Capacity = imageYEncoderSize;
-
- for (int i = 0; i < imageYEncoderSize; i++)
- {
- _byteBufferList.Add(_streamDataBuffer.GetBuffer()[bufferOffset++]);
- }
- DecodeImageMat(_byteBufferList.ToArray(),out rgbMat);
- }
-
- private bool UnPack2()
- {
- int bufferOffset = 0;
-
- ulong totalLenght = BitConverter.ToUInt64(_streamDataBuffer.GetBuffer(), bufferOffset);
- bufferOffset += sizeof(UInt64);
- int realCount = 0;
-
- uint atlasXyzBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
- if (atlasXyzBufferLenght > 1000000)
- {
- lostCount++;
- return false;
- }
- bufferOffset += sizeof(UInt32);
- realCount = (int)(atlasXyzBufferLenght / sizeof(Int16));
-
- netMeshData.netAtlasVertexList.Capacity = (int)realCount;
- for (int i = 0; i < realCount; i++)
- {
- netMeshData.netAtlasVertexList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
- bufferOffset += sizeof(Int16);
- }
-
- uint atlasUvBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
- if (atlasUvBufferLenght > 1000000)
- {
- lostCount++;
- return false;
- }
- bufferOffset += sizeof(UInt32);
- realCount = (int)(atlasUvBufferLenght / sizeof(UInt16));
-
- netMeshData.netAtlasUVList.Capacity = (int)realCount;
- for (int i = 0; i < realCount; i++)
- {
-
- netMeshData.netAtlasUVList.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
- bufferOffset += sizeof(UInt16);
- }
-
- uint atlasIndicesBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
- if (atlasIndicesBufferLenght > 1000000)
- {
- lostCount++;
- return false;
- }
- bufferOffset += sizeof(UInt32);
- realCount = (int)(atlasIndicesBufferLenght / sizeof(UInt16));
-
- netMeshData.netTrianglesList.Capacity = (int)realCount;
- for (int i = 0; i < realCount; i++)
- {
- netMeshData.netTrianglesList.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
- bufferOffset += sizeof(UInt16);
- }
-
- uint atlasImageBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
- if (atlasImageBufferLenght > 1000000)
- {
- lostCount++;
- return false;
- }
- bufferOffset += sizeof(UInt32);
-
- _byteBufferList.Clear();
- realCount = (int)(atlasImageBufferLenght / sizeof(byte));
-
- _byteBufferList.Capacity = (int)realCount;
-
- for (int i = 0; i < realCount; i++)
- {
- _byteBufferList.Add(_streamDataBuffer.GetBuffer()[bufferOffset++]);
- }
- DecodeImageMat(_byteBufferList.ToArray(), out rgbMat);
-
- uint noseXyzrgbBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
- if (noseXyzrgbBufferLenght > 1000000)
- {
- lostCount++;
- return false;
- }
- bufferOffset += sizeof(UInt32);
- realCount = (int)(noseXyzrgbBufferLenght / sizeof(short));
- _shortBufferList.Clear();
- _shortBufferList.TrimExcess();
-
- _shortBufferList.Capacity = (int)realCount;
-
- for (int i = 0; i < realCount; i++)
- {
- _shortBufferList.Add(BitConverter.ToInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
- bufferOffset += sizeof(Int16);
- }
- uint vertexCount = (uint)realCount / 6;
- netMeshData.netSideVertexList.Capacity = (int)vertexCount * 3;
- netMeshData.netColorList.Capacity = (int)vertexCount * 3;
-
- for (int i = 0; i < vertexCount; i++)
- {
- netMeshData.netNoseVertexList.Add(_shortBufferList[i * 6 + 0]);
- netMeshData.netNoseVertexList.Add(_shortBufferList[i * 6 + 1]);
- netMeshData.netNoseVertexList.Add(_shortBufferList[i * 6 + 2]);
- netMeshData.netNoseColorList.Add(_shortBufferList[i * 6 + 3]);
- netMeshData.netNoseColorList.Add(_shortBufferList[i * 6 + 4]);
- netMeshData.netNoseColorList.Add(_shortBufferList[i * 6 + 5]);
- }
-
- uint noseIndicesBufferLenght = BitConverter.ToUInt32(_streamDataBuffer.GetBuffer(), bufferOffset);
- if (noseIndicesBufferLenght > 1000000)
- {
- lostCount++;
- return false;
- }
- bufferOffset += sizeof(UInt32);
- realCount = (int)(noseIndicesBufferLenght / sizeof(ushort));
-
- netMeshData.netNoseTriangles.Capacity = (int)realCount;
- for (int i = 0; i < realCount; i++)
- {
- netMeshData.netNoseTriangles.Add(BitConverter.ToUInt16(_streamDataBuffer.GetBuffer(), bufferOffset));
- bufferOffset += sizeof(UInt16);
- }
- return true;
- }
- private bool CheckSum(Dictionary<int, PackageCheck> packageCheckDic, ulong checksums)
- {
- UInt64 checkNumLocak = 0;
- packageCheckDic.Values.ToList().ForEach(pcidValue => { checkNumLocak += (ulong)pcidValue.frameLenght; checkNumLocak += (ulong)pcidValue.frameLastByteValue; });
-
- if (checkNumLocak == checksums)
- {
-
- return true;
- }
- return false;
- }
- Stopwatch watch = new Stopwatch();
- private void DecodeImageMat(byte[] imageRgbByte, out Mat imageRGBMat)
- {
- MatOfByte matOfByte = new MatOfByte(imageRgbByte);
- Mat yuv;
-
- {
-
- watch.Start();
- yuv = Imgcodecs.imdecode(matOfByte, -1);
- watch.Stop();
- _decodeTime = watch.ElapsedMilliseconds;
- watch.Reset();
- }
- imageRGBMat = new Mat(yuv.rows(), yuv.cols(), CvType.CV_8UC3);
- Imgproc.cvtColor(yuv, imageRGBMat, Imgproc.COLOR_BGR2RGB);
- }
- }
- }
|