package datachannel
import (
"encoding/binary"
"fmt"
)
type channelOpen struct {
ChannelType ChannelType
Priority uint16
ReliabilityParameter uint32
Label []byte
Protocol []byte
}
const (
channelOpenHeaderLength = 12
)
type ChannelType byte
const (
ChannelTypeReliable ChannelType = 0x00
ChannelTypeReliableUnordered ChannelType = 0x80
ChannelTypePartialReliableRexmit ChannelType = 0x01
ChannelTypePartialReliableRexmitUnordered ChannelType = 0x81
ChannelTypePartialReliableTimed ChannelType = 0x02
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"
}
const (
ChannelPriorityBelowNormal uint16 = 128
ChannelPriorityNormal uint16 = 256
ChannelPriorityHigh uint16 = 512
ChannelPriorityExtraHigh uint16 = 1024
)
func (c *channelOpen ) Marshal () ([]byte , error ) {
labelLength := len (c .Label )
protocolLength := len (c .Protocol )
totalLen := channelOpenHeaderLength + labelLength + protocolLength
raw := make ([]byte , totalLen )
raw [0 ] = uint8 (dataChannelOpen )
raw [1 ] = byte (c .ChannelType )
binary .BigEndian .PutUint16 (raw [2 :], c .Priority )
binary .BigEndian .PutUint32 (raw [4 :], c .ReliabilityParameter )
binary .BigEndian .PutUint16 (raw [8 :], uint16 (labelLength ))
binary .BigEndian .PutUint16 (raw [10 :], uint16 (protocolLength ))
endLabel := channelOpenHeaderLength + labelLength
copy (raw [channelOpenHeaderLength :endLabel ], c .Label )
copy (raw [endLabel :endLabel +protocolLength ], c .Protocol )
return raw , nil
}
func (c *channelOpen ) Unmarshal (raw []byte ) error {
if len (raw ) < channelOpenHeaderLength {
return fmt .Errorf ("%w expected(%d) actual(%d)" , ErrExpectedAndActualLengthMismatch , channelOpenHeaderLength , len (raw ))
}
c .ChannelType = ChannelType (raw [1 ])
c .Priority = binary .BigEndian .Uint16 (raw [2 :])
c .ReliabilityParameter = binary .BigEndian .Uint32 (raw [4 :])
labelLength := binary .BigEndian .Uint16 (raw [8 :])
protocolLength := binary .BigEndian .Uint16 (raw [10 :])
if expectedLen := channelOpenHeaderLength + int (labelLength ) + int (protocolLength ); len (raw ) != expectedLen {
return fmt .Errorf ("%w expected(%d) actual(%d)" , ErrExpectedAndActualLengthMismatch , expectedLen , len (raw ))
}
c .Label = raw [channelOpenHeaderLength : channelOpenHeaderLength +labelLength ]
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 ))
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .