Debian Codegraph @

proto/frame/rst.go

raw
1 package frame
2
3 import "io"
4
5 const (
6 rstBodySize = 4
7 rstFrameSize = headerSize + rstBodySize
8 )
9
10 // RsStreamRst is a STREAM_RST frame that is read from a transport
11 type RStreamRst struct {
12 Header
13 body [rstBodySize]byte
14 }
15
16 func (f *RStreamRst) readFrom(d deserializer) (err error) {
17 if f.Length() != rstBodySize {
18 return protoError("STREAM_RST length must be %d, got %d", rstBodySize, f.Length())
19 }
20
21 if _, err = io.ReadFull(d, f.body[:]); err != nil {
22 return
23 }
24
25 return
26 }
27
28 func (f *RStreamRst) ErrorCode() ErrorCode {
29 return ErrorCode(order.Uint32(f.body[0:]))
30 }
31
32 // WStreamRst is a STREAM_RST frame that can be written, it terminate a stream ungracefully
33 type WStreamRst struct {
34 Header
35 all [rstFrameSize]byte
36 }
37
38 func NewWStreamRst() (f *WStreamRst) {
39 f = new(WStreamRst)
40 f.Header = Header(f.all[:headerSize])
41 return
42 }
43
44 func (f *WStreamRst) writeTo(s serializer) (err error) {
45 _, err = s.Write(f.all[:])
46 return
47 }
48
49 func (f *WStreamRst) Set(streamId StreamId, errorCode ErrorCode) (err error) {
50 if err = f.Header.SetAll(TypeStreamRst, rstBodySize, streamId, 0); err != nil {
51 return
52 }
53
54 if err = validRstErrorCode(errorCode); err != nil {
55 return
56 }
57
58 order.PutUint32(f.all[headerSize:], uint32(errorCode))
59 return
60 }
61
62 func validRstErrorCode(errorCode ErrorCode) error {
63 if errorCode >= NoSuchError {
64 return protoError("Invalid error code %d for STREAM_RST", errorCode)
65 }
66 return nil
67 }