Exemplo n.º 1
0
 public void Reset(int pending)
 {
     Stats.Reset(pending);
     ActiveTransfers.Clear();
     ActiveDeletions.Clear();
     ErrorMessages.Clear();
 }
 private static void RemoveFileTransfer(int id)
 {
     if (ActiveTransfers.ContainsKey(id))
     {
         ActiveTransfers[id].Dispose();
         ActiveTransfers.TryRemove(id, out _);
     }
 }
 public static void HandleDoDownloadFileCancel(FileTransferCancel command, Networking.Client client)
 {
     if (ActiveTransfers.ContainsKey(command.Id))
     {
         RemoveFileTransfer(command.Id);
         client.Send(new FileTransferCancel
         {
             Id     = command.Id,
             Reason = "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();
        }
        public static void HandleDoUploadFile(FileTransferChunk command, Networking.Client client)
        {
            try
            {
                if (command.Chunk.Offset == 0)
                {
                    if (File.Exists(command.FilePath))
                    {
                        NativeMethods.DeleteFile(command.FilePath); // delete existing file
                    }

                    ActiveTransfers[command.Id] = new FileSplit(command.FilePath, FileAccess.Write);
                }

                if (!ActiveTransfers.ContainsKey(command.Id))
                {
                    return;
                }

                var destFile = ActiveTransfers[command.Id];
                destFile.WriteChunk(command.Chunk);

                if (destFile.FileSize == command.FileSize)
                {
                    RemoveFileTransfer(command.Id);
                }
            }
            catch (Exception)
            {
                RemoveFileTransfer(command.Id);
                client.Send(new FileTransferCancel
                {
                    Id     = command.Id,
                    Reason = "Error writing file"
                });
            }
        }