Esempio n. 1
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();
        }
Esempio n. 2
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.Transferred += read;
                    queue.Index       += read;

                    queue.Client.Send(pw.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.Close();
        }
Esempio n. 3
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 (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.Transferred * 100) / queue.Length);
                if (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();
        }