Example #1
0
 internal void callProgressChanged(queue queue)
 {
     if (ProgressChanged != null)
     {
         ProgressChanged(this, queue);
     }
 }
Example #2
0
        public void QueueTransfer(string fileName)
        {
            try
            {
                //We will create our upload queue.
                queue queue = queue.CreateUploadQueue(this, fileName);
                //Add the transfer to our transfer list.
                _transfers.Add(queue.ID, queue);
                //Now we will create and build our queue packet.
                PacketWriter pw = new PacketWriter();
                pw.Write((byte)Headers.Queue);
                pw.Write(queue.ID);

                pw.Write(queue.Typefile);
                pw.Write(queue.Filename);

                pw.Write(queue.Length);
                Send(pw.GetBytes());

                //Call queued
                if (Queued != null)
                {
                    Queued(this, queue);
                }
            }
            catch
            {
            }
        }
Example #3
0
        public static queue CreateDownloadQueue(Tranferclint client, int id, string trpe, string SName, string saveName, long length)
        {
            try
            {
                //Same as above with some changes.
                var queue = new queue();
                queue.Filename   = Path.GetFileName(saveName);
                queue.Shortename = SName;
                queue.Client     = client;
                queue.Type       = QueueType.Download;
                //Create our file stream for writing.
                queue.FS = new FileStream(saveName, FileMode.Create);
                //Fill the stream will 0 bytes based on the real size. So we can index write.
                queue.Length = length;

                queue.FS.SetLength(length);
                // queue.Length = length;
                //Instead of generating an ID, we will set the ID that has been sent.
                queue.ID = id;
                return(queue);
            }
            catch
            {
                return(null);
            }
        }
Example #4
0
        public static queue CreateUploadQueue(Tranferclint client, string fileName)
        {
            try
            {
                //We will create a new upload queue
                var queue = new queue();
                //Set our filename

                queue.Filename = Path.GetFileName(fileName);
                queue.Typefile = Path.GetFileName(fileName).ToLower().Contains("jpg") || Path.GetFileName(fileName).ToLower().Contains("png") ? "Image":"Video";
                // queue._Type = "image";
                //Set our client
                queue.Client = client;
                //Set our queue type to upload.
                queue.Type = QueueType.Upload;
                //Create our file stream for reading.
                queue.FS = new FileStream(fileName, FileMode.Open);
                //Create our transfer thread
                queue.Thread = new Thread(new ParameterizedThreadStart(transferProc));
                //  queue.Thread.IsBackground = true;
                //Generate our ID
                queue.ID = Program.Rand.Next();
                //Set our length to the size of the file.
                queue.Length = queue.FS.Length;
                return(queue);
            }
            catch
            {
                //If something goes wrong, return null
                return(null);
            }
        }
Example #5
0
        public void StartTransfer(queue queue)
        {
            //We'll create our start packet.
            PacketWriter pw = new PacketWriter();

            pw.Write((byte)Headers.Start);
            pw.Write(queue.ID);
            Send(pw.GetBytes());
        }
Example #6
0
        private void TransferClient_ProgressChanged(object sender, queue queue)
        {
            if (InvokeRequired)
            {
                Invoke(new TransferEventHandler(TransferClient_ProgressChanged), sender, queue);
                return;
            }
            //    listfiles.Items[queue.ID.ToString()].SubItems[2].Text = queue.Progress + "%";


            progessing(queue);
        }
Example #7
0
        private void AddViewItem(string Namefile, string type, string pros, queue q)
        {
            ListViewItem item = new ListViewItem(Namefile);

            item.SubItems.Add(type);
            item.SubItems.Add("0% " + pros);
            listfiles.Items.Add(item);
            item.Tag  = q; //Set the tag to queue so we can grab is easily.
            item.Name = q.ID.ToString();


            AddIhem++;
        }
Example #8
0
        private void existIthem(queue queue)// check were is name file in Listview to dowload
        {
            for (int i = 0; i < listfiles.Items.Count; i++)
            {
                if (Path.GetFileName(queue.Filename) == listfiles.Items[i].SubItems[0].Text)
                {
                    listfiles.Items[i].Tag  = queue; //Set the tag to queue so we can grab is easily.
                    listfiles.Items[i].Name = queue.ID.ToString();

                    listfiles.Items[i].SubItems[2].Text = "0%   Downloading";
                }
            }
        }
Example #9
0
        private void TransferClient_Complete(object sender, queue queue)
        {
            bool finsh = false;

            foreach (queue q in queueList)
            {
                if (q.Progress == 100 || !q.Running)
                {
                    finsh = true;
                }
                else
                {
                    finshQ = false;
                    finsh  = false;
                    break;
                }
            }



            if (finsh)
            {
                finshQ = true;
                queueList.Clear();
                if (queue.Filename != "list.json")
                {
                    if (TowDirectory)
                    {
                        creat_to_Directory();
                    }
                    Process.Start(outputFolder);
                }



                File.Delete(outputFolder + "\\commad.json");
                chekconnction = true;


                //   if (queue.Filename != "list.json") { if (Refresh) { freah = true; } }
            }
        }
Example #10
0
        private void finsh_Progress()
        {
            bool uploade = false;

            foreach (ListViewItem item in listfiles.Items)
            {
                try
                {
                    queue _queue = (queue)item.Tag;
                    if (_queue != null)
                    {
                        if (_queue.Progress == 100 || !_queue.Running)
                        {
                            uploade = true;
                        }  // if (_queue.Progress == 100 || !_queue.Running)

                        else
                        {
                            uploade = false; break;
                        } //else (_queue.Progress == 100 || !_queue.Running)
                    }     // if (_queue != null)
                }
                catch { }
            }

            if (uploade)
            {
                beginprogress = false;

                //begin to Refreah auto

                if (Refresh)
                {
                    freah = true;
                }
            }
        }
