ChannelOpenFailureMessage.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. namespace Renci.SshNet.Messages.Connection
  2. {
  3. /// <summary>
  4. /// Represents SSH_MSG_CHANNEL_OPEN_FAILURE message.
  5. /// </summary>
  6. [Message("SSH_MSG_CHANNEL_OPEN_FAILURE", 92)]
  7. public class ChannelOpenFailureMessage : ChannelMessage
  8. {
  9. internal const uint AdministrativelyProhibited = 1;
  10. internal const uint ConnectFailed = 2;
  11. internal const uint UnknownChannelType = 3;
  12. internal const uint ResourceShortage = 4;
  13. #if TUNING
  14. private byte[] _description;
  15. private byte[] _language;
  16. #endif
  17. /// <summary>
  18. /// Gets failure reason code.
  19. /// </summary>
  20. public uint ReasonCode { get; private set; }
  21. /// <summary>
  22. /// Gets description for failure.
  23. /// </summary>
  24. #if TUNING
  25. public string Description
  26. {
  27. get { return Utf8.GetString(_description, 0, _description.Length); }
  28. private set { _description = Utf8.GetBytes(value); }
  29. }
  30. #else
  31. public string Description { get; private set; }
  32. #endif
  33. /// <summary>
  34. /// Gets message language.
  35. /// </summary>
  36. #if TUNING
  37. public string Language
  38. {
  39. get { return Utf8.GetString(_language, 0, _language.Length); }
  40. private set { _language = Utf8.GetBytes(value); }
  41. }
  42. #else
  43. public string Language { get; private set; }
  44. #endif
  45. #if TUNING
  46. /// <summary>
  47. /// Gets the size of the message in bytes.
  48. /// </summary>
  49. /// <value>
  50. /// The size of the messages in bytes.
  51. /// </value>
  52. protected override int BufferCapacity
  53. {
  54. get
  55. {
  56. var capacity = base.BufferCapacity;
  57. capacity += 4; // ReasonCode
  58. capacity += 4; // Description length
  59. capacity += _description.Length; // Description
  60. capacity += 4; // Language length
  61. capacity += _language.Length; // Language
  62. return capacity;
  63. }
  64. }
  65. #endif
  66. /// <summary>
  67. /// Initializes a new instance of the <see cref="ChannelOpenFailureMessage"/> class.
  68. /// </summary>
  69. public ChannelOpenFailureMessage()
  70. {
  71. }
  72. /// <summary>
  73. /// Initializes a new instance of the <see cref="ChannelOpenFailureMessage"/> class.
  74. /// </summary>
  75. /// <param name="localChannelNumber">The local channel number.</param>
  76. /// <param name="description">The description.</param>
  77. /// <param name="reasonCode">The reason code.</param>
  78. public ChannelOpenFailureMessage(uint localChannelNumber, string description, uint reasonCode)
  79. : this(localChannelNumber, description, reasonCode, "en")
  80. {
  81. }
  82. /// <summary>
  83. /// Initializes a new instance of the <see cref="ChannelOpenFailureMessage"/> class.
  84. /// </summary>
  85. /// <param name="localChannelNumber">The local channel number.</param>
  86. /// <param name="description">The description.</param>
  87. /// <param name="reasonCode">The reason code.</param>
  88. /// <param name="language">The language (RFC3066).</param>
  89. public ChannelOpenFailureMessage(uint localChannelNumber, string description, uint reasonCode, string language)
  90. : base(localChannelNumber)
  91. {
  92. Description = description;
  93. ReasonCode = reasonCode;
  94. Language = language;
  95. }
  96. /// <summary>
  97. /// Called when type specific data need to be loaded.
  98. /// </summary>
  99. protected override void LoadData()
  100. {
  101. base.LoadData();
  102. ReasonCode = ReadUInt32();
  103. #if TUNING
  104. _description = ReadBinary();
  105. _language = ReadBinary();
  106. #else
  107. Description = ReadString();
  108. Language = ReadString();
  109. #endif
  110. }
  111. /// <summary>
  112. /// Called when type specific data need to be saved.
  113. /// </summary>
  114. protected override void SaveData()
  115. {
  116. base.SaveData();
  117. Write(ReasonCode);
  118. #if TUNING
  119. WriteBinaryString(_description);
  120. WriteBinaryString(_language);
  121. #else
  122. Write(Description ?? string.Empty);
  123. Write(Language ?? "en");
  124. #endif
  125. }
  126. }
  127. }