internal void callProgressChanged(TransferQueue queue)
 {
     if (ProgressChanged != null)
     {
         ProgressChanged(this, queue);
     }
 }
        public void StartTransfer(TransferQueue queue)
        {
            PacketWriter pw = new PacketWriter();

            pw.Write((byte)Headers.Start);
            pw.Write(queue.ID);
            Send(pw.GetBytes());
        }
        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());
        }
        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();
        }
Exemple #5
0
 public static TransferQueue CreateDownloadQueue(TransferClient client, int id, string savename, long lenght)
 {
     try
     {
         var queu = new TransferQueue();
         queu.FileName = Path.GetFileName(savename);
         queu.Client   = client;
         queu.Type     = QueueType.Download;
         queu.FS       = new FileStream(savename, FileMode.Create);
         queu.FS.SetLength(lenght);
         queu.Length = lenght;
         queu.ID     = id;
         return(queu);
     }
     catch
     {
         return(null);
     }
 }
Exemple #6
0
 public static TransferQueue CreateUploadQueue(TransferClient client, string filename)
 {
     try
     {
         var queu = new TransferQueue();
         queu.FileName            = Path.GetFileName(filename);
         queu.Client              = client;
         queu.Type                = QueueType.Upload;
         queu.FS                  = new FileStream(filename, FileMode.Open);
         queu.Thread              = new Thread(new ParameterizedThreadStart(transferProc));
         queu.Thread.IsBackground = true;
         queu.ID                  = App.Rand.Next();
         queu.Length              = queu.FS.Length;
         return(queu);
     }
     catch
     {
         return(null);
     }
 }
        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
            {
            }
        }
Exemple #8
0
        private static void transferProc(object o)
        {
            TransferQueue queue = (TransferQueue)o;

            while (queue.Running && queue.Index < queue.Length)
            {
                queue.pauseEnevt.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.TransferRead += read;
                    queue.Index        += read;
                    queue.Client.Send(pw.GetBytes());

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

                    if (queue.LastProgress < queue.Progress)
                    {
                        queue.LastProgress = queue.Progress;
                        queue.Client.callProgressChanged(queue);
                    }
                    Thread.Sleep(1);
                }
            }
            queue.Close();
        }
        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   lenght   = pr.ReadInt64();

                TransferQueue queue = TransferQueue.CreateDownloadQueue(this, id, Path.Combine(OutputFolder, Path.GetFileName(filename)), lenght);
                _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 (Stopped != null)
                    {
                        Stopped(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.TransferRead * 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();
        }