Debian Codegraph @

Control.go

raw symbols SCIP
1 package framestream
2
3 import (
4 "bufio"
5 "bytes"
6 "encoding/binary"
7 "io"
8 )
9
10 const CONTROL_ACCEPT = 0x01
11 const CONTROL_START = 0x02
12 const CONTROL_STOP = 0x03
13 const CONTROL_READY = 0x04
14 const CONTROL_FINISH = 0x05
15
16 const CONTROL_FIELD_CONTENT_TYPE = 0x01
17
18 const CONTROL_FRAME_LENGTH_MAX = 512
19
20 type ControlFrame struct {
21 ControlType uint32
22 ContentTypes [][]byte
23 }
24
25 var ControlStart = ControlFrame{ControlType: CONTROL_START}
26 var ControlStop = ControlFrame{ControlType: CONTROL_STOP}
27 var ControlReady = ControlFrame{ControlType: CONTROL_READY}
28 var ControlAccept = ControlFrame{ControlType: CONTROL_ACCEPT}
29 var ControlFinish = ControlFrame{ControlType: CONTROL_FINISH}
30
31 func (c *ControlFrame) Encode(w io.Writer) (err error) {
32 var buf bytes.Buffer
33 err = binary.Write(&buf, binary.BigEndian, c.ControlType)
34 if err != nil {
35 return
36 }
37 for _, ctype := range c.ContentTypes {
38 err = binary.Write(&buf, binary.BigEndian, uint32(CONTROL_FIELD_CONTENT_TYPE))
39 if err != nil {
40 return
41 }
42
43 err = binary.Write(&buf, binary.BigEndian, uint32(len(ctype)))
44 if err != nil {
45 return
46 }
47
48 _, err = buf.Write(ctype)
49 if err != nil {
50 return
51 }
52 }
53
54 err = binary.Write(w, binary.BigEndian, uint32(0))
55 if err != nil {
56 return
57 }
58 err = binary.Write(w, binary.BigEndian, uint32(buf.Len()))
59 if err != nil {
60 return
61 }
62 _, err = buf.WriteTo(w)
63 return
64 }
65
66 func (c *ControlFrame) EncodeFlush(w *bufio.Writer) error {
67 if err := c.Encode(w); err != nil {
68 return err
69 }
70 return w.Flush()
71 }
72
73 func (c *ControlFrame) Decode(r io.Reader) (err error) {
74 var cflen uint32
75 err = binary.Read(r, binary.BigEndian, &cflen)
76 if err != nil {
77 return
78 }
79
80 if cflen > CONTROL_FRAME_LENGTH_MAX {
81 return ErrDecode
82 }
83
84 if cflen < 4 {
85 return ErrDecode
86 }
87
88 err = binary.Read(r, binary.BigEndian, &c.ControlType)
89 if err != nil {
90 return
91 }
92
93 cflen -= 4
94 if cflen > 0 {
95 cfields := make([]byte, int(cflen))
96 _, err = io.ReadFull(r, cfields)
97 if err != nil {
98 return
99 }
100
101 for len(cfields) > 8 {
102 cftype := binary.BigEndian.Uint32(cfields[:4])
103 cfields = cfields[4:]
104 if cftype != CONTROL_FIELD_CONTENT_TYPE {
105 return ErrDecode
106 }
107
108 cflen := int(binary.BigEndian.Uint32(cfields[:4]))
109 cfields = cfields[4:]
110 if cflen > len(cfields) {
111 return ErrDecode
112 }
113
114 c.ContentTypes = append(c.ContentTypes, cfields[:cflen])
115 cfields = cfields[cflen:]
116 }
117
118 if len(cfields) > 0 {
119 return ErrDecode
120 }
121 }
122 return
123 }
124
125 func (c *ControlFrame) DecodeEscape(r io.Reader) error {
126 var zero uint32
127 err := binary.Read(r, binary.BigEndian, &zero)
128 if err != nil {
129 return err
130 }
131 if zero != 0 {
132 return ErrDecode
133 }
134 return c.Decode(r)
135 }
136
137 func (c *ControlFrame) DecodeTypeEscape(r io.Reader, ctype uint32) error {
138 err := c.DecodeEscape(r)
139 if err != nil {
140 return err
141 }
142
143 if ctype != c.ControlType {
144 return ErrDecode
145 }
146
147 return nil
148 }
149
150 // ChooseContentType selects a content type from the ControlFrame which
151 // also exists in the supplied ctypes. Preference is given to values occurring
152 // earliest in ctypes.
153 //
154 // ChooseContentType returns the chosen content type, which may be nil, and
155 // a bool value indicating whether a matching type was found.
156 //
157 // If either the ControlFrame types or ctypes is empty, ChooseContentType
158 // returns nil as a matching content type.
159 func (c *ControlFrame) ChooseContentType(ctypes [][]byte) (typ []byte, found bool) {
160 if c.ContentTypes == nil || ctypes == nil {
161 return nil, true
162 }
163 tm := make(map[string]bool)
164 for _, cfctype := range c.ContentTypes {
165 tm[string(cfctype)] = true
166 }
167 for _, ctype := range ctypes {
168 if tm[string(ctype)] {
169 return ctype, true
170 }
171 }
172 return nil, false
173 }
174
175 func (c *ControlFrame) MatchContentType(ctype []byte) bool {
176 for _, cfctype := range c.ContentTypes {
177 if bytes.Compare(ctype, cfctype) == 0 {
178 return true
179 }
180 }
181 return len(c.ContentTypes) == 0
182 }
183
184 func (c *ControlFrame) SetContentTypes(ctypes [][]byte) {
185 c.ContentTypes = ctypes
186 }
187
188 func (c *ControlFrame) SetContentType(ctype []byte) {
189 if ctype != nil {
190 c.SetContentTypes([][]byte{ctype})
191 } else {
192 c.ContentTypes = nil
193 }
194 }