상속: SftpRequest
예제 #1
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);
        }
예제 #2
0
        public void Complete_SftpDataResponse()
        {
            var statusActionInvocations = new List<SftpStatusResponse>();
            var dataActionInvocations = new List<SftpDataResponse>();

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

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

            request.Complete(dataResponse);

            Assert.AreEqual(0, statusActionInvocations.Count);
            Assert.AreEqual(1, dataActionInvocations.Count);
            Assert.AreSame(dataResponse, dataActionInvocations[0]);
        }
예제 #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)
        {
            SshException exception = null;

            byte[] 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.WaitHandle(wait, this._operationTimeout);
            }

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

            return data;
        }
        /// <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 = null;

            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;
        }
예제 #5
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);
        }
예제 #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>
        /// <returns>data array; null if EOF</returns>
        public byte[] RequestRead(byte[] handle, ulong offset, uint length)
        {
            SshException exception = null;

            byte[] data = null;

            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);
                            }
                            data = Array<byte>.Empty;
                            wait.Set();
                        });

                SendRequest(request);

                WaitOnHandle(wait, OperationTimeout);
            }

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

            return data;
        }