Debian Codegraph @

proto/frame/syn.go

raw
1 package frame
2
3 import (
4 "fmt"
5 "io"
6 )
7
8 const (
9 maxSynBodySize = 8
10 maxSynFrameSize = headerSize + maxSynBodySize
11 )
12
13 type RStreamSyn struct {
14 Header
15 body [maxSynBodySize]byte
16 streamPriority StreamPriority
17 streamType StreamType
18 }
19
20 // StreamType returns the stream's defined type as specified by
21 // the remote endpoint
22 func (f *RStreamSyn) StreamType() StreamType {
23 return f.streamType
24 }
25
26 // StreamPriority returns the stream priority set on this frame
27 func (f *RStreamSyn) StreamPriority() StreamPriority {
28 return f.streamPriority
29 }
30
31 func (f *RStreamSyn) parseFields() error {
32 var length uint16 = 0
33 flags := f.Flags()
34
35 if flags.IsSet(flagStreamPriority) {
36 f.streamPriority = StreamPriority(order.Uint32(f.body[length : length+4]))
37 length += 4
38 } else {
39 f.streamPriority = 0
40 }
41
42 if flags.IsSet(flagStreamType) {
43 f.streamType = StreamType(order.Uint32(f.body[length : length+4]))
44 length += 4
45 } else {
46 f.streamType = 0
47 }
48
49 if length != f.Length() {
50 return fmt.Errorf("Expected length %d for flags %v, but got %v", length, flags, f.Length())
51 }
52
53 return nil
54 }
55
56 func (f *RStreamSyn) readFrom(d deserializer) (err error) {
57 if _, err = io.ReadFull(d, f.body[:f.Length()]); err != nil {
58 return
59 }
60
61 if err = f.parseFields(); err != nil {
62 return
63 }
64
65 return
66 }
67
68 type WStreamSyn struct {
69 Header
70 data [maxSynFrameSize]byte
71 length int
72 }
73
74 func (f *WStreamSyn) writeTo(s serializer) (err error) {
75 _, err = s.Write(f.data[:headerSize+f.Length()])
76 return
77 }
78
79 func (f *WStreamSyn) Set(streamId StreamId, streamPriority StreamPriority, streamType StreamType, fin bool) (err error) {
80 var (
81 flags flagsType
82 length int = 0
83 )
84
85 // set fin bit
86 if fin {
87 flags.Set(flagFin)
88 }
89
90 if streamPriority != 0 {
91 if streamPriority > priorityMask {
92 err = protoError("Priority %d is out of range", streamPriority)
93 return
94 }
95
96 flags.Set(flagStreamPriority)
97 start := headerSize + length
98 order.PutUint32(f.data[start:start+4], uint32(streamPriority))
99 length += 4
100 }
101
102 if streamType != 0 {
103 flags.Set(flagStreamType)
104 start := headerSize + length
105 order.PutUint32(f.data[start:start+4], uint32(streamType))
106 length += 4
107 }
108
109 // make the frame
110 if err = f.Header.SetAll(TypeStreamSyn, length, streamId, flags); err != nil {
111 return
112 }
113 return
114 }
115
116 func NewWStreamSyn() (f *WStreamSyn) {
117 f = new(WStreamSyn)
118 f.Header = Header(f.data[:headerSize])
119 return
120 }