public byte[] GetData(int ml) { if (ByteList.Count < ml) { return(null); } int res = 0; for (int i = 0; i < ml; i++) { int temp = (( int )ByteList[Current + i]) & 0xff; temp <<= i * 8; res = res + temp; } if (res <= 0) { Reset(); ByteList.Clear(); return(null); } if (res > (ByteList.Count - Current)) { return(null); } byte[] data = new byte[res]; ByteList.CopyTo(Current, data, 0, data.Length); Current += res; if (Current == ByteList.Count) { Reset(); ByteList.Clear(); } return(data); }
public bool InsertByteArray(byte[] Data, int ml, out List <byte[]> datax) { lock (locklist) { datax = new List <byte[]>(); ByteList.AddRange(Data); Interlocked.Add(ref Vlent, Data.Length); if (lengt == -1 && Vlent > ml) { int res = 0; for (int i = 0; i < ml; i++) { int temp = ((int)ByteList[current + i]) & 0xff; temp <<= i * 8; res = temp + res; } if (res > MaxSize) { Reset(); throw new Exception("数据包大于预设长度,如果你传入的数据比较大,请设置重新 maxSize 值"); } if (res <= 0) { Reset(); return(false); } Interlocked.Exchange(ref lengt, res); } if ((Vlent - current) >= lengt) { int lengx = lengt; Interlocked.Exchange(ref lengt, -1); byte[] data = new byte[lengx]; ByteList.CopyTo(current, data, 0, lengx); datax.Add(data); Interlocked.Add(ref current, lengx); recopy: if (current == ByteList.Count) { Reset(); return(true); } if (ByteList.Count - current > ml) { int res = 0; for (int i = 0; i < ml; i++) { int temp = ((int)ByteList[current + i]) & 0xff; temp <<= i * 8; res = temp + res; } if (res > MaxSize) { Reset(); throw new Exception("数据包大于预设长度,如果你传入的数据比较大,请设置重新 maxSize 值"); } if (res <= 0) { Reset(); return(true); } if (ByteList.Count - current < res) { return(true); } data = new byte[res]; ByteList.CopyTo(current, data, 0, res); datax.Add(data); Interlocked.Add(ref current, res); goto recopy; } return(true); } else { return(false); } } }