Exemple #1
0
 internal async Task <bool> HandleP06Accepted(P06Accepted p)
 {
     if (p.RelatedPacket > 5 && p.RelatedPacket < 10)
     {
         return(await parent.FileTransfer.OnPacketReceivedAsync(p));
     }
     else
     {
         parent.ExceptionHandler.CloseConnection("InvalidPacket",
                                                 $"Could not resume related packet with id {p.RelatedPacket}.",
                                                 nameof(PacketHandler), nameof(HandleP06Accepted));
         return(false);
     }
 }
Exemple #2
0
        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);
        }