private void process() { PacketReader pr = new PacketReader(_Buffer); //Create our packet reader. Headers header = (Headers)pr.ReadByte(); //Read and cast our header. switch (header) { case Headers.Queue: { //Read the ID, Filename and length of the file (For progress) from the packet. int id = pr.ReadInt32(); string fileName = pr.ReadString(); long length = pr.ReadInt64(); //Create our download queue. TransferQueue queue = TransferQueue.CreateDownloadQueue(this, id, Path.Combine(OutputFolder, Path.GetFileName(fileName)), length); //Add it to our transfer list. Transfers.Add(id, queue); //Call queued. if (Queued != null) { Queued(this, queue); } } break; case Headers.Start: { //Read the ID int id = pr.ReadInt32(); //Start the upload. if (Transfers.ContainsKey(id)) { Transfers[id].Start(); } } break; case Headers.Stop: { //Read the ID int id = pr.ReadInt32(); if (Transfers.ContainsKey(id)) { //Get the queue. TransferQueue queue = Transfers[id]; //Stop and close the queue queue.Stop(); queue.Close(); //Call the stopped event. if (Stopped != null) { Stopped(this, queue); } //Remove the queue Transfers.Remove(id); } } break; case Headers.Pause: { int id = pr.ReadInt32(); //Pause the upload. if (Transfers.ContainsKey(id)) { Transfers[id].Pause(); } } break; case Headers.Chunk: { //Read the ID, index, size and buffer from the packet. int id = pr.ReadInt32(); long index = pr.ReadInt64(); int size = pr.ReadInt32(); byte[] buffer = pr.ReadBytes(size); //Get the queue. TransferQueue queue = Transfers[id]; //Write the newly transferred bytes to the queue based on the write index. queue.Write(buffer, index); //Get the progress of the current transfer with the formula //(AMOUNT_TRANSFERRED * 100) / COMPLETE SIZE queue.Progress = (int)((queue.Transferred * 100) / queue.Length); //This will prevent the us from calling progress changed multiple times. /* Such as * 2, 2, 2, 2, 2, 2 (Since the actual progress minus the decimals will be the same for a bit * It will be * 1, 2, 3, 4, 5, 6 * Instead*/ if (queue.LastProgress < queue.Progress) { queue.LastProgress = queue.Progress; if (ProgressChanged != null) { ProgressChanged(this, queue); } //If the transfer is complete, call the event. if (queue.Progress == 100) { queue.Close(); if (Complete != null) { Complete(this, queue); } } } } break; } pr.Dispose(); //Dispose the reader. }