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 f55406a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion datachannel.go
Original file line number Diff line number Diff line change
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)

Check warning on line 290 in datachannel.go

View check run for this annotation

Codecov / codecov/patch

datachannel.go#L290

Added line #L290 was not covered by tests
}

return nil
Expand Down
1 change: 1 addition & 0 deletions message.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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"

Check warning on line 28 in message_channel_ack.go

View check run for this annotation

Codecov / codecov/patch

message_channel_ack.go#L27-L28

Added lines #L27 - L28 were not covered by tests
}
21 changes: 21 additions & 0 deletions message_channel_open.go
Original file line number Diff line number Diff line change
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"

Check warning on line 90 in message_channel_open.go

View check run for this annotation

Codecov / codecov/patch

message_channel_open.go#L78-L90

Added lines #L78 - L90 were not covered by tests
}
return "Unknown"

Check warning on line 92 in message_channel_open.go

View check run for this annotation

Codecov / codecov/patch

message_channel_open.go#L92

Added line #L92 was not covered by tests
}

// 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) \n", c.ChannelType, c.Priority, c.ReliabilityParameter, string(c.Label), string(c.Protocol))

Check warning on line 146 in message_channel_open.go

View check run for this annotation

Codecov / codecov/patch

message_channel_open.go#L145-L146

Added lines #L145 - L146 were not covered by tests
}

0 comments on commit f55406a

Please sign in to comment.