Exemple #1
0
        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);
        }
Exemple #2
0
        /// <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);
            }
        }