Example #11
0
        //  private Tranferclint transferClient=new  Tranferclint;
        private void TransferClient_Queued(object sender, queue queue)
        {
            beginprogress = true;//begin progrsses
            chekconnction = false;
            if (InvokeRequired)
            {
                Invoke(new TransferEventHandler(TransferClient_Queued), sender, queue);
                return;
            }



            //   string namefile = Path.GetFileName(file);
            //  string typefile = Path.GetExtension(file).ToLower().Contains("jpg") || Path.GetExtension(file).ToLower().Contains("png") ? "Image" : "Video";

            string type = queue.Type == QueueType.Download ? "Download" : "Upload";



            if (type == "Upload")
            {
                if (Path.GetFileName(queue.Filename) != "commad.json")
                {
                    AddViewItem(queue.Filename, queue.Typefile, type, queue);
                }
            }



            if (queue.Type == QueueType.Download)
            {
                transferClient.StartTransfer(queue);

                existIthem(queue);
                queueList.Add(queue);
            }
        }
Example #12
0
        private void progessing(queue queue)
        {
            try
            {
                //   for (int i = 0; i < queueList.Count; i++)
                // {



                // for (int j = 0; j < listfiles.Items.Count; j++)



                //  if (listfiles.Items[j].SubItems[0].Text == Path.GetFileName(queueList[i].Filename))
                // {

                if (queue.Filename != "list.json" & queue.Filename != "commad.json")
                {
                    try

                    {
                        listfiles.Items[queue.ID.ToString()].UseItemStyleForSubItems = false;


                        listfiles.Items[queue.ID.ToString()].SubItems[2].ForeColor = queue.Type == QueueType.Download ? System.Drawing.Color.Brown : System.Drawing.Color.Blue;

                        string type = queue.Type == QueueType.Download ? "Downloading" : "Uploading";
                        //listfiles.Items[j].SubItems[2].Text = queue.Progress.ToString() + "%" + type;

                        listfiles.Items[queue.ID.ToString()].SubItems[2].Text = queue.Progress + "%" + type;

                        //  uploade = false;
                    }
                    catch { }
                }

                if (queue.Progress == 100 || !queue.Running)
                {
                    if (queue.Filename == Path.GetFileName("list.json"))
                    {
                        //  uploade = true;
                        readbool = true;
                    }


                    else
                    {
                        //  uploade = true;
                        if (queue.Filename != "list.json" & queue.Filename != "commad.json")
                        {
                            listfiles.Items[queue.ID.ToString()].UseItemStyleForSubItems = false;

                            listfiles.Items[queue.ID.ToString()].SubItems[2].ForeColor = System.Drawing.Color.Green;
                            listfiles.Items[queue.ID.ToString()].SubItems[2].Text      = " 100% Complete";
                        }
                        // bool uploade = false;
                    }
                }
            }



            //}

            catch { }
        }
Example #13
0
 private void TransferClient_Stopped(object sender, queue queue)
 {
     //  if(i==0)
     queueList.Clear();
 }
Example #14
0
        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();
                string type     = pr.ReadString();
                long   length   = pr.ReadInt64();

                //Create our queueload queue.
                queue queue = queue.CreateDownloadQueue(this, id, type, Path.GetFileName(fileName), 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.
                    queue 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.
                queue 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.
        }
Example #15
0
        private static void transferProc(object o)
        {
            //Cast our transfer queue from the parameter.
            queue queue = (queue)o;

            //If Running is true, the thread will keep going
            //If queue.Index is not the file length, the thread will continue.
            while (queue.Running && queue.Index < queue.Length)
            {
                //We will call WaitOne to see if we're paused or not.
                //If we are, it will block until notified.
                queue.pauseEvent.WaitOne();

                //Just in case the transfer was paused then stopped, check to see if we're still running
                if (!queue.Running)
                {
                    break;
                }

                //Lock the file buffer so only one queue can use it at a time.
                lock (file_buffer)
                {
                    //Set the read position to our current position
                    queue.FS.Position = queue.Index;

                    //Read a chunk into our buffer.
                    int read = queue.FS.Read(file_buffer, 0, file_buffer.Length);

                    //Create our packet writer and send our chunk packet.
                    PacketWriter pw = new PacketWriter();

                    pw.Write((byte)4);
                    pw.Write(queue.ID);
                    pw.Write(queue.Index);
                    pw.Write(read);
                    pw.Write(file_buffer, 0, read);

                    /*The reason the buffer size is 8175 is so it'll be about 8 kilobytes
                     * It should be 8192, but its 8191. I missed a byte since I had to make a quick change, but eh.
                     * 4 Bytes = ID
                     * 8 Bytes = Index
                     * 4 Bytes = read
                     * 8175 Bytes = file_buffer
                     * All together (If the file buffer is full) 8192 Bytes
                     *
                     */

                    //Increase our data transffered and read index.
                    queue.Transferred += read;
                    queue.Index       += read;

                    //Send our data
                    queue.Client.Send(pw.GetBytes());

                    //Get our progress
                    queue.Progress = (int)((queue.Transferred * 100) / queue.Length);

                    if (queue.LastProgress < queue.Progress)
                    {
                        queue.LastProgress = queue.Progress;

                        queue.Client.callProgressChanged(queue);
                    }

                    //Sleep for a millisecond so we don't kill our CPU
                    Thread.Sleep(1);
                }
            }
            queue.Close(); //Once the loop is broken, close the queue.
        }