HeartbeatExtension.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.IO;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Tls
  6. {
  7. public sealed class HeartbeatExtension
  8. {
  9. private readonly short m_mode;
  10. public HeartbeatExtension(short mode)
  11. {
  12. if (!HeartbeatMode.IsValid(mode))
  13. throw new ArgumentException("not a valid HeartbeatMode value", "mode");
  14. this.m_mode = mode;
  15. }
  16. public short Mode
  17. {
  18. get { return m_mode; }
  19. }
  20. /// <summary>Encode this <see cref="HeartbeatExtension"/> to a <see cref="Stream"/>.</summary>
  21. /// <param name="output">the <see cref="Stream"/> to encode to.</param>
  22. /// <exception cref="IOException"/>
  23. public void Encode(Stream output)
  24. {
  25. TlsUtilities.WriteUint8(m_mode, output);
  26. }
  27. /// <summary>Parse a <see cref="HeartbeatExtension"/> from a <see cref="Stream"/>.</summary>
  28. /// <param name="input">the <see cref="Stream"/> to parse from.</param>
  29. /// <returns>a <see cref="HeartbeatExtension"/> object.</returns>
  30. /// <exception cref="IOException"/>
  31. public static HeartbeatExtension Parse(Stream input)
  32. {
  33. short mode = TlsUtilities.ReadUint8(input);
  34. if (!HeartbeatMode.IsValid(mode))
  35. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  36. return new HeartbeatExtension(mode);
  37. }
  38. }
  39. }
  40. #pragma warning restore
  41. #endif