Message.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Renci.SshNet.Common;
  4. using System.Globalization;
  5. namespace Renci.SshNet.Messages
  6. {
  7. /// <summary>
  8. /// Base class for all SSH protocol messages
  9. /// </summary>
  10. public abstract class Message : SshData
  11. {
  12. /// <summary>
  13. /// Gets the index that represents zero in current data type.
  14. /// </summary>
  15. /// <value>
  16. /// The index of the zero reader.
  17. /// </value>
  18. protected override int ZeroReaderIndex
  19. {
  20. get
  21. {
  22. return 1;
  23. }
  24. }
  25. /// <summary>
  26. /// Gets data bytes array
  27. /// </summary>
  28. /// <returns>Byte array representation of the message</returns>
  29. public override byte[] GetBytes()
  30. {
  31. var messageAttribute = GetType().GetCustomAttributes(typeof(MessageAttribute), true).SingleOrDefault() as MessageAttribute;
  32. if (messageAttribute == null)
  33. throw new SshException(string.Format(CultureInfo.CurrentCulture, "Type '{0}' is not a valid message type.", GetType().AssemblyQualifiedName));
  34. var data = new List<byte>(base.GetBytes());
  35. data.Insert(0, messageAttribute.Number);
  36. return data.ToArray();
  37. }
  38. /// <summary>
  39. /// Returns a <see cref="System.String"/> that represents this instance.
  40. /// </summary>
  41. /// <returns>
  42. /// A <see cref="System.String"/> that represents this instance.
  43. /// </returns>
  44. public override string ToString()
  45. {
  46. var messageAttribute = GetType().GetCustomAttributes(typeof(MessageAttribute), true).SingleOrDefault() as MessageAttribute;
  47. if (messageAttribute == null)
  48. return string.Format(CultureInfo.CurrentCulture, "'{0}' without Message attribute.", GetType().FullName);
  49. return messageAttribute.Name;
  50. }
  51. }
  52. }