Skip to content

Commit

Permalink
Return useful error for incorrect channel type
Browse files Browse the repository at this point in the history
Before (Wrapped for linter)
```
invalid Message Type &{%!s(datachannel.ChannelType=0)
%!s(uint16=256) %!s(uint32=0) initial_data_channel }
```

Now (Wrapped for linter)
```
expected and actual message type does not match, wanted ACK
got Open ChannelType(PartialReliableRexmitUnordered) Priority(500)
ReliabilityParameter(750) Label(Sean) Protocol(Test)
```

Resolves #125
Resolves pion/webrtc#2258
  • Loading branch information
Sean-Der committed Mar 28, 2024
1 parent f56395b commit 9449b50
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion datachannel.go
Expand Up @@ -287,7 +287,7 @@ func (c *DataChannel) handleDCEP(data []byte) error {
case *channelAck:
c.onOpenComplete()
default:
return fmt.Errorf("%w %v", ErrInvalidMessageType, msg)
return fmt.Errorf("%w, wanted ACK got %v", ErrUnexpectedDataChannelType, msg)
}

return nil
Expand Down
1 change: 1 addition & 0 deletions message.go
Expand Up @@ -11,6 +11,7 @@ import (
type message interface {
Marshal() ([]byte, error)
Unmarshal([]byte) error
String() string
}

// messageType is the first byte in a DataChannel message that specifies type
Expand Down
4 changes: 4 additions & 0 deletions message_channel_ack.go
Expand Up @@ -23,3 +23,7 @@ func (c *channelAck) Unmarshal(_ []byte) error {
// Message type already checked in Parse and there is no further data
return nil
}

func (c channelAck) String() string {
return "ACK"
}
21 changes: 21 additions & 0 deletions message_channel_open.go
Expand Up @@ -75,6 +75,23 @@ const (
ChannelTypePartialReliableTimedUnordered ChannelType = 0x82
)

func (c ChannelType) String() string {
switch c {
case ChannelTypeReliable:
case ChannelTypeReliableUnordered:
return "ReliableUnordered"
case ChannelTypePartialReliableRexmit:
return "PartialReliableRexmit"
case ChannelTypePartialReliableRexmitUnordered:
return "PartialReliableRexmitUnordered"
case ChannelTypePartialReliableTimed:
return "PartialReliableTimed"
case ChannelTypePartialReliableTimedUnordered:
return "PartialReliableTimedUnordered"
}
return "Unknown"
}

// ChannelPriority enums
const (
ChannelPriorityBelowNormal uint16 = 128
Expand Down Expand Up @@ -124,3 +141,7 @@ func (c *channelOpen) Unmarshal(raw []byte) error {
c.Protocol = raw[channelOpenHeaderLength+labelLength : channelOpenHeaderLength+labelLength+protocolLength]
return nil
}

func (c channelOpen) String() string {
return fmt.Sprintf("Open ChannelType(%s) Priority(%v) ReliabilityParameter(%d) Label(%s) Protocol(%s)", c.ChannelType, c.Priority, c.ReliabilityParameter, string(c.Label), string(c.Protocol))
}
14 changes: 14 additions & 0 deletions message_test.go
Expand Up @@ -95,3 +95,17 @@ func TestChannelAckUnmarshal(t *testing.T) {
_, ok := msgUncast.(*channelAck)
assert.True(t, ok, "Failed to cast to ChannelAck")
}

func TestChannelString(t *testing.T) {
channelString := channelOpen{
ChannelType: ChannelTypeReliable,
Priority: 0,
ReliabilityParameter: 0,

Label: []byte("foo"),
Protocol: []byte("bar"),
}.String()

assert.Equal(t, channelString, "Open ChannelType(Unknown) Priority(0) ReliabilityParameter(0) Label(foo) Protocol(bar)")
assert.Equal(t, channelAck{}.String(), "ACK")
}

0 comments on commit 9449b50

Please sign in to comment.