dtlstransportstate.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package webrtc
  4. // DTLSTransportState indicates the DTLS transport establishment state.
  5. type DTLSTransportState int
  6. const (
  7. // DTLSTransportStateNew indicates that DTLS has not started negotiating
  8. // yet.
  9. DTLSTransportStateNew DTLSTransportState = iota + 1
  10. // DTLSTransportStateConnecting indicates that DTLS is in the process of
  11. // negotiating a secure connection and verifying the remote fingerprint.
  12. DTLSTransportStateConnecting
  13. // DTLSTransportStateConnected indicates that DTLS has completed
  14. // negotiation of a secure connection and verified the remote fingerprint.
  15. DTLSTransportStateConnected
  16. // DTLSTransportStateClosed indicates that the transport has been closed
  17. // intentionally as the result of receipt of a close_notify alert, or
  18. // calling close().
  19. DTLSTransportStateClosed
  20. // DTLSTransportStateFailed indicates that the transport has failed as
  21. // the result of an error (such as receipt of an error alert or failure to
  22. // validate the remote fingerprint).
  23. DTLSTransportStateFailed
  24. )
  25. // This is done this way because of a linter.
  26. const (
  27. dtlsTransportStateNewStr = "new"
  28. dtlsTransportStateConnectingStr = "connecting"
  29. dtlsTransportStateConnectedStr = "connected"
  30. dtlsTransportStateClosedStr = "closed"
  31. dtlsTransportStateFailedStr = "failed"
  32. )
  33. func newDTLSTransportState(raw string) DTLSTransportState {
  34. switch raw {
  35. case dtlsTransportStateNewStr:
  36. return DTLSTransportStateNew
  37. case dtlsTransportStateConnectingStr:
  38. return DTLSTransportStateConnecting
  39. case dtlsTransportStateConnectedStr:
  40. return DTLSTransportStateConnected
  41. case dtlsTransportStateClosedStr:
  42. return DTLSTransportStateClosed
  43. case dtlsTransportStateFailedStr:
  44. return DTLSTransportStateFailed
  45. default:
  46. return DTLSTransportState(Unknown)
  47. }
  48. }
  49. func (t DTLSTransportState) String() string {
  50. switch t {
  51. case DTLSTransportStateNew:
  52. return dtlsTransportStateNewStr
  53. case DTLSTransportStateConnecting:
  54. return dtlsTransportStateConnectingStr
  55. case DTLSTransportStateConnected:
  56. return dtlsTransportStateConnectedStr
  57. case DTLSTransportStateClosed:
  58. return dtlsTransportStateClosedStr
  59. case DTLSTransportStateFailed:
  60. return dtlsTransportStateFailedStr
  61. default:
  62. return ErrUnknownType.Error()
  63. }
  64. }
  65. // MarshalText implements encoding.TextMarshaler
  66. func (t DTLSTransportState) MarshalText() ([]byte, error) {
  67. return []byte(t.String()), nil
  68. }
  69. // UnmarshalText implements encoding.TextUnmarshaler
  70. func (t *DTLSTransportState) UnmarshalText(b []byte) error {
  71. *t = newDTLSTransportState(string(b))
  72. return nil
  73. }