HeartbeatExtension.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.Crypto.Tls
  6. {
  7. public class HeartbeatExtension
  8. {
  9. protected readonly byte mMode;
  10. public HeartbeatExtension(byte mode)
  11. {
  12. if (!HeartbeatMode.IsValid(mode))
  13. throw new ArgumentException("not a valid HeartbeatMode value", "mode");
  14. this.mMode = mode;
  15. }
  16. public virtual byte Mode
  17. {
  18. get { return mMode; }
  19. }
  20. /**
  21. * Encode this {@link HeartbeatExtension} to a {@link Stream}.
  22. *
  23. * @param output
  24. * the {@link Stream} to encode to.
  25. * @throws IOException
  26. */
  27. public virtual void Encode(Stream output)
  28. {
  29. TlsUtilities.WriteUint8(mMode, output);
  30. }
  31. /**
  32. * Parse a {@link HeartbeatExtension} from a {@link Stream}.
  33. *
  34. * @param input
  35. * the {@link Stream} to parse from.
  36. * @return a {@link HeartbeatExtension} object.
  37. * @throws IOException
  38. */
  39. public static HeartbeatExtension Parse(Stream input)
  40. {
  41. byte mode = TlsUtilities.ReadUint8(input);
  42. if (!HeartbeatMode.IsValid(mode))
  43. throw new TlsFatalAlert(AlertDescription.illegal_parameter);
  44. return new HeartbeatExtension(mode);
  45. }
  46. }
  47. }
  48. #pragma warning restore
  49. #endif