Esempio n. 1
0
        public void Constructor()
        {
            var request = new SftpWriteRequest(_protocolVersion, _requestId, _handle, _offset, _data, _length, null);

            Assert.AreSame(_data, request.Data);
            Assert.AreSame(_handle, request.Handle);
            Assert.AreEqual(_length, request.Length);
            Assert.AreEqual(_protocolVersion, request.ProtocolVersion);
            Assert.AreEqual(_requestId, request.RequestId);
            Assert.AreEqual(SftpMessageTypes.Write, request.SftpMessageType);
        }
Esempio n. 2
0
        public void Complete_SftpStatusResponse()
        {
            var statusActionInvocations = new List<SftpStatusResponse>();
            Action<SftpStatusResponse> statusAction = statusActionInvocations.Add;
            var statusResponse = new SftpStatusResponse(_protocolVersion);

            var request = new SftpWriteRequest(
                _protocolVersion,
                _requestId,
                _handle,
                _offset,
                _data,
                _length,
                statusAction);

            request.Complete(statusResponse);

            Assert.AreEqual(1, statusActionInvocations.Count);
            Assert.AreSame(statusResponse, statusActionInvocations[0]);
        }
Esempio n. 3
0
        public void GetBytes()
        {
            var request = new SftpWriteRequest(_protocolVersion, _requestId, _handle, _offset, _data, _length, 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; // Data length
            expectedBytesLength += _length; // Data

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

            Assert.AreEqual((uint) bytes.Length - 4, sshDataStream.ReadUInt32());
            Assert.AreEqual((byte) SftpMessageTypes.Write, 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((uint) _length, sshDataStream.ReadUInt32());
            var actualData = new byte[_length];
            sshDataStream.Read(actualData, 0, actualData.Length);
            Assert.IsTrue(_data.Take(_length).SequenceEqual(actualData));

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
Esempio n. 4
0
        /// <summary>
        /// Performs SSH_FXP_WRITE request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="data">The data to send.</param>
        /// <param name="wait">The wait event handle if needed.</param>
        internal void RequestWrite(byte[] handle, UInt64 offset, byte[] data, EventWaitHandle wait, Action<SftpStatusResponse> writeCompleted = null)
        {
            SshException exception = null;

            var request = new SftpWriteRequest(this.ProtocolVersion, this.NextRequestId, handle, offset, data,
                (response) =>
                {
                    if (writeCompleted != null)
                    {
                        writeCompleted(response);
                    }

                    exception = this.GetSftpException(response);
                    if (wait != null)
                        wait.Set();
                });

            this.SendRequest(request);

            if (wait != null)
                this.WaitHandle(wait, this._operationTimeout);

            if (exception != null)
            {
                throw exception;
            }
        }
        /// <summary>
        /// Performs SSH_FXP_WRITE request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="data">The data.</param>
        internal void RequestWrite(byte[] handle, UInt64 offset, byte[] data)
        {
            using (var wait = new AutoResetEvent(false))
            {
                var request = new SftpWriteRequest(this.NextRequestId, handle, offset, data,
                    (response) =>
                    {
                        if (response.StatusCode == StatusCodes.Ok)
                        {
                            wait.Set();
                        }
                        else
                        {
                            this.ThrowSftpException(response);
                        }
                    });

                this.SendRequest(request);

                this.WaitHandle(wait, this._operationTimeout);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Performs SSH_FXP_WRITE request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="serverOffset">The the zero-based offset (in bytes) relative to the beginning of the file that the write must start at.</param>
        /// <param name="data">The buffer holding the data to write.</param>
        /// <param name="offset">the zero-based offset in <paramref name="data" /> at which to begin taking bytes to write.</param>
        /// <param name="length">The length (in bytes) of the data to write.</param>
        /// <param name="wait">The wait event handle if needed.</param>
        /// <param name="writeCompleted">The callback to invoke when the write has completed.</param>
        public void RequestWrite(byte[] handle,
                                 ulong serverOffset,
                                 byte[] data,
                                 int offset,
                                 int length,
                                 AutoResetEvent wait,
                                 Action<SftpStatusResponse> writeCompleted = null)
        {
            SshException exception = null;

            var request = new SftpWriteRequest(ProtocolVersion, NextRequestId, handle, serverOffset, data, offset,
                length, response =>
                    {
                        if (writeCompleted != null)
                        {
                            writeCompleted(response);
                        }

                        exception = GetSftpException(response);
                        if (wait != null)
                            wait.Set();
                    });

            SendRequest(request);

            if (wait != null)
                WaitOnHandle(wait, OperationTimeout);

            if (exception != null)
            {
                throw exception;
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Performs SSH_FXP_WRITE request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="data">The data to send.</param>
        /// <param name="wait">The wait event handle if needed.</param>
        public void RequestWrite(byte[] handle, UInt64 offset, byte[] data)
        {
            const int maximumDataSize = 1024 * 32 - 38;

            if (data.Length < maximumDataSize + 1)
            {
                using (var wait = new AutoResetEvent(false))
                {
                    var request = new SftpWriteRequest(this.NextRequestId, handle, offset, data,
                                                       (response) =>
                                                           {
                                                               if (response.StatusCode == StatusCodes.Ok)
                                                               {
                                                                   wait.Set();
                                                               }
                                                               else
                                                               {
                                                                   ThrowSftpException(response);
                                                               }
                                                           });

                    this.SendRequest(request);

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

            }
            else
            {
              
            int block = ((data.Length - 1)/maximumDataSize) + 1;
            using (var cnt = new CountdownEvent(block))
            {
                for (int i = 0; i < block; i++)
                {
                    var blockBufferSize = Math.Min(data.Length - maximumDataSize*i, maximumDataSize);
                    var blockBuffer = new byte[blockBufferSize];

                    Buffer.BlockCopy(data, i*maximumDataSize, blockBuffer, 0, blockBufferSize);

                    var request = new SftpWriteRequest(this.NextRequestId, handle, offset + (ulong) (i*maximumDataSize),
                                                       blockBuffer,
                                                       (response) =>
                                                           {
                                                               if (response.StatusCode == StatusCodes.Ok)
                                                               {
                                                                   // if (wait != null)
                                                                   //  wait.Set();
                                                                   cnt.Signal();
                                                               }
                                                               else
                                                               {
                                                                   ThrowSftpException(response);
                                                               }
                                                           });

                    this.SendRequest(request);
                }

                this.WaitHandle(cnt.WaitHandle, this._operationTimeout/*new TimeSpan(block*this._operationTimeout.Ticks)*/);
                

            }
            }
        }
        /// <summary>
        /// Performs SSH_FXP_WRITE request.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="data">The data to send.</param>
        /// <param name="wait">The wait event handle if needed.</param>
        internal void RequestWrite(byte[] handle, UInt64 offset, byte[] data, EventWaitHandle wait)
        {
            var maximumDataSize = 1024 * 32 - 38;

            if (data.Length < maximumDataSize + 1)
            {
                var request = new SftpWriteRequest(this.NextRequestId, handle, offset, data,
                    (response) =>
                    {
                        if (response.StatusCode == StatusCodes.Ok)
                        {
                            if (wait != null)
                                wait.Set();
                        }
                        else
                        {
                            this.ThrowSftpException(response);
                        }
                    });

                this.SendRequest(request);

                if (wait != null)
                    this.WaitHandle(wait, this._operationTimeout);
            }
            else
            {
                var block = data.Length / maximumDataSize + 1;

                for (int i = 0; i < block; i++)
                {
                    var blockBufferSize = Math.Min(data.Length - maximumDataSize * i, maximumDataSize);
                    var blockBuffer = new byte[blockBufferSize];

                    Buffer.BlockCopy(data, i * maximumDataSize, blockBuffer, 0, blockBufferSize);

                    var request = new SftpWriteRequest(this.NextRequestId, handle, offset + (ulong)(i * maximumDataSize), blockBuffer,
                        (response) =>
                        {
                            if (response.StatusCode == StatusCodes.Ok)
                            {
                                if (wait != null)
                                    wait.Set();
                            }
                            else
                            {
                                this.ThrowSftpException(response);
                            }
                        });

                    this.SendRequest(request);

                    if (wait != null)
                        this.WaitHandle(wait, this._operationTimeout);
                }
            }
        }