| | | 1 | | using System.Globalization; |
| | | 2 | | using System.IO; |
| | | 3 | | |
| | | 4 | | using Renci.SshNet.Common; |
| | | 5 | | |
| | | 6 | | namespace Renci.SshNet.Sftp |
| | | 7 | | { |
| | | 8 | | internal abstract class SftpMessage : SshData |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Gets the size of the message in bytes. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <value> |
| | | 14 | | /// The size of the messages in bytes. |
| | | 15 | | /// </value> |
| | | 16 | | protected override int BufferCapacity |
| | | 17 | | { |
| | | 18 | | get |
| | 32405 | 19 | | { |
| | | 20 | | // 4 bytes for the length of the SFTP data |
| | | 21 | | // 1 byte for the SFTP message type |
| | 32405 | 22 | | return 5; |
| | 32405 | 23 | | } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public abstract SftpMessageTypes SftpMessageType { get; } |
| | | 27 | | |
| | | 28 | | protected override void LoadData() |
| | 32177 | 29 | | { |
| | 32177 | 30 | | } |
| | | 31 | | |
| | | 32 | | protected override void SaveData() |
| | 32405 | 33 | | { |
| | 32405 | 34 | | Write((byte) SftpMessageType); |
| | 32405 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Writes the current message to the specified <see cref="SshDataStream"/>. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="stream">The <see cref="SshDataStream"/> to write the message to.</param> |
| | | 41 | | protected override void WriteBytes(SshDataStream stream) |
| | 32405 | 42 | | { |
| | | 43 | | const int sizeOfDataLengthBytes = 4; |
| | | 44 | | |
| | 32405 | 45 | | var startPosition = stream.Position; |
| | | 46 | | |
| | | 47 | | // skip 4 bytes for the length of the SFTP message data |
| | 32405 | 48 | | _ = stream.Seek(sizeOfDataLengthBytes, SeekOrigin.Current); |
| | | 49 | | |
| | | 50 | | // write the SFTP message data to the stream |
| | 32405 | 51 | | base.WriteBytes(stream); |
| | | 52 | | |
| | | 53 | | // save where we were positioned when we finished writing the SSH message data |
| | 32405 | 54 | | var endPosition = stream.Position; |
| | | 55 | | |
| | | 56 | | // determine the length of the SSH message data |
| | 32405 | 57 | | var dataLength = endPosition - startPosition - sizeOfDataLengthBytes; |
| | | 58 | | |
| | | 59 | | // write the length of the SFTP message where we were positioned before we started |
| | | 60 | | // writing the SFTP message data |
| | 32405 | 61 | | stream.Position = startPosition; |
| | 32405 | 62 | | stream.Write((uint) dataLength); |
| | | 63 | | |
| | | 64 | | // move back to we were positioned when we finished writing the SFTP message data |
| | 32405 | 65 | | stream.Position = endPosition; |
| | 32405 | 66 | | } |
| | | 67 | | |
| | | 68 | | protected SftpFileAttributes ReadAttributes() |
| | 22150 | 69 | | { |
| | 22150 | 70 | | return SftpFileAttributes.FromBytes(DataStream); |
| | 22150 | 71 | | } |
| | | 72 | | |
| | | 73 | | public override string ToString() |
| | 0 | 74 | | { |
| | 0 | 75 | | return string.Format(CultureInfo.CurrentCulture, "SFTP Message : {0}", SftpMessageType); |
| | 0 | 76 | | } |
| | | 77 | | } |
| | | 78 | | } |