HeartbeatExtension.cs 1.4 KB

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