Exemplo n.º 1
0
        /// <summary>
        /// Performs SSH_FXP_READ request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="length">The length.</param>
        /// <returns>data array; null if EOF</returns>
        public byte[] RequestRead(byte[] handle, ulong offset, uint length)
        {
            SshException exception = null;

            var data = new byte[0];

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpReadRequest(ProtocolVersion, NextRequestId, handle, offset, length,
                                                  response =>
                {
                    data = response.Data;
                    wait.Set();
                },
                                                  response =>
                {
                    if (response.StatusCode != StatusCodes.Eof)
                    {
                        exception = GetSftpException(response);
                    }
                    wait.Set();
                });

                SendRequest(request);

                WaitOnHandle(wait, OperationTimeout);
            }

            if (exception != null)
            {
                throw exception;
            }

            return(data);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Performs SSH_FXP_READ request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="length">The length.</param>
        /// <returns>data array; null if EOF</returns>
        internal byte[] RequestRead(byte[] handle, UInt64 offset, UInt32 length)
        {
            SshException exception = null;

            var data = new byte[0];

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpReadRequest(this.ProtocolVersion, this.NextRequestId, handle, offset, length,
                                                  (response) =>
                {
                    data = response.Data;
                    wait.Set();
                },
                                                  (response) =>
                {
                    if (response.StatusCode != StatusCodes.Eof)
                    {
                        exception = this.GetSftpException(response);
                    }
                    wait.Set();
                });

                this.SendRequest(request);

                this.WaitOnHandle(wait, this._operationTimeout);
            }

            if (exception != null)
            {
                throw exception;
            }

            return(data);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Performs SSH_FXP_READ request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="length">The length.</param>
        /// <returns>data array; null if EOF</returns>
        internal byte[] RequestRead(byte[] handle, UInt64 offset, UInt32 length)
        {
            byte[] data = new byte[0];

            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpReadRequest(this.NextRequestId, handle, offset, length,
                                                  (response) =>
                {
                    data = response.Data;
                    wait.Set();
                },
                                                  (response) =>
                {
                    if (response.StatusCode == StatusCodes.Eof)
                    {
                        wait.Set();
                    }
                    else
                    {
                        this.ThrowSftpException(response);
                    }
                });

                this.SendRequest(request);

                this.WaitHandle(wait, this._operationTimeout);
            }

            return(data);
        }
Exemplo n.º 4
0
        public void GetBytes()
        {
            var request = new SftpReadRequest(_protocolVersion, _requestId, _handle, _offset, _length, null, null);

            var bytes = request.GetBytes();

            var expectedBytesLength = 0;

            expectedBytesLength += 4;              // Length
            expectedBytesLength += 1;              // Type
            expectedBytesLength += 4;              // RequestId
            expectedBytesLength += 4;              // Handle length
            expectedBytesLength += _handle.Length; // Handle
            expectedBytesLength += 8;              // Offset
            expectedBytesLength += 4;              // Length

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

            Assert.AreEqual((uint)bytes.Length - 4, sshDataStream.ReadUInt32());
            Assert.AreEqual((byte)SftpMessageTypes.Read, sshDataStream.ReadByte());
            Assert.AreEqual(_requestId, sshDataStream.ReadUInt32());

            Assert.AreEqual((uint)_handle.Length, sshDataStream.ReadUInt32());
            var actualHandle = new byte[_handle.Length];

            sshDataStream.Read(actualHandle, 0, actualHandle.Length);
            Assert.IsTrue(_handle.SequenceEqual(actualHandle));

            Assert.AreEqual(_offset, sshDataStream.ReadUInt64());
            Assert.AreEqual(_length, sshDataStream.ReadUInt32());

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
Exemplo n.º 5
0
        public void Constructor()
        {
            var request = new SftpReadRequest(_protocolVersion, _requestId, _handle, _offset, _length, null, null);

            Assert.AreSame(_handle, request.Handle);
            Assert.AreEqual(_length, request.Length);
            Assert.AreEqual(_offset, request.Offset);
            Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
            Assert.AreEqual(_requestId, request.RequestId);
            Assert.AreEqual(SftpMessageTypes.Read, request.SftpMessageType);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Performs SSH_FXP_READ request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="length">The length.</param>
        /// <param name="readCompleted">Action for response</param>
        /// <returns>data array; null if EOF</returns>
        public bool RequestReadAsync(byte[] handle, UInt64 offset, UInt32 length, Action <SftpDataResponse> readCompleted)
        {
            var request = new SftpReadRequest(this.ProtocolVersion, this.NextRequestId, handle, offset, length,
                                              (response) =>//data
            {
                readCompleted(response);
            },
                                              (response) =>//status, eof, no data in area
            {
                SftpDataResponse dataResponse = new SftpDataResponse(response.ProtocolVersion);
                //dataResponse.Data = null;
                readCompleted(dataResponse);
            });

            this.SendRequest(request);

            return(true);
        }
Exemplo n.º 7
0
        public void Complete_SftpStatusResponse()
        {
            var statusActionInvocations = new List <SftpStatusResponse>();
            var dataActionInvocations   = new List <SftpDataResponse>();

            Action <SftpStatusResponse> statusAction = statusActionInvocations.Add;
            Action <SftpDataResponse>   dataAction   = dataActionInvocations.Add;
            var statusResponse = new SftpStatusResponse(_protocolVersion);

            var request = new SftpReadRequest(
                _protocolVersion,
                _requestId,
                _handle,
                _offset,
                _length,
                dataAction,
                statusAction);

            request.Complete(statusResponse);

            Assert.AreEqual(1, statusActionInvocations.Count);
            Assert.AreSame(statusResponse, statusActionInvocations[0]);
            Assert.AreEqual(0, dataActionInvocations.Count);
        }