예제 #1
0
        public void QueueTransfer(string fileName)
        {
            try
            {
                //We will create our upload queue.
                TransferQueue queue = TransferQueue.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.Filename);
                pw.Write(queue.Length);
                Send(pw.GetBytes());

                //Call queued
                if (Queued != null)
                {
                    Queued(this, queue);
                }
            }
            catch
            {
            }
        }
 public static TransferQueue CreateUploadQueue(TransferClient client, string fileName)
 {
     try
     {
         //We will create a new upload queue
         var queue = new TransferQueue();
         //Set our filename
         queue.Filename = Path.GetFileName(fileName);
         //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
         rnd      = new Random();
         queue.ID = rnd.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);
     }
 }
예제 #3
0
 internal void callProgressChanged(TransferQueue queue)
 {
     if (ProgressChanged != null)
     {
         ProgressChanged(this, queue);
     }
 }
        public void QueueTransfer(string fileName)
        {
            try
            {
                //We will create our upload queue.
                TransferQueue queue = TransferQueue.CreateUploadQueue(this, fileName);
                //Add the transfer to our transfer list.
                _transfers.Add(queue.ID, queue);
                //Now we will create and build our queue packet.
#pragma warning disable CS0436 // 类型与导入类型冲突
                PacketWriter pw = new PacketWriter();
#pragma warning restore CS0436 // 类型与导入类型冲突
                pw.Write((byte)Headers.Queue);
                pw.Write(queue.ID);
                pw.Write(queue.Filename);
                pw.Write(queue.Length);
                Send(pw.GetBytes());

                //Call queued
                if (Queued != null)
                {
                    Queued(this, queue);
                }
            }
            catch(Exception e)
            {
                MessageBox.Show("文件"+fileName+"传输失败"+'\n'+e, "提示", MessageBoxButtons.YesNo);
            }
        }
예제 #5
0
        public void startTransfer(TransferQueue queue)
        {
            PacketWriter pw = new PacketWriter();

            pw.Write((byte)Headers.Start);
            pw.Write(queue.ID);
            Send(pw.GetBytes());
        }
예제 #6
0
        public void StartTransfer(TransferQueue queue)
        {
            //We'll create our start packet.
            PacketWriter pw = new PacketWriter();

            pw.Write((byte)Headers.Start);
            pw.Write(queue.ID);
            Send(pw.GetBytes());
        }
        public void StartTransfer(TransferQueue queue)
        {
            //We'll create our start packet.
#pragma warning disable CS0436 // 类型与导入类型冲突
            PacketWriter pw = new PacketWriter();
#pragma warning restore CS0436 // 类型与导入类型冲突
            pw.Write((byte)Headers.Start);
            pw.Write(queue.ID);
            Send(pw.GetBytes());
        }
예제 #8
0
        public void PauseTransfer(TransferQueue queue)
        {
            //Pause the queue.
            //This doesn't have to be done for the downloading queue, but its here for a reason.
            queue.Pause();

            PacketWriter pw = new PacketWriter();

            pw.Write((byte)Headers.Pause);
            pw.Write(queue.ID);
            Send(pw.GetBytes());
        }
예제 #9
0
        public void pauseTransfer(TransferQueue queue)
        {
            if (queue.Type == QueueType.Upload)
            {
                queue.Pause();
                return;
            }
            PacketWriter pw = new PacketWriter();

            pw.Write((byte)Headers.Pause);
            pw.Write(queue.ID);
            Send(pw.GetBytes());
        }
예제 #10
0
        public void stopTransfer(TransferQueue queue)
        {
            if (queue.Type == QueueType.Upload)
            {
                queue.Stop();
            }
            PacketWriter pw = new PacketWriter();

            pw.Write((byte)Headers.Stop);
            pw.Write(queue.ID);
            Send(pw.GetBytes());
            queue.Close();
        }
        public void PauseTransfer(TransferQueue queue)
        {
            //Pause the queue.
            //This doesn't have to be done for the downloading queue, but its here for a reason.
            queue.Pause();

#pragma warning disable CS0436 // 类型与导入类型冲突
            PacketWriter pw = new PacketWriter();
#pragma warning restore CS0436 // 类型与导入类型冲突
            pw.Write((byte)Headers.Pause);
            pw.Write(queue.ID);
            Send(pw.GetBytes());
        }
예제 #12
0
        public void StopTransfer(TransferQueue queue)
        {
            //If we're the uploading transfer, we'll just stop it.
            if (queue.Type == QueueType.Upload)
            {
                queue.Stop();
            }

            PacketWriter pw = new PacketWriter();

            pw.Write((byte)Headers.Stop);
            pw.Write(queue.ID);
            Send(pw.GetBytes());
            //Don't forget to close the queue.
            queue.Close();
        }
        public void StopTransfer(TransferQueue queue)
        {
            //If we're the uploading transfer, we'll just stop it.
            if (queue.Type == QueueType.Upload)
            {
                queue.Stop();
            }

#pragma warning disable CS0436 // 类型与导入类型冲突
            PacketWriter pw = new PacketWriter();
#pragma warning restore CS0436 // 类型与导入类型冲突
            pw.Write((byte)Headers.Stop);
            pw.Write(queue.ID);
            Send(pw.GetBytes());
            //Don't forget to close the queue.
            queue.Close();
        }
