internal static bool TryBuild(CitpRecieveData data, out CitpPacket packet) { CitpHeader header = new CitpHeader(string.Empty); packet = header; try { //We have read all the data. if (data.EndOfData()) { data.Reset(); return(false); } //Check we have enough data to construct the header. if (data.Length - data.ReadPosition < CitpHeader.PacketSize) { return(false); } //Read the packet header. header.ReadData(data.GetReader()); //Ensure the header packet is valid if (!header.IsValid()) { //Purge data as it is probably corrupted. data.Reset(); //Reset position to start so we dump the data. return(false); } //Read the sub packet switch (header.ContentType) { case CitpPInfHeader.PacketType: packet = BuildPInf(data); break; case SDmxHeader.PacketType: packet = BuildSDmx(data); break; case FPtcHeader.PacketType: packet = BuildFPtc(data); break; case FSelHeader.PacketType: packet = BuildFSel(data); break; case CitpMsexHeader.PacketType: packet = BuildMsex(data); break; case CaExHeader.PacketType: packet = BuildCaEx(data); break; case FInfHeader.PacketType: packet = BuildFInf(data); break; default: packet = null; break; } //Advance the read and write pointers past the successfully read packet. data.ReadPosition += header.MessageSize; } catch (EndOfStreamException) { return(false); } //We have managed to read the packet even if it was unknown and set to null so lets return true return(true); }
internal static bool TryBuild(CitpReceiveData data, out CitpPacket packet) { CitpHeader header = new CitpHeader(string.Empty); packet = header; try { //We have read all the data. if (data.EndOfData()) { data.Reset(); return(false); } //Check we have enough data to construct the header. if (data.Length - data.ReadPosition < CitpHeader.PacketSize) { return(false); } //Read the packet header. header.ReadData(data.GetReader()); //Ensure the header packet is valid if (!header.IsValid()) { //Purge data as it is probably corrupted. data.Reset(); //Reset position to start so we dump the data. return(false); } //Read the sub packet switch (header.ContentType) { case CitpPInfHeader.PacketType: packet = BuildPInf(data); break; case SDmxHeader.PacketType: packet = BuildSDmx(data); break; case FPtcHeader.PacketType: packet = BuildFPtc(data); break; case FSelHeader.PacketType: packet = BuildFSel(data); break; case CitpMsexHeader.PacketType: packet = BuildMsex(data); break; case CaExHeader.PacketType: packet = BuildCaEx(data); break; default: return(false); } } catch (EndOfStreamException) { return(false); } return(packet != null); }