コード例 #1
0
ファイル: FTSocket.cs プロジェクト: skynet-im/vsl-net
        private async void SendFileAsync(object state)
        {
            try
            {
                byte[] buffer   = new byte[parent.Settings.FTBlockSize];
                ulong  position = 0;
                int    count    = 0;
                do
                {
                    position = (ulong)currentItem.Stream.Position;
                    count    = await currentItem.Stream.ReadAsync(buffer, 0, buffer.Length);

                    var packet = new P09FileDataBlock(position, new ArraySegment <byte>(buffer, 0, count));
                    if (!await parent.Manager.SendPacketAsync(packet, background: true))
                    {
                        return;
                    }
                    currentItem.OnProgress();
                } while (count == buffer.Length);
                currentItem.Finish(success: true);
                currentItem = null;
            }
            catch (Exception ex)
            {
                parent.ExceptionHandler.CloseConnection(ex);
            }
        }
コード例 #2
0
ファイル: FTSocket.cs プロジェクト: skynet-im/vsl-net
        private async Task <bool> SendBlockAsync()
        {
            byte[] buffer = new byte[parent.Settings.FTBlockSize];
            ulong  pos    = Convert.ToUInt64(currentItem.Stream.Position);
            int    count;

            try
            {
                count = await currentItem.Stream.ReadAsync(buffer, 0, buffer.Length);
            }
            catch (Exception ex)
            {
                parent.ExceptionHandler.CloseConnection(ex);
                return(false);
            }
            var packet = new P09FileDataBlock(pos, new ArraySegment <byte>(buffer, 0, count));

            if (!await parent.Manager.SendPacketAsync(packet, background: true))
            {
                return(false);
            }
            currentItem.OnProgress();
            if (count < buffer.Length)
            {
                return(currentItem.Finish(success: true));
            }
            return(true);
        }
コード例 #3
0
ファイル: FTSocket.cs プロジェクト: skynet-im/vsl-net
        internal async Task <bool> OnPacketReceivedAsync(P09FileDataBlock packet)
        {
            const string name = nameof(OnPacketReceivedAsync) + "(" + nameof(P09FileDataBlock) + ")";

            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 file data block.",
                                                        nameof(FTSocket), name);
                return(false);
            }
            if (currentItem.Mode != StreamMode.GetFile)
            {
                parent.ExceptionHandler.CloseConnection("InvalidPacket",
                                                        $"The running file transfer with mode {currentItem.Mode} is not supposed to receive a file data block.",
                                                        nameof(FTSocket), name);
                return(false);
            }
            if (currentItem.Stream == null)
            {
                parent.ExceptionHandler.CloseConnection("InvalidPacket",
                                                        "The request for the first FileDataBlock has not been sent yet.",
                                                        nameof(FTSocket), name);
                return(false);
            }
            try
            {
                await currentItem.Stream.WriteAsync(packet.DataBlock.Array, 0, packet.DataBlock.Count, currentItem.CancellationToken);
            }
            catch (Exception ex)
            {
                parent.ExceptionHandler.CloseConnection(ex);
                return(false);
            }
            currentItem.OnProgress();
            if (currentItem.Stream.Position == currentItem.FileMeta.Length)
            {
                if (!currentItem.Finish(success: true))
                {
                    return(false);
                }
                currentItem = null;
            }
            else if (currentItem.Stream.Position > currentItem.FileMeta.Length)
            {
                parent.ExceptionHandler.CloseConnection("EndOfFileExpected",
                                                        $"The file meta indicates a size of {currentItem.FileMeta.Length} bytes for this file " +
                                                        $"but the stream position is already at {currentItem.Stream.Position} bytes",
                                                        nameof(FTSocket), name);
            }

            if (parent.ConnectionVersion <= 2) // Previous versions to 1.3 need to request the counterpart to continue
            {
                return(await parent.Manager.SendPacketAsync(new P06Accepted(true, 9, ProblemCategory.None)));
            }

            return(true);
        }
コード例 #4
0
 internal Task <bool> HandleP09FileDataBlock(P09FileDataBlock p)
 {
     return(parent.FileTransfer.OnPacketReceivedAsync(p));
 }