package relayproto
import "bytes"
type Datagrams struct {
Ecn EcnCodepoint
SegmentSize uint16
Contents []byte
}
func DatagramsFromBytes (b []byte ) Datagrams {
return Datagrams {Contents : bytes .Clone (b )}
}
func (d Datagrams ) isBatch () bool { return d .SegmentSize != 0 }
func (d Datagrams ) appendTo (dst []byte ) []byte {
dst = append (dst , byte (d .Ecn ))
if d .SegmentSize != 0 {
dst = append (dst , byte (d .SegmentSize >>8 ), byte (d .SegmentSize ))
}
return append (dst , d .Contents ...)
}
func (d Datagrams ) encodedLen () int {
n := 1 + len (d .Contents )
if d .SegmentSize != 0 {
n += 2
}
return n
}
func datagramsFromBytes(b []byte , isBatch bool ) (Datagrams , error ) {
return datagramsFromBytesCopy (b , isBatch , true )
}
func datagramsFromBytesNoCopy(b []byte , isBatch bool ) (Datagrams , error ) {
return datagramsFromBytesCopy (b , isBatch , false )
}
func datagramsFromBytesCopy(b []byte , isBatch , copyContents bool ) (Datagrams , error ) {
if isBatch {
if len (b ) < 3 {
return Datagrams {}, ErrInvalidFrame
}
} else if len (b ) < 1 {
return Datagrams {}, ErrInvalidFrame
}
ecn , _ := ecnFromBits (b [0 ])
b = b [1 :]
var segSize uint16
if isBatch {
segSize = uint16 (b [0 ])<<8 | uint16 (b [1 ])
b = b [2 :]
}
if copyContents {
b = bytes .Clone (b )
}
return Datagrams {Ecn : ecn , SegmentSize : segSize , Contents : b }, nil
}
The pages are generated with Golds v0.8.4 . (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 .