github.com/pion/webrtc/v4@v4.0.1/datachannelstate.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package webrtc
     5  
     6  // DataChannelState indicates the state of a data channel.
     7  type DataChannelState int
     8  
     9  const (
    10  	// DataChannelStateUnknown is the enum's zero-value
    11  	DataChannelStateUnknown DataChannelState = iota
    12  
    13  	// DataChannelStateConnecting indicates that the data channel is being
    14  	// established. This is the initial state of DataChannel, whether created
    15  	// with CreateDataChannel, or dispatched as a part of an DataChannelEvent.
    16  	DataChannelStateConnecting
    17  
    18  	// DataChannelStateOpen indicates that the underlying data transport is
    19  	// established and communication is possible.
    20  	DataChannelStateOpen
    21  
    22  	// DataChannelStateClosing indicates that the procedure to close down the
    23  	// underlying data transport has started.
    24  	DataChannelStateClosing
    25  
    26  	// DataChannelStateClosed indicates that the underlying data transport
    27  	// has been closed or could not be established.
    28  	DataChannelStateClosed
    29  )
    30  
    31  // This is done this way because of a linter.
    32  const (
    33  	dataChannelStateConnectingStr = "connecting"
    34  	dataChannelStateOpenStr       = "open"
    35  	dataChannelStateClosingStr    = "closing"
    36  	dataChannelStateClosedStr     = "closed"
    37  )
    38  
    39  func newDataChannelState(raw string) DataChannelState {
    40  	switch raw {
    41  	case dataChannelStateConnectingStr:
    42  		return DataChannelStateConnecting
    43  	case dataChannelStateOpenStr:
    44  		return DataChannelStateOpen
    45  	case dataChannelStateClosingStr:
    46  		return DataChannelStateClosing
    47  	case dataChannelStateClosedStr:
    48  		return DataChannelStateClosed
    49  	default:
    50  		return DataChannelStateUnknown
    51  	}
    52  }
    53  
    54  func (t DataChannelState) String() string {
    55  	switch t {
    56  	case DataChannelStateConnecting:
    57  		return dataChannelStateConnectingStr
    58  	case DataChannelStateOpen:
    59  		return dataChannelStateOpenStr
    60  	case DataChannelStateClosing:
    61  		return dataChannelStateClosingStr
    62  	case DataChannelStateClosed:
    63  		return dataChannelStateClosedStr
    64  	default:
    65  		return ErrUnknownType.Error()
    66  	}
    67  }
    68  
    69  // MarshalText implements encoding.TextMarshaler
    70  func (t DataChannelState) MarshalText() ([]byte, error) {
    71  	return []byte(t.String()), nil
    72  }
    73  
    74  // UnmarshalText implements encoding.TextUnmarshaler
    75  func (t *DataChannelState) UnmarshalText(b []byte) error {
    76  	*t = newDataChannelState(string(b))
    77  	return nil
    78  }