File: rst.go

package info (click to toggle)
golang-github-inconshreveable-muxado 0.0~git20140312.0.f693c7e-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 256 kB
  • ctags: 374
  • sloc: makefile: 2
file content (67 lines) | stat: -rw-r--r-- 1,410 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package frame

import "io"

const (
	rstBodySize  = 4
	rstFrameSize = headerSize + rstBodySize
)

// RsStreamRst is a STREAM_RST frame that is read from a transport
type RStreamRst struct {
	Header
	body [rstBodySize]byte
}

func (f *RStreamRst) readFrom(d deserializer) (err error) {
	if f.Length() != rstBodySize {
		return protoError("STREAM_RST length must be %d, got %d", rstBodySize, f.Length())
	}

	if _, err = io.ReadFull(d, f.body[:]); err != nil {
		return
	}

	return
}

func (f *RStreamRst) ErrorCode() ErrorCode {
	return ErrorCode(order.Uint32(f.body[0:]))
}

// WStreamRst is a STREAM_RST frame that can be written, it terminate a stream ungracefully
type WStreamRst struct {
	Header
	all [rstFrameSize]byte
}

func NewWStreamRst() (f *WStreamRst) {
	f = new(WStreamRst)
	f.Header = Header(f.all[:headerSize])
	return
}

func (f *WStreamRst) writeTo(s serializer) (err error) {
	_, err = s.Write(f.all[:])
	return
}

func (f *WStreamRst) Set(streamId StreamId, errorCode ErrorCode) (err error) {
	if err = f.Header.SetAll(TypeStreamRst, rstBodySize, streamId, 0); err != nil {
		return
	}

	if err = validRstErrorCode(errorCode); err != nil {
		return
	}

	order.PutUint32(f.all[headerSize:], uint32(errorCode))
	return
}

func validRstErrorCode(errorCode ErrorCode) error {
	if errorCode >= NoSuchError {
		return protoError("Invalid error code %d for STREAM_RST", errorCode)
	}
	return nil
}