示例#1
0
        public static void HandleDoDownloadFile(DoDownloadFile command, Networking.Client client)
        {
            new Thread(() =>
            {
                _limitThreads.WaitOne();
                try
                {
                    FileSplit srcFile = new FileSplit(command.RemotePath);
                    if (srcFile.MaxBlocks < 0)
                    {
                        throw new Exception(srcFile.LastError);
                    }

                    for (int currentBlock = 0; currentBlock < srcFile.MaxBlocks; currentBlock++)
                    {
                        if (!client.Connected || _canceledDownloads.ContainsKey(command.Id))
                        {
                            break;
                        }

                        byte[] block;

                        if (!srcFile.ReadBlock(currentBlock, out block))
                        {
                            throw new Exception(srcFile.LastError);
                        }


                        client.SendBlocking(new DoDownloadFileResponse
                        {
                            Id            = command.Id,
                            Filename      = Path.GetFileName(command.RemotePath),
                            Block         = block,
                            MaxBlocks     = srcFile.MaxBlocks,
                            CurrentBlock  = currentBlock,
                            CustomMessage = srcFile.LastError
                        });
                    }
                }
                catch (Exception ex)
                {
                    client.SendBlocking(new DoDownloadFileResponse
                    {
                        Id            = command.Id,
                        Filename      = Path.GetFileName(command.RemotePath),
                        Block         = new byte[0],
                        MaxBlocks     = -1,
                        CurrentBlock  = -1,
                        CustomMessage = ex.Message
                    });
                }
                _limitThreads.Release();
            }).Start();
        }
示例#2
0
 public static void HandleDoDownloadFileCancel(DoDownloadFileCancel command, Networking.Client client)
 {
     if (!_canceledDownloads.ContainsKey(command.Id))
     {
         _canceledDownloads.Add(command.Id, "canceled");
         client.SendBlocking(new DoDownloadFileResponse
         {
             Id            = command.Id,
             Filename      = "canceled",
             Block         = new byte[0],
             MaxBlocks     = -1,
             CurrentBlock  = -1,
             CustomMessage = "Canceled"
         });
     }
 }
        public static void HandleDoDownloadFile(FileTransferRequest command, Networking.Client client)
        {
            new Thread(() =>
            {
                LimitThreads.WaitOne();
                try
                {
                    using (var srcFile = new FileSplit(command.RemotePath, FileAccess.Read))
                    {
                        ActiveTransfers[command.Id] = srcFile;
                        foreach (var chunk in srcFile)
                        {
                            if (!client.Connected || !ActiveTransfers.ContainsKey(command.Id))
                            {
                                break;
                            }

                            // blocking sending might not be required, needs further testing
                            client.SendBlocking(new FileTransferChunk
                            {
                                Id       = command.Id,
                                FilePath = command.RemotePath,
                                FileSize = srcFile.FileSize,
                                Chunk    = chunk
                            });
                        }
                    }
                }
                catch (Exception)
                {
                    client.Send(new FileTransferCancel
                    {
                        Id     = command.Id,
                        Reason = "Error reading file"
                    });
                }
                finally
                {
                    RemoveFileTransfer(command.Id);
                    LimitThreads.Release();
                }
            }).Start();
        }