public static FrameSectionPackage Deserialize(byte[] buffer, int startIndex) { FrameSectionPackage package = new FrameSectionPackage(); //Header package.FrameSequenceIndex = BitConverter.ToInt32(buffer, startIndex); package.Index = BitConverter.ToInt32(buffer, startIndex + 4); int numberOfPixelInstructions = BitConverter.ToInt32(buffer, startIndex + 8); //Content package.pixelInstructions = new List <PixelInstruction>(numberOfPixelInstructions); int pixelInstructionsStartIndex = startIndex + 12; for (int i = 0; i < numberOfPixelInstructions; i++) { package.pixelInstructions.Add(PixelInstructionProtocol.Deserialize(buffer, pixelInstructionsStartIndex + i * PixelInstructionProtocol.BYTES_NEEDED)); } if (package.pixelInstructions.Count != numberOfPixelInstructions) { throw new System.Net.ProtocolViolationException( $"Failed to deserialize FrameSectionPackage, the number of pixelInstructions incorrect (exp. {numberOfPixelInstructions}, rec. {package.pixelInstructions.Count})"); } return(package); }
public void Deserialize_bytes_CorrectlyDeserializes() { FrameSectionPackage expectedPackage = new FrameSectionPackage() { FrameSequenceIndex = 100, Index = 10, pixelInstructions = new List <PixelInstruction> { new PixelInstruction(10, 20, 30), new PixelInstruction(40, 50, 60) } }; int header_bytes_needed = sizeof(int) + sizeof(int) + sizeof(int); int pixelInstructions_bytes_needed = PixelInstructionProtocol.BYTES_NEEDED * 2; byte[] expectedBytes = new byte[header_bytes_needed + pixelInstructions_bytes_needed]; BitConverter.GetBytes(expectedPackage.FrameSequenceIndex).CopyTo(expectedBytes, 0); BitConverter.GetBytes(expectedPackage.Index).CopyTo(expectedBytes, 4); BitConverter.GetBytes(expectedPackage.pixelInstructions.Count).CopyTo(expectedBytes, 8); PixelInstructionProtocol.Serialize(expectedPackage.pixelInstructions[0], expectedBytes, 12); PixelInstructionProtocol.Serialize(expectedPackage.pixelInstructions[1], expectedBytes, 12 + PixelInstructionProtocol.BYTES_NEEDED); FrameSectionPackage package = FrameSectionProtocol.Deserialize(expectedBytes, 0); Assert.AreEqual(expectedPackage.FrameSequenceIndex, package.FrameSequenceIndex); Assert.AreEqual(expectedPackage.Index, package.Index); Assert.AreEqual(expectedPackage.NumberOfPixelInstructions, package.NumberOfPixelInstructions); Assert.AreEqual(expectedPackage.pixelInstructions[0], package.pixelInstructions[0]); Assert.AreEqual(expectedPackage.pixelInstructions[1], package.pixelInstructions[1]); }
public void Serialize_FrameSectionPackage_CorrectlySerialized() { FrameSectionPackage package = new FrameSectionPackage() { FrameSequenceIndex = 100, Index = 10, pixelInstructions = new List <PixelInstruction> { new PixelInstruction(10, 20, 30), new PixelInstruction(40, 50, 60) } }; int header_bytes_needed = sizeof(int) + sizeof(int) + sizeof(int); int pixelInstructions_bytes_needed = PixelInstructionProtocol.BYTES_NEEDED * 2; byte[] expectedBytes = new byte[header_bytes_needed + pixelInstructions_bytes_needed]; BitConverter.GetBytes(package.FrameSequenceIndex).CopyTo(expectedBytes, 0); BitConverter.GetBytes(package.Index).CopyTo(expectedBytes, 4); BitConverter.GetBytes(package.pixelInstructions.Count).CopyTo(expectedBytes, 8); PixelInstructionProtocol.Serialize(package.pixelInstructions[0], expectedBytes, 12); PixelInstructionProtocol.Serialize(package.pixelInstructions[1], expectedBytes, 12 + PixelInstructionProtocol.BYTES_NEEDED); byte[] returnBytes = new byte[FrameSectionProtocol.HEADER_BYTES_NEEDED + pixelInstructions_bytes_needed]; FrameSectionProtocol.Serialize(package, returnBytes, 0); Assert.AreEqual(expectedBytes, returnBytes); }
public const int HEADER_BYTES_NEEDED = sizeof(int) + sizeof(int) + sizeof(int); // frameSequenceIndex + Index + number of pixelinstructions; public static byte[] Serialize(FrameSectionPackage package, byte[] buffer, int startIndex) { BitConverter.GetBytes(package.FrameSequenceIndex).CopyTo(buffer, startIndex); BitConverter.GetBytes(package.Index).CopyTo(buffer, startIndex + 4); BitConverter.GetBytes(package.pixelInstructions.Count).CopyTo(buffer, startIndex + 8); int pixelInstructionsStartIndex = startIndex + 12; for (int i = 0; i < package.pixelInstructions.Count; i++) { PixelInstructionProtocol.Serialize(package.pixelInstructions[i], buffer, pixelInstructionsStartIndex + i * PixelInstructionProtocol.BYTES_NEEDED); } return(buffer); }
private static void CreateFrameSection(byte[] buffer, int bufferStartIndex, FrameWithoutDelta frame, int frameIndex, int sectionIndex, int instructionStartIndex, int numberOfInstructions) { FrameSectionPackage package = new FrameSectionPackage(); package.FrameSequenceIndex = frameIndex; package.Index = sectionIndex; package.pixelInstructions = new List <PixelInstruction>(); for (int i = instructionStartIndex; i < instructionStartIndex + numberOfInstructions; i++) { package.pixelInstructions.Add(frame[i]); } FrameSectionProtocol.Serialize(package, buffer, bufferStartIndex); }
/// <summary> /// Deserialize a package /// </summary> /// <param name="buffer"></param> /// <returns>True if the frame has been deserialized, False if not all packages have been received.</returns> public bool TryDeserialize(byte[] bytes, out FrameWithoutDelta frame) { frame = null; if (_frameSectionPackages == null) { // First package. // Read Frame header int startIndex = 0; _frameIndex = BitConverter.ToInt32(bytes, startIndex); _timeStampRelative = BitConverter.ToInt32(bytes, startIndex += 4); int itemCount = BitConverter.ToInt32(bytes, startIndex += 4); bool hasFrameSections = BitConverter.ToBoolean(bytes, startIndex += 4); startIndex += 1; if (hasFrameSections) { _frameSectionPackages = new FrameSectionPackage[itemCount]; _frameSectionsReceived = new bool[itemCount]; _frameSectionsReceived[0] = true; // The rest of the package is a FrameSection _frameSectionPackages[0] = FrameSectionProtocol.Deserialize(bytes, startIndex); return(false); } else { frame = new FrameWithoutDelta(_frameIndex, _timeStampRelative, itemCount); // The rest of the package contains PixelInstructions for (int i = 0; i < itemCount; i++) { int instructionStartIndex = startIndex + i * PixelInstructionProtocol.BYTES_NEEDED; frame[i] = PixelInstructionProtocol.Deserialize(bytes, instructionStartIndex); } return(true); } } else { // Subsequent package. Always starts with a FrameSection. FrameSectionPackage package = FrameSectionProtocol.Deserialize(bytes, 0); if (package.FrameSequenceIndex != _frameIndex) { throw new System.Net.ProtocolViolationException( $"The frameIndex of the frameSection does not match with the Frame's index. Expected :{_frameIndex}, Received: {package.FrameSequenceIndex}"); } _frameSectionPackages[package.Index] = package; _frameSectionsReceived[package.Index] = true; if (_frameSectionsReceived.All(x => x)) { // TODO add totalNumberOfPixelInstructions to frame header int totalPixelInstructions = _frameSectionPackages.Sum(x => x.NumberOfPixelInstructions); frame = new FrameWithoutDelta(_frameIndex, _timeStampRelative, totalPixelInstructions); int index = 0; for (int i = 0; i < _frameSectionPackages.Length; i++) { for (int j = 0; j < _frameSectionPackages[i].NumberOfPixelInstructions; j++) { frame[index++] = _frameSectionPackages[i].pixelInstructions[j]; } } return(true); } return(false); } }