Renci.SshNet.Sftp.SftpSession.CalculateOptimalReadLength C# (CSharp) Method

CalculateOptimalReadLength() public method

Calculates the optimal size of the buffer to read data from the channel.
public CalculateOptimalReadLength ( uint bufferSize ) : uint
bufferSize uint The buffer size configured on the client.
return uint
        public uint CalculateOptimalReadLength(uint bufferSize)
        {
            // a SSH_FXP_DATA message has 13 bytes of protocol fields:
            // bytes 1 to 4: packet length
            // byte 5: message type
            // bytes 6 to 9: response id
            // bytes 10 to 13: length of payload‏
            //
            // most ssh servers limit the size of the payload of a SSH_MSG_CHANNEL_DATA
            // response to 16 KB; if we requested 16 KB of data, then the SSH_FXP_DATA
            // payload of the SSH_MSG_CHANNEL_DATA message would be too big (16 KB + 13 bytes), and
            // as a result, the ssh server would split this into two responses:
            // one containing 16384 bytes (13 bytes header, and 16371 bytes file data)
            // and one with the remaining 13 bytes of file data
            const uint lengthOfNonDataProtocolFields = 13u;
            var maximumPacketSize = Channel.LocalPacketSize;
            return Math.Min(bufferSize, maximumPacketSize) - lengthOfNonDataProtocolFields;
        }

Usage Example

Beispiel #1
0
        internal SftpFileStream(SftpSession session, string path, FileMode mode, FileAccess access, int bufferSize, bool useAsync)
        {
            // Validate the parameters.
            if (session == null)
                throw new SshConnectionException("Client not connected.");

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException("bufferSize");
            }
            if (access < FileAccess.Read || access > FileAccess.ReadWrite)
            {
                throw new ArgumentOutOfRangeException("access");
            }
            if (mode < FileMode.CreateNew || mode > FileMode.Append)
            {
                throw new ArgumentOutOfRangeException("mode");
            }

            this.Timeout = TimeSpan.FromSeconds(30);
            this.Name = path;

            // Initialize the object state.
            this._session = session;
            this._access = access;
            this._ownsHandle = true;
            this._isAsync = useAsync;
            this._bufferPosition = 0;
            this._bufferLen = 0;
            this._bufferOwnedByWrite = false;
            this._canSeek = true;
            this._position = 0;
            this._serverFilePosition = 0;
            this._session.Disconnected += Session_Disconnected;

            var flags = Flags.None;

            switch (access)
            {
                case FileAccess.Read:
                    flags |= Flags.Read;
                    break;
                case FileAccess.Write:
                    flags |= Flags.Write;
                    break;
                case FileAccess.ReadWrite:
                    flags |= Flags.Read;
                    flags |= Flags.Write;
                    break;
            }

            switch (mode)
            {
                case FileMode.Append:
                    flags |= Flags.Append;
                    break;
                case FileMode.Create:
                    this._handle = this._session.RequestOpen(path, flags | Flags.Truncate, true);
                    if (this._handle == null)
                    {
                        flags |= Flags.CreateNew;
                    }
                    else
                    {
                        flags |= Flags.Truncate;
                    }
                    break;
                case FileMode.CreateNew:
                    flags |= Flags.CreateNew;
                    break;
                case FileMode.Open:
                    break;
                case FileMode.OpenOrCreate:
                    flags |= Flags.CreateNewOrOpen;
                    break;
                case FileMode.Truncate:
                    flags |= Flags.Truncate;
                    break;
            }

            if (this._handle == null)
                this._handle = this._session.RequestOpen(path, flags);

            this._attributes = this._session.RequestFStat(this._handle);

            this._readBufferSize = (int)session.CalculateOptimalReadLength((uint)bufferSize);
            this._readBuffer = new byte[_readBufferSize];
            this._writeBufferSize = (int)session.CalculateOptimalWriteLength((uint)bufferSize, _handle);
            this._writeBuffer = new byte[_writeBufferSize];

            if (mode == FileMode.Append)
            {
                this._position = this._attributes.Size;
                this._serverFilePosition = (ulong)this._attributes.Size;
            }
        }
All Usage Examples Of Renci.SshNet.Sftp.SftpSession::CalculateOptimalReadLength