-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathstream.go
336 lines (304 loc) · 6.44 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package quic
import (
"io"
"net"
"os"
"sync"
"time"
)
// Stream provides asynchronous APIs to interact which a QUIC stream.
// All Stream functions must be used in a separated goroutine that is
// different to the connection callback.
// For example:
//
// func (handler) Serve(conn *quic.Conn, events []transport.Event) {
// for _, e := range events {
// switch e.Type {
// case transport.EventStreamOpen:
// st, err := conn.Stream(e.ID)
// ...
// go func(stream *quic.Stream) {
// // Working on the stream.
// }(st)
// }
// }
// }
//
// Stream implements net.Conn interface.
type Stream struct {
id uint64
conn *Conn
// Writing
wrMu sync.Mutex
wrCh chan io.Writer
wrDl deadlineTimer
// Reading
rdMu sync.Mutex
rdCh chan io.Reader
rdDl deadlineTimer
// Closing
clMu sync.Mutex
clCh chan error
closeOnce sync.Once
closeCh chan struct{}
// Due to asynchronous operators, application may not fully read data when the connection is closed.
// This stream needs to own the QUIC Stream in this case so that the application can continue reading.
closeErr error
closeRd io.Reader
}
var (
_ net.Conn = (*Stream)(nil)
)
func newStream(conn *Conn, id uint64) *Stream {
s := &Stream{
conn: conn,
id: id,
wrCh: make(chan io.Writer),
rdCh: make(chan io.Reader),
clCh: make(chan error),
closeCh: make(chan struct{}),
}
s.wrDl.init()
s.rdDl.init()
return s
}
// Write writes data to the connection stream.
// The function is blocked until all data are put into stream buffer or timeout.
func (s *Stream) Write(b []byte) (int, error) {
select {
case <-s.closeCh:
// This connection or stream is already closed.
return 0, s.closeErr
default:
s.wrMu.Lock()
defer s.wrMu.Unlock()
return s.writeLocked(b)
}
}
func (s *Stream) writeLocked(b []byte) (n int, err error) {
cmd := connCommand{
cmd: cmdStreamWrite,
id: s.id,
}
select {
case <-s.closeCh:
err = s.closeErr
return
case <-s.wrDl.ch:
err = os.ErrDeadlineExceeded
return
case s.conn.cmdCh <- cmd:
// Waiting for writer
for {
select {
case <-s.closeCh:
err = s.closeErr
return
case <-s.wrDl.ch:
err = os.ErrDeadlineExceeded
return
case w := <-s.wrCh:
var m int
m, err = w.Write(b[n:])
s.wrCh <- nil // Done writing
n += m
if err != nil || n >= len(b) {
return
}
}
}
}
}
// Read reads data from the stream.
// The function is blocked until any stream data is available or timeout.
func (s *Stream) Read(b []byte) (int, error) {
s.rdMu.Lock()
defer s.rdMu.Unlock()
select {
case <-s.closeCh:
// This connection or stream is already closed.
return s.readClosed(b)
default:
return s.readLocked(b)
}
}
func (s *Stream) readLocked(b []byte) (n int, err error) {
cmd := connCommand{
cmd: cmdStreamRead,
id: s.id,
}
select {
case <-s.closeCh:
return s.readClosed(b)
case <-s.rdDl.ch:
err = os.ErrDeadlineExceeded
return
case s.conn.cmdCh <- cmd:
// Wait for reader
for {
select {
case <-s.closeCh:
if n > 0 {
return n, nil
}
return s.readClosed(b)
case <-s.rdDl.ch:
err = os.ErrDeadlineExceeded
return
case r := <-s.rdCh:
n, err = r.Read(b)
s.rdCh <- nil // Done reading
if err != nil || n > 0 || len(b) == 0 {
return
}
}
}
}
}
func (s *Stream) readClosed(b []byte) (int, error) {
if s.closeRd == nil {
return 0, s.closeErr
}
n, err := s.closeRd.Read(b)
if err == nil && n == 0 {
// Nothing else to read
err = s.closeErr
}
return n, err
}
// Close closes the sending part of the stream.
func (s *Stream) Close() error {
return s.close(cmdStreamClose, 0)
}
// CloseWrite terminates sending part of the stream.
func (s *Stream) CloseWrite(errorCode uint64) error {
return s.close(cmdStreamCloseWrite, errorCode)
}
// CloseRead terminates reading part of the stream.
func (s *Stream) CloseRead(errorCode uint64) error {
return s.close(cmdStreamCloseRead, errorCode)
}
// LocalAddr returns the local network address.
func (s *Stream) LocalAddr() net.Addr {
return s.conn.LocalAddr()
}
// RemoteAddr returns the remote network address.
func (s *Stream) RemoteAddr() net.Addr {
return s.conn.RemoteAddr()
}
// SetDeadline sets the read and write deadlines associated with the stream.
func (s *Stream) SetDeadline(t time.Time) error {
s.SetWriteDeadline(t)
s.SetReadDeadline(t)
return nil
}
// SetWriteDeadline sets the write deadline associated with the stream.
func (s *Stream) SetWriteDeadline(t time.Time) error {
s.wrMu.Lock()
s.wrDl.setDeadline(t)
s.wrMu.Unlock()
return nil
}
// SetReadDeadline sets the read deadline associated with the stream.
func (s *Stream) SetReadDeadline(t time.Time) error {
s.rdMu.Lock()
s.rdDl.setDeadline(t)
s.rdMu.Unlock()
return nil
}
func (s *Stream) close(comm command, errorCode uint64) error {
select {
case <-s.closeCh:
// This connection or stream is already closed.
return s.closeErr
default:
}
s.clMu.Lock()
defer s.clMu.Unlock()
cmd := connCommand{
cmd: comm,
id: s.id,
n: errorCode,
}
select {
case <-s.closeCh:
return s.closeErr
case s.conn.cmdCh <- cmd:
select {
case <-s.closeCh:
return s.closeErr
case err := <-s.clCh:
return err
}
}
}
func (s *Stream) sendWriter(w io.Writer) {
select {
case s.wrCh <- w:
<-s.wrCh // Wait
default:
}
}
func (s *Stream) sendReader(w io.Reader) {
select {
case s.rdCh <- w:
<-s.rdCh // Wait
default:
}
}
// sendCloseResult is called from Conn goroutine.
func (s *Stream) sendCloseResult(err error) {
select {
case <-s.closeCh:
case s.clCh <- err:
}
}
// setClosed is called from Conn goroutine.
func (s *Stream) setClosed(err error, rd io.Reader) {
s.closeOnce.Do(func() {
s.closeErr = err
s.closeRd = rd
close(s.closeCh)
})
}
type deadlineTimer struct {
tm *time.Timer
ch chan struct{}
}
func (s *deadlineTimer) init() {
s.ch = make(chan struct{})
}
func (s *deadlineTimer) setDeadline(t time.Time) {
if s.tm != nil && !s.tm.Stop() {
// Wait for the current timer callback to finish and close channel
<-s.ch
}
s.tm = nil
closed := false
select {
case <-s.ch:
closed = true
default:
}
// Time is zero, no deadline
if t.IsZero() {
if closed {
s.ch = make(chan struct{})
}
return
}
// Time in the future, set up a timer
if d := time.Until(t); d > 0 {
if closed {
s.ch = make(chan struct{})
}
s.tm = time.AfterFunc(d, func() {
close(s.ch)
})
return
}
// Time in the past, close immediately
if !closed {
close(s.ch)
}
}