Rover.PacketPartitionner.PartitionFile C# (CSharp) Method

PartitionFile() public method

Partition a file in packets and adds the packet to the arraylist.
public PartitionFile ( byte Data, int FileSize ) : void
Data byte
FileSize int
return void
        public void PartitionFile(byte[] Data, int FileSize)
        {
            //Clear previous packets in the arraylist
            PartitionnedPackets.Clear();

            //Calculate the number of full packets
            int NbFullPkt = FileSize / Packet.DEFAULT_PACKET_SIZE;
            int NbRemainingPkt = FileSize % Packet.DEFAULT_PACKET_SIZE;
            int TotalNbOfPackets;

            if(NbRemainingPkt > 0)
            {
                TotalNbOfPackets = NbFullPkt + 1;
            }
            else
            {
                TotalNbOfPackets = NbFullPkt;
            }

            for (int i = 0; i < NbFullPkt; i++)
            {
                byte[] d = new byte[Packet.DEFAULT_PACKET_SIZE];

                Buffer.BlockCopy(Data, i * Packet.DEFAULT_PACKET_SIZE, d, 0, Packet.DEFAULT_PACKET_SIZE);

                Packet p = new Packet(FileID, FileSize, i, TotalNbOfPackets, Packet.DEFAULT_PACKET_SIZE, d);

                PartitionnedPackets.Add(p);

                d = null;
            }

            if (NbRemainingPkt > 0)
            {
                byte[] d = new byte[Packet.DEFAULT_PACKET_SIZE];

                Buffer.BlockCopy(Data, NbFullPkt * Packet.DEFAULT_PACKET_SIZE, d, 0, NbRemainingPkt);

                Packet p = new Packet(FileID, FileSize, TotalNbOfPackets-1, TotalNbOfPackets, NbRemainingPkt, d);

                PartitionnedPackets.Add(p);

                d = null;
            }

            if (PacketPartitionnerCBHandler != null)
            {
                PacketPartitionnerCBHandler(PartitionnedPackets);
            }
        }

Usage Example

Ejemplo n.º 1
0
        public void BitmapAcquiredCBHandler(Bitmap aNewBitmap)
        {
            stopwatch.Restart();

            //Console.WriteLine("BitmapAcquiredCBHandler:");
            //Console.WriteLine("From CameraId " + GetID());
            byte[] newBA = aCodecUtility.CompressBmpToJPEGArray(aNewBitmap);
            //byte[] newBA = (byte[])converter.ConvertTo(aNewBitmap, typeof(byte[]));

            //Total size of the packet to send including header  +  data.
            PacketPartitionner pp = new PacketPartitionner(GetID(), newBA.Length, Packet.GetHeaderSize() + Packet.DEFAULT_PACKET_SIZE, null);

            pp.PartitionFile(newBA, newBA.Length);

            //Console.WriteLine("Bitmap of size:" + newBA.Length + ", Splitted in:" + pp.GetPartitionnedPackets().Count + "Packets of " + (Packet.GetHeaderSize() + Packet.DEFAULT_PACKET_SIZE) + " Bytes");

            //Remove prints to the console
            //This is a critical section, we don't to have more than 1 camera sending data at the same time.
            for (int i = 0; i < pp.GetPartitionnedPackets().Count; i++)
            {
                //Console.WriteLine("Send packet number:" + i + "/" + (pp.GetPartitionnedPackets().Count - 1));

                //((Packet)pp.GetPartitionnedPackets()[i]).PrintInfo();

                Packet p = ((Packet)(pp.GetPartitionnedPackets()[i]));

                byte[] SerializedPacket = p.GetBytes();

                //aUDPSender.SendDataUDP(SerializedPacket, SerializedPacket.Length);
                aUDPSender.SendNow(SerializedPacket, SerializedPacket.Length);

                Thread.Sleep(10);
            }

            //Thread.Sleep(100);

            //clear the array list so you dont send alway the same image.
            //pp.GetPartitionnedPackets().Clear();

            stopwatch.Stop();
            // Write result
            //Console.WriteLine("Time elapsed ms: {0}", stopwatch.ElapsedMilliseconds);
        }
All Usage Examples Of Rover.PacketPartitionner::PartitionFile