GetPreferredDatagramSize() public method

Gets preferred datagram size.
public GetPreferredDatagramSize ( ) : int
return int
Ejemplo n.º 1
0
        private bool UploadFile(string fullPath, SCPChannelStream stream, bool preserveTime,
                                Cancellation cancellation,
                                SCPFileTransferProgressDelegate progressDelegate)
        {
            Debug.Assert(fullPath != null);

            string   fileName    = Path.GetFileName(fullPath);
            FileInfo fileInfo    = new FileInfo(fullPath);
            long     fileSize    = fileInfo.Length;
            ulong    transmitted = 0;

            if (progressDelegate != null)
            {
                progressDelegate(fullPath, fileName, SCPFileTransferStatus.Open, (ulong)fileSize, transmitted);
            }

            if (preserveTime)
            {
                SendModTime(
                    stream,
                    File.GetLastWriteTimeUtc(fullPath),
                    File.GetLastAccessTimeUtc(fullPath)
                    );
            }

            using (FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                string line = new StringBuilder()
                              .Append('C')
                              .Append(GetPermissionsText(false))
                              .Append(' ')
                              .Append(fileSize.ToString(NumberFormatInfo.InvariantInfo))
                              .Append(' ')
                              .Append(fileName)
                              .Append('\n')
                              .ToString();
                stream.Write(_encoding.GetBytes(line));
                CheckResponse(stream);

                byte[] buff = new byte[stream.GetPreferredDatagramSize()];

                long remain = fileSize;
                while (remain > 0)
                {
                    if (cancellation != null && cancellation.IsRequested)
                    {
                        if (progressDelegate != null)
                        {
                            progressDelegate(fullPath, fileName, SCPFileTransferStatus.CompletedAbort, (ulong)fileSize, transmitted);
                        }
                        return(false);   // cancel
                    }

                    int readLength = fileStream.Read(buff, 0, buff.Length);
                    if (readLength <= 0)
                    {
                        break;
                    }
                    if (readLength > remain)
                    {
                        readLength = (int)remain;
                    }
                    stream.Write(buff, readLength);
                    remain -= readLength;

                    transmitted += (ulong)readLength;
                    if (progressDelegate != null)
                    {
                        progressDelegate(fullPath, fileName, SCPFileTransferStatus.Transmitting, (ulong)fileSize, transmitted);
                    }
                }

                stream.Write(ZERO);
                CheckResponse(stream);
            }

            if (progressDelegate != null)
            {
                progressDelegate(fullPath, fileName, SCPFileTransferStatus.CompletedSuccess, (ulong)fileSize, transmitted);
            }

            return(true);
        }
Ejemplo n.º 2
0
        private bool UploadFile(string fullPath, SCPChannelStream stream, bool preserveTime,
                        Cancellation cancellation,
                        SCPFileTransferProgressDelegate progressDelegate)
        {
            Debug.Assert(fullPath != null);

            string fileName = Path.GetFileName(fullPath);
            FileInfo fileInfo = new FileInfo(fullPath);
            long fileSize = fileInfo.Length;
            ulong transmitted = 0;

            if (progressDelegate != null)
                progressDelegate(fullPath, fileName, SCPFileTransferStatus.Open, (ulong)fileSize, transmitted);

            if (preserveTime) {
                SendModTime(
                    stream,
                    File.GetLastWriteTimeUtc(fullPath),
                    File.GetLastAccessTimeUtc(fullPath)
                );
            }

            using (FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                string line = new StringBuilder()
                    .Append('C')
                    .Append(GetPermissionsText(false))
                    .Append(' ')
                    .Append(fileSize.ToString(NumberFormatInfo.InvariantInfo))
                    .Append(' ')
                    .Append(fileName)
                    .Append('\n')
                    .ToString();
                stream.Write(_encoding.GetBytes(line));
                CheckResponse(stream);

                byte[] buff = new byte[stream.GetPreferredDatagramSize()];

                long remain = fileSize;
                while (remain > 0) {
                    if (cancellation != null && cancellation.IsRequested) {
                        if (progressDelegate != null)
                            progressDelegate(fullPath, fileName, SCPFileTransferStatus.CompletedAbort, (ulong)fileSize, transmitted);
                        return false;   // cancel
                    }

                    int readLength = fileStream.Read(buff, 0, buff.Length);
                    if (readLength <= 0)
                        break;
                    if (readLength > remain)
                        readLength = (int)remain;
                    stream.Write(buff, readLength);
                    remain -= readLength;

                    transmitted += (ulong)readLength;
                    if (progressDelegate != null)
                        progressDelegate(fullPath, fileName, SCPFileTransferStatus.Transmitting, (ulong)fileSize, transmitted);
                }

                stream.Write(ZERO);
                CheckResponse(stream);
            }

            if (progressDelegate != null)
                progressDelegate(fullPath, fileName, SCPFileTransferStatus.CompletedSuccess, (ulong)fileSize, transmitted);

            return true;
        }