-
Notifications
You must be signed in to change notification settings - Fork 4
/
stream.go
170 lines (147 loc) · 4.12 KB
/
stream.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package http2
import (
"github.com/Jxck/hpack"
. "github.com/Jxck/http2/frame"
. "github.com/Jxck/logger"
"log"
"net/http"
)
func init() {
log.SetFlags(log.Lshortfile)
}
type Stream struct {
ID uint32
State State
Window *Window
ReadChan chan Frame
WriteChan chan Frame
Settings map[SettingsID]int32
PeerSettings map[SettingsID]int32
HpackContext *hpack.Context
CallBack CallBack
Bucket *Bucket
Closed bool
}
type Bucket struct {
Headers http.Header
Body *Body
}
func NewBucket() *Bucket {
return &Bucket{
Headers: make(http.Header),
Body: new(Body),
}
}
type CallBack func(stream *Stream)
func NewStream(id uint32, writeChan chan Frame, settings, peerSettings map[SettingsID]int32, hpackContext *hpack.Context, callback CallBack) *Stream {
stream := &Stream{
ID: id,
State: IDLE,
Window: NewWindow(settings[SETTINGS_INITIAL_WINDOW_SIZE], peerSettings[SETTINGS_INITIAL_WINDOW_SIZE]),
ReadChan: make(chan Frame),
WriteChan: writeChan,
Settings: settings,
PeerSettings: peerSettings,
HpackContext: hpackContext,
CallBack: callback,
Bucket: NewBucket(),
Closed: false,
}
go stream.ReadLoop()
return stream
}
func (stream *Stream) Read(f Frame) {
Debug("stream (%d) recv (%v)", stream.ID, f.Header().Type)
switch frame := f.(type) {
case *HeadersFrame:
// Decode Headers
header := stream.DecodeHeader(frame.HeaderBlockFragment)
frame.Headers = header
for name, values := range header {
for _, value := range values {
stream.Bucket.Headers.Add(name, value)
}
}
if frame.Header().Flags&END_STREAM == END_STREAM {
go stream.CallBack(stream)
}
case *DataFrame:
length := int32(frame.Header().Length)
stream.WindowUpdate(length)
_, err := stream.Bucket.Body.Write(frame.Data)
if err != nil {
Fatal("%v", err)
}
if frame.Header().Flags&END_STREAM == END_STREAM {
stream.CallBack(stream)
}
case *RstStreamFrame:
Debug("close stream by RST_STREAM")
Error("RST_STREAM(%v)", frame.ErrorCode)
stream.Close()
case *PingFrame:
Debug("response to PING")
pong := NewPingFrame(ACK, stream.ID, frame.OpaqueData)
stream.Write(pong)
case *WindowUpdateFrame:
Info("Window Update %d byte stream(%v)", frame.WindowSizeIncrement, stream.ID)
stream.Window.UpdatePeer(int32(frame.WindowSizeIncrement))
case *ContinuationFrame:
// Decode Headers
header := stream.DecodeHeader(frame.HeaderBlockFragment)
frame.Headers = header
for name, values := range header {
for _, value := range values {
stream.Bucket.Headers.Add(name, value)
}
}
if frame.Header().Flags&END_STREAM == END_STREAM {
go stream.CallBack(stream)
}
}
}
func (stream *Stream) ReadLoop() {
Debug("start stream (%d) ReadLoop()", stream.ID)
for f := range stream.ReadChan {
stream.Read(f)
}
Debug("stop stream (%d) ReadLoop()", stream.ID)
}
func (stream *Stream) Write(frame Frame) {
Trace("stream.Write (%v)", frame)
if stream.Closed {
return
}
stream.ChangeState(frame, SEND)
stream.WriteChan <- frame
}
func (stream *Stream) WindowUpdate(length int32) {
Debug("stream(%d) window update %d byte", stream.ID, length)
// update する必要があればそれが返ってくる
update := stream.Window.Consume(length)
// update があれば WindowUpdate を送る
if update > 0 {
stream.Write(NewWindowUpdateFrame(stream.ID, uint32(update)))
stream.Window.Update(update)
}
}
func (stream *Stream) Close() {
Debug("stream(%d) Close()", stream.ID)
// stream.WriteChan は conn.WriteChan であり
// conn の方で close するので
// ここでは close しない
stream.Closed = true
Info("close stream(%v).ReadChan", stream.ID)
close(stream.ReadChan)
}
// Encode Header using HPACK
func (stream *Stream) EncodeHeader(header http.Header) []byte {
headerList := hpack.ToHeaderList(header)
Trace("sending header list %s", headerList)
return stream.HpackContext.Encode(*headerList)
}
// Decode Header using HPACK
func (stream *Stream) DecodeHeader(headerBlockFragment []byte) http.Header {
stream.HpackContext.Decode(headerBlockFragment)
return stream.HpackContext.ES.ToHeader()
}