Beispiel #1
0
 internal void callCompleted(TransferQueue queue)
 {
     if (Complete != null)
     {
         Complete(this, queue);
     }
 }
Beispiel #2
0
 internal void callProgressChanged(TransferQueue queue)
 {
     if (ProgressChanged != null)
     {
         ProgressChanged(this, queue);
     }
 }
Beispiel #3
0
        public void StartTransfer(TransferQueue queue)
        {
            PacketWriter packetWriter = new PacketWriter();

            packetWriter.Write((byte)Headers.Start);
            packetWriter.Write(queue.Id);
            Send(packetWriter.GetBytes());
        }
Beispiel #4
0
        public void PauseTransfer(TransferQueue queue)
        {
            if (queue.Type == QueueType.Upload)
            {
                queue.Pause();
            }

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

            PacketWriter packetWriter = new PacketWriter();

            packetWriter.Write((byte)Headers.Pause);
            packetWriter.Write(queue.Id);
            Send(packetWriter.GetBytes());
        }
Beispiel #5
0
        private static void transferProc(object obj)
        {
            TransferQueue queue = (TransferQueue)obj;

            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 packetWriter = new PacketWriter();
                    packetWriter.Write((byte)Headers.Chunk);
                    packetWriter.Write(queue.Id);
                    packetWriter.Write(queue.Index);
                    packetWriter.Write(read);
                    packetWriter.Write(file_buffer, 0, read);

                    queue.Transferred += read;
                    queue.Index       += read;

                    queue.Client.Send(packetWriter.GetBytes());
                    queue.Progress = (int)((queue.Transferred * 100) / queue.Length);

                    if (queue.LastProgress < queue.Progress)
                    {
                        queue.LastProgress = queue.Progress;
                        queue.Client.callProgressChanged(queue);
                    }

                    Thread.Sleep(1);
                }
            }

            queue.Client?.callCompleted(queue);
            queue.Close();
        }
Beispiel #6
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 (Exception)
     {
         return(null);
     }
 }
Beispiel #7
0
        public void StopTransfer(TransferQueue queue)
        {
            if (queue.Type == QueueType.Upload)
            {
                queue.Stop();
            }

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

            PacketWriter packetWriter = new PacketWriter();

            packetWriter.Write((byte)Headers.Stop);
            packetWriter.Write(queue.Id);
            Send(packetWriter.GetBytes());
            queue.Close();
        }
Beispiel #8
0
 public static TransferQueue CreateUploadQueue(TransferClient client, string fileName)
 {
     try
     {
         var queue = new TransferQueue();
         queue.Filename            = fileName;
         queue.Client              = client;
         queue.Type                = QueueType.Upload;
         queue.FS                  = new FileStream(fileName, FileMode.Open);
         queue.Thread              = new Thread(new ParameterizedThreadStart(transferProc));
         queue.Thread.IsBackground = true;
         queue.Id                  = App.Random.Next();
         queue.Length              = queue.FS.Length;
         return(queue);
     }
     catch (Exception)
     {
         return(null);
     }
 }
Beispiel #9
0
        public void QueueTransfer(string fileName)
        {
            try
            {
                TransferQueue queue = TransferQueue.CreateUploadQueue(this, fileName);
                _transfers.Add(queue.Id, queue);
                PacketWriter packetWriter = new PacketWriter();
                packetWriter.Write((byte)Headers.Queue);
                packetWriter.Write(queue.Id);
                packetWriter.Write(queue.Filename);
                packetWriter.Write(queue.Length);
                Send(packetWriter.GetBytes());

                if (Queued != null)
                {
                    Queued(this, queue);
                }
            }
            catch (Exception)
            {
            }
        }
Beispiel #10
0
        private void process()
        {
            PacketReader packetReader = new PacketReader(_buffer);

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

            switch (header)
            {
            case Headers.Queue:
            {
                int    id       = packetReader.ReadInt32();
                string fileName = packetReader.ReadString();
                long   length   = packetReader.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 = packetReader.ReadInt32();

                if (_transfers.ContainsKey(id))
                {
                    _transfers[id].Start();
                }
            }
            break;

            case Headers.Stop:
            {
                int id = packetReader.ReadInt32();

                if (_transfers.ContainsKey(id))
                {
                    TransferQueue queue = _transfers[id];
                    queue.Stop();
                    queue.Close();

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

                    _transfers.Remove(id);
                }
            }
            break;

            case Headers.Pause:
            {
                int id = packetReader.ReadInt32();

                if (_transfers.ContainsKey(id))
                {
                    TransferQueue queue = _transfers[id];

                    if (queue.Type == QueueType.Upload)
                    {
                        queue.Pause();
                    }

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

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

                TransferQueue queue = _transfers[id];

                queue.Write(buffer, index);
                queue.Progress = (int)((queue.Transferred * 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;

            default:
                break;
            }
            packetReader.Dispose();
        }