예제 #14
0
 public static TransferQueue CreateDownloadQueue(TransferClient client, int id, string saveName, long length)
 {
     try
     {
         var queue = new TransferQueue();
         queue.FileName = Path.GetFileName(saveName);
         queue.Client   = client;
         queue.Type     = QueueType.Download;
         queue.FS       = new FileStream(saveName, FileMode.Create);
         queue.FS.SetLength(length);
         queue.Length = length;
         queue.ID     = id;
         return(queue);
     }
     catch
     {
         return(null);
     }
 }
예제 #15
0
        private static void transferProc(object o)
        {
            TransferQueue queue = (TransferQueue)o;

            while (queue.Running && queue.Index < queue.Length)
            {
                queue.pauseEvent.WaitOne();

                if (!queue.Running)
                {
                    break;
                }

                lock (file_buffer)
                {
                    queue.FS.Position = queue.Index;
                    int read = queue.FS.Read(file_buffer, 0, file_buffer.Length);

                    PacketWriter pw = new PacketWriter();
                    pw.Write((byte)Headers.Chunk);
                    pw.Write(queue.ID);
                    pw.Write(queue.Index);
                    pw.Write(read);
                    pw.Write(file_buffer, 0, read);

                    queue.Transfered += read;
                    queue.Index      += read;

                    queue.Client.Send(pw.GetBytes());

                    queue.Progress = (int)((queue.Transfered * 100) / queue.Length);

                    if (queue.LastProgress < queue.Progress)
                    {
                        queue.LastProgress = queue.Progress;
                        queue.Client.CallProgressChanged(queue);
                    }
                    Thread.Sleep(1);
                }
            }

            queue.Close();
        }
예제 #16
0
        public void QueueTransfer(string fileName)
        {
            try
            {
                TransferQueue queue = TransferQueue.CreateUploadQueue(this, fileName);
                _transfers.Add(queue.ID, queue);

                PacketWriter pw = new PacketWriter();
                pw.Write((byte)Headers.Queue);
                pw.Write(queue.ID);
                pw.Write(queue.FileName);
                pw.Write(queue.Length);
                Send(pw.GetBytes());

                if (Queued != null)
                {
                    Queued(this, queue);
                }
            }
            catch { }
        }
예제 #17
0
        public static TransferQueue CreateUploadQueue(TransferClient client, string fileNme)
        {
            try
            {
                var queue = new TransferQueue();
                queue.FileName            = Path.GetFileName(fileNme);
                queue.Client              = client;
                queue.Type                = QueueType.Upload;
                queue.FS                  = new FileStream(fileNme, FileMode.Open);
                queue.Thread              = new Thread(new ParameterizedThreadStart(transferProc));
                queue.Thread.IsBackground = true;
                queue.ID                  = Program.Rand.Next();
                queue.Length              = queue.FS.Length;

                return(queue);
            }
            catch
            {
                return(null);
            }
        }
예제 #18
0
 public static TransferQueue CreateDownloadQueue(TransferClient client, int id, string saveName, long length)
 {
     try
     {
         //Same as above with some changes.
         var queue = new TransferQueue();
         queue.Filename = Path.GetFileName(saveName);
         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.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);
     }
 }
예제 #19
0
        private static void transferProc(object o)
        {
            //Cast our transfer queue from the parameter.
            TransferQueue queue = (TransferQueue)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)Headers.Chunk);
                    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.
        }
예제 #20
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();
                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.
        }
예제 #21
0
        private void process()
        {
            PacketReader pr = new PacketReader(_buffer);

            Headers header = (Headers)pr.ReadByte();

            switch (header)
            {
            case Headers.Queue:
            {
                int    id       = pr.ReadInt32();
                string fileName = pr.ReadString();
                long   length   = pr.ReadInt64();

                TransferQueue queue = TransferQueue.CreateDownloadQueue(this, id, Path.Combine(OutputFolder, Path.GetFileName(fileName)), length);

                _transfers.Add(id, queue);

                if (Queued != null)
                {
                    Queued(this, queue);
                }
            }
            break;

            case Headers.Start:
            {
                int id = pr.ReadInt32();
                if (_transfers.ContainsKey(id))
                {
                    _transfers[id].Start();
                }
            }
            break;

            case Headers.Stop:
            {
                int id = pr.ReadInt32();
                if (_transfers.ContainsKey(id))
                {
                    TransferQueue queue = _transfers[id];
                    queue.Stop();
                    queue.Close();

                    if (Stoped != null)
                    {
                        Stoped(this, queue);
                    }
                    _transfers.Remove(id);
                }
            }
            break;

            case Headers.Pause:
            {
                int id = pr.ReadInt32();
                if (_transfers.ContainsKey(id))
                {
                    _transfers[id].Pause();
                }
            }
            break;

            case Headers.Chunk:
            {
                int    id     = pr.ReadInt32();
                long   index  = pr.ReadInt64();
                int    size   = pr.ReadInt32();
                byte[] buffer = pr.ReadBytes(size);

                TransferQueue queue = _transfers[id];
                queue.Write(buffer, index);
                queue.Progress = (int)((queue.Transfered * 100) / queue.Length);

                if (queue.LastProgress < queue.Progress)
                {
                    queue.LastProgress = queue.Progress;
                    if (ProgressChanged != null)
                    {
                        ProgressChanged(this, queue);
                    }
                    if (queue.Progress == 100)
                    {
                        queue.Close();
                        if (Complete != null)
                        {
                            Complete(this, queue);
                        }
                    }
                }
            }
            break;
            }
            pr.Dispose();
        }