/// <summary> /// Cancels a pending request or a running file transfer. /// </summary> /// <param name="e"></param> public Task <bool> CancelAsync(FTEventArgs e) { Task <bool> t = parent.Manager.SendPacketAsync(new P06Accepted(false, 7, ProblemCategory.None)); // This packet cancels any request or running transfer. e.Assign(parent, this); // This assignment is necessary for FTEventArgs to print exceptions and raise events. e.Finish(success: false); currentItem = null; return(t); }
internal async Task <bool> OnPacketReceivedAsync(P06Accepted packet) { const string name = nameof(OnPacketReceivedAsync) + "(" + nameof(P06Accepted) + ")"; if (currentItem == null) // It may be more efficient not to close the connection but only to cancel the file transfer. { parent.ExceptionHandler.CloseConnection("InvalidPacket", "Cannot resume file transfer for the received accepted packet.", nameof(FTSocket), name); return(false); } if (!packet.Accepted && packet.RelatedPacket == 7) // Cancellation is always made by denying P07OpenFileTransfer { if (!currentItem.Finish(success: false)) { return(false); } currentItem = null; } else if (packet.Accepted && packet.RelatedPacket == 7) { if ((currentItem.Mode == StreamMode.PushHeader || currentItem.Mode == StreamMode.PushFile) && !await parent.Manager.SendPacketAsync(new P08FileHeader(currentItem.FileMeta.GetBinaryData(parent.ConnectionVersion.Value)))) { return(false); } // counterpart accepted file transfer -> we have to sent the FileMeta first // No exceptions here because every request type get's an accepted packet } else if (packet.Accepted && packet.RelatedPacket == 8) { currentItem.OnFileMetaTransfered(); if (currentItem.Mode == StreamMode.PushFile) // only StreamMode.PushFile wants to receive the file data { if (!currentItem.OpenStream()) { return(false); } if (parent.ConnectionVersion <= 2) { return(await SendBlockAsync()); } else { return(ThreadPool.QueueUserWorkItem(SendFileAsync)); } } else if (currentItem.Mode != StreamMode.PushHeader) { parent.ExceptionHandler.CloseConnection("InvalidPacket", "The running file transfer is not supposed to receive an accepted packet for a file header.", nameof(FTSocket), name); return(false); } } else if (packet.Accepted && packet.RelatedPacket == 9) { if (parent.ConnectionVersion <= 2) { if (currentItem.Mode != StreamMode.PushFile) { parent.ExceptionHandler.CloseConnection("InvalidPacket", "The running file transfer is not supposed to receive an accepted packet for a file data block.", nameof(FTSocket), name); return(false); } else if (currentItem.Stream != null) { return(await SendBlockAsync()); } else // accept for the last file data block { currentItem = null; } } else { parent.ExceptionHandler.CloseConnection("InvalidPacket", "VSL 1.3 does not accept data block acknoledges anymore.", nameof(FTSocket), name); return(false); } } return(true); }