-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
254 lines (229 loc) · 5.55 KB
/
writer.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package framestream
import (
"errors"
"io"
"net"
"sync"
"time"
)
type Writer struct {
*RW
contentTypes []ContentType // read-only data
data chan []byte
stop chan struct{} // signal chan, closes when writer-owner says data writes should stop
err error
errLock sync.RWMutex
finish chan struct{} // used to signal close of bidi network connections
done chan struct{} // closes when either .stop or .finish closes
}
func (r *Writer) Error() error {
r.errLock.RLock()
defer r.errLock.RUnlock()
return r.err
}
func (r *Writer) setError(err error) {
if err != nil {
r.errLock.Lock()
defer r.errLock.Unlock()
if r.err == nil {
r.err = err
}
}
}
func (w *Writer) Output() chan<- []byte {
return w.data
}
func (w *Writer) Done() <-chan struct{} {
return w.done
}
type writerStateFn func(*Writer) writerStateFn
func NewWriter(rw *RW, types []ContentType) *Writer {
writer := &Writer{
RW: rw,
data: make(chan []byte, 2),
stop: make(chan struct{}),
finish: make(chan struct{}),
done: make(chan struct{}),
}
for _, t := range types {
switch l := len(t); {
case l == 0:
continue
case l <= ControlFieldContentTypeMaxLength:
writer.contentTypes = append(writer.contentTypes, t)
default:
// TODO(jdef) original implementation generates an error, we'll just ignore for now
}
}
go func() {
defer close(writer.done)
select {
case <-writer.stop:
case <-writer.finish:
}
}()
return writer
}
func (w *Writer) Run(shouldStop <-chan struct{}) {
go func() {
// TODO(jdef) probably want something better here later
defer close(w.stop)
<-shouldStop
}()
st := writerStateOpening
for {
next := st(w)
if next == nil {
break
}
st = next
}
}
func writerStateFailed(w *Writer) writerStateFn {
//TODO(jdef) implement some cleanup here?
return nil
}
func writerStateClosed(w *Writer) writerStateFn {
//TODO(jdef) implement some cleanup here
return nil
}
func writerStateClosing(w *Writer) writerStateFn {
err := w.writeControl(ControlTypeStop, nil)
w.setError(err)
if w.ReadCloser != nil {
// wait for finish before terminating the connection
t := time.NewTimer(15 * time.Second)
select {
case <-t.C:
// timed out waiting got FINISH, proceed to close the connection
case <-w.finish:
// FINISH control frame received
t.Stop()
}
}
err = w.WriteCloser.Close()
w.setError(err)
return writerStateClosed
}
// monitorConnection is called for bidi connections and watches for the client
// to disappear. this func blocks until the connection closes or generates a
// a non-timeout error (possibly protocol related).
func (w *Writer) monitorConnection(conn net.Conn) {
defer close(w.finish)
var err error
for {
conn.SetReadDeadline(time.Now().Add(1 * time.Second))
// test the connection by attempting to read a FINISH control frame.
// we should only see this if we've initiated a close by sending a
// STOP, but it doesn't hurt to start watching for this before then
// because we're really not expecting the client to send anything
// else at this point. we also don't have to write anything special
// back to the client if we receive it because the client closes their
// end of the connection upon sending.
_, err = w.readControl(ControlTypeFinish)
if err == io.EOF {
// connection closed
} else if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
// timeout
time.Sleep(5 * time.Second) // TODO(jdef) extract constant
continue
}
break
}
w.setError(err)
}
func writerStateOpen(w *Writer) writerStateFn {
if w.ReadCloser != nil {
if conn, ok := w.ReadCloser.(net.Conn); ok {
go w.monitorConnection(conn)
}
}
writeLoop:
for {
select {
case <-w.finish:
return writerStateClosing
case <-w.stop:
return writerStateClosing
case buf := <-w.data:
// we possibly won a tie, make sure that we're not stopped yet
select {
case <-w.finish:
return writerStateClosing
case <-w.stop:
return writerStateClosing
default:
x := len(buf)
err := write32(w, uint32(x))
if err == nil {
n, err := w.Write(buf)
if err == nil && n == len(buf) {
continue writeLoop
}
}
w.setError(err)
return writerStateFailed
}
}
}
}
func writerStateOpening(w *Writer) writerStateFn {
var err error
if w.ReadCloser != nil {
err = w.openBidi()
} else {
err = w.openUni()
}
w.setError(err)
if err != nil {
return writerStateFailed
}
return writerStateOpen
}
func (w *Writer) openBidi() error {
// write a READY frame
ready := &Control{
Type: ControlTypeReady,
ContentTypes: w.contentTypes,
}
err := w.writeControlFrame(ready)
if err != nil {
return err
}
// read ACCEPT frame and find matching content type
af, err := w.readControl(ControlTypeAccept)
if err != nil {
return err
}
match := true
var matchType ContentType
for _, t := range w.contentTypes {
if af.MatchFieldContentType(t) {
matchType = t
break
}
match = false
continue
}
if !match {
return errors.New("failed to find matching content-type in ACCEPT frame")
}
// send a START frame, indicate a matching control type (if any)
start := &Control{
Type: ControlTypeStart,
}
if matchType != nil {
start.ContentTypes = append(start.ContentTypes, matchType)
}
return w.writeControlFrame(start)
}
func (w *Writer) openUni() error {
// send a START frame, indicate a matching control type (if any)
start := &Control{
Type: ControlTypeStart,
}
if len(w.contentTypes) > 0 {
start.ContentTypes = append(start.ContentTypes, w.contentTypes[0])
}
return w.writeControlFrame(start)
}