-
Notifications
You must be signed in to change notification settings - Fork 37
/
frame.go
128 lines (112 loc) · 2.29 KB
/
frame.go
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package http2
import (
"strconv"
"sync"
)
type FrameType int8
func (ft FrameType) String() string {
switch ft {
case FrameData:
return "FrameData"
case FrameHeaders:
return "FrameHeaders"
case FramePriority:
return "FramePriority"
case FrameResetStream:
return "FrameResetStream"
case FrameSettings:
return "FrameSettings"
case FramePushPromise:
return "FramePushPromise"
case FramePing:
return "FramePing"
case FrameGoAway:
return "FrameGoAway"
case FrameWindowUpdate:
return "FrameWindowUpdate"
case FrameContinuation:
return "FrameContinuation"
}
return strconv.Itoa(int(ft))
}
type FrameFlags int8
// Has returns if `f` is in the frame flags or not.
func (flags FrameFlags) Has(f FrameFlags) bool {
return flags&f == f
}
// Add adds a flag to frame flags.
func (flags FrameFlags) Add(f FrameFlags) FrameFlags {
return flags | f
}
// Del deletes f from frame flags.
func (flags FrameFlags) Del(f FrameFlags) FrameFlags {
return flags ^ f
}
type Frame interface {
Type() FrameType
Reset()
Serialize(*FrameHeader)
Deserialize(*FrameHeader) error
}
var framePools = func() [FrameContinuation + 1]*sync.Pool {
var pools [FrameContinuation + 1]*sync.Pool
pools[FrameData] = &sync.Pool{
New: func() interface{} {
return &Data{}
},
}
pools[FrameHeaders] = &sync.Pool{
New: func() interface{} {
return &Headers{}
},
}
pools[FramePriority] = &sync.Pool{
New: func() interface{} {
return &Priority{}
},
}
pools[FrameResetStream] = &sync.Pool{
New: func() interface{} {
return &RstStream{}
},
}
pools[FrameSettings] = &sync.Pool{
New: func() interface{} {
return &Settings{}
},
}
pools[FramePushPromise] = &sync.Pool{
New: func() interface{} {
return &PushPromise{}
},
}
pools[FramePing] = &sync.Pool{
New: func() interface{} {
return &Ping{}
},
}
pools[FrameGoAway] = &sync.Pool{
New: func() interface{} {
return &GoAway{}
},
}
pools[FrameWindowUpdate] = &sync.Pool{
New: func() interface{} {
return &WindowUpdate{}
},
}
pools[FrameContinuation] = &sync.Pool{
New: func() interface{} {
return &Continuation{}
},
}
return pools
}()
func AcquireFrame(ftype FrameType) Frame {
fr := framePools[ftype].Get().(Frame)
fr.Reset()
return fr
}
func ReleaseFrame(fr Frame) {
framePools[fr.Type()].Put(fr)
}