/// <summary> /// 写入数据,通过返回值可以判断是否符合解包条件 /// </summary> /// <param name="data">数据</param> /// <param name="ex">异常</param> /// <returns>解包后的数据,如果返回null则代表没有解包</returns> public object Input(byte[] data, out Exception ex) { ex = null; if (stream.Position != stream.Length) { stream.Seek(stream.Length, SeekOrigin.Begin); } stream.Write(data, 0, data.Length); if (packetLength <= 0) { var receiveData = new byte[stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(receiveData, 0, receiveData.Length); packetLength = Math.Max(packer.Input(receiveData, out ex), 0); if (ex != null) { return(null); } } return(packetLength < stream.Length ? null : Decode(out ex)); }
/// <summary> /// 写入数据,通过返回值可以判断是否符合解包条件 /// </summary> /// <param name="data">数据</param> /// <param name="ex">异常</param> /// <returns>解包后的数据,如果返回null则代表没有解包</returns> public object[] Input(byte[] data, out Exception ex) { packet.Clear(); ex = null; if (stream.Position != stream.Length) { stream.Seek(stream.Length, SeekOrigin.Begin); } if (data != null && data.Length > 0) { stream.Write(data, 0, data.Length); } while (stream.Length > 0) { if (packetLength <= 0) { var receiveData = new byte[stream.Length]; stream.Seek(0, SeekOrigin.Begin); stream.Read(receiveData, 0, receiveData.Length); packetLength = Math.Max(packer.Input(receiveData, out ex), 0); if (packetLength <= 0 || ex != null) { break; } } if (packetLength > stream.Length) { break; } packet.Enqueue(Decode(out ex)); } return(packet.Count > 0 ? packet.ToArray() : null); }