github.com/pion/webrtc/v3@v3.2.24/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  	// DataChannelStateConnecting indicates that the data channel is being
    11  	// established. This is the initial state of DataChannel, whether created
    12  	// with CreateDataChannel, or dispatched as a part of an DataChannelEvent.
    13  	DataChannelStateConnecting DataChannelState = iota + 1
    14  
    15  	// DataChannelStateOpen indicates that the underlying data transport is
    16  	// established and communication is possible.
    17  	DataChannelStateOpen
    18  
    19  	// DataChannelStateClosing indicates that the procedure to close down the
    20  	// underlying data transport has started.
    21  	DataChannelStateClosing
    22  
    23  	// DataChannelStateClosed indicates that the underlying data transport
    24  	// has been closed or could not be established.
    25  	DataChannelStateClosed
    26  )
    27  
    28  // This is done this way because of a linter.
    29  const (
    30  	dataChannelStateConnectingStr = "connecting"
    31  	dataChannelStateOpenStr       = "open"
    32  	dataChannelStateClosingStr    = "closing"
    33  	dataChannelStateClosedStr     = "closed"
    34  )
    35  
    36  func newDataChannelState(raw string) DataChannelState {
    37  	switch raw {
    38  	case dataChannelStateConnectingStr:
    39  		return DataChannelStateConnecting
    40  	case dataChannelStateOpenStr:
    41  		return DataChannelStateOpen
    42  	case dataChannelStateClosingStr:
    43  		return DataChannelStateClosing
    44  	case dataChannelStateClosedStr:
    45  		return DataChannelStateClosed
    46  	default:
    47  		return DataChannelState(Unknown)
    48  	}
    49  }
    50  
    51  func (t DataChannelState) String() string {
    52  	switch t {
    53  	case DataChannelStateConnecting:
    54  		return dataChannelStateConnectingStr
    55  	case DataChannelStateOpen:
    56  		return dataChannelStateOpenStr
    57  	case DataChannelStateClosing:
    58  		return dataChannelStateClosingStr
    59  	case DataChannelStateClosed:
    60  		return dataChannelStateClosedStr
    61  	default:
    62  		return ErrUnknownType.Error()
    63  	}
    64  }
    65  
    66  // MarshalText implements encoding.TextMarshaler
    67  func (t DataChannelState) MarshalText() ([]byte, error) {
    68  	return []byte(t.String()), nil
    69  }
    70  
    71  // UnmarshalText implements encoding.TextUnmarshaler
    72  func (t *DataChannelState) UnmarshalText(b []byte) error {
    73  	*t = newDataChannelState(string(b))
    74  	return nil
    75  }