MCPcopy Create free account
hub / github.com/pion/rtcp / write

Method write

packet_buffer.go:51–124  ·  view source on GitHub ↗

Writes the structure passed to into the buffer that PacketBuffer is initialized with. This function will modify the PacketBuffer.bytes slice to exclude those bytes that have been written into. nolint:gocognit,cyclop

(v any)

Source from the content-addressed store, hash-verified

49//
50//nolint:gocognit,cyclop
51func (b *packetBuffer) write(v any) error {
52 value := reflect.ValueOf(v)
53
54 // Indirect is safe to call on non-pointers, and
55 // will simply return the same value in such cases
56 value = reflect.Indirect(value)
57
58 switch value.Kind() {
59 case reflect.Uint8:
60 if len(b.bytes) < 1 {
61 return errWrongMarshalSize
62 }
63 if value.CanInterface() {
64 b.bytes[0] = byte(value.Uint()) //nolint:gosec // value.Kind() == reflect.Uint8 guarantees range
65 }
66 b.bytes = b.bytes[1:]
67 case reflect.Uint16:
68 if len(b.bytes) < 2 {
69 return errWrongMarshalSize
70 }
71 if value.CanInterface() {
72 binary.BigEndian.PutUint16(b.bytes, uint16(value.Uint())) //nolint:gosec // G115
73 }
74 b.bytes = b.bytes[2:]
75 case reflect.Uint32:
76 if len(b.bytes) < 4 {
77 return errWrongMarshalSize
78 }
79 if value.CanInterface() {
80 binary.BigEndian.PutUint32(b.bytes, uint32(value.Uint())) //nolint:gosec // G115
81 }
82 b.bytes = b.bytes[4:]
83 case reflect.Uint64:
84 if len(b.bytes) < 8 {
85 return errWrongMarshalSize
86 }
87 if value.CanInterface() {
88 binary.BigEndian.PutUint64(b.bytes, value.Uint())
89 }
90 b.bytes = b.bytes[8:]
91 case reflect.Slice:
92 for i := 0; i < value.Len(); i++ {
93 if value.Index(i).CanInterface() {
94 if err := b.write(value.Index(i).Interface()); err != nil {
95 return err
96 }
97 } else {
98 b.bytes = b.bytes[value.Index(i).Type().Size():]
99 }
100 }
101 case reflect.Struct:
102 for i := 0; i < value.NumField(); i++ {
103 encoding := value.Type().Field(i).Tag.Get("encoding")
104 if encoding == omit {
105 continue
106 }
107 if value.Field(i).CanInterface() {
108 if err := b.write(value.Field(i).Interface()); err != nil {

Callers 2

MarshalMethod · 0.95
TestWriteFunction · 0.95

Calls 2

TypeMethod · 0.80
LenMethod · 0.45

Tested by 1

TestWriteFunction · 0.76