-
Notifications
You must be signed in to change notification settings - Fork 100
/
datastream_client.go
136 lines (116 loc) · 3.29 KB
/
datastream_client.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
package goquic
import (
"errors"
"io"
"net/http"
"time"
"github.com/oleiade/lane"
)
// implement OutgoingDataStreamCreator for Client
type SpdyClientSession struct {
conn *Conn
}
func (c *SpdyClientSession) CreateOutgoingDynamicStream() DataStreamProcessor {
return &SpdyClientStream{
conn: c.conn,
}
}
// implement DataStreamProcessor for Client
type SpdyClientStream struct {
// not goroutine-safe
conn *Conn
quicClientStream *QuicClientStream
pendingReads *lane.Deque
header http.Header
headerParsed bool
trailer http.Header
trailerParsed bool
// True readFinished means that this stream is half-closed on read-side
readFinished bool
// True writeFinished means that this stream is half-closed on write-side
writeFinished bool
// True when stream is closed fully
closed bool
}
func (stream *SpdyClientStream) OnInitialHeadersComplete(header http.Header, peerAddress string) {
stream.header = header
stream.headerParsed = true
}
func (stream *SpdyClientStream) OnTrailingHeadersComplete(header http.Header) {
stream.trailer = header
stream.trailerParsed = true
}
func (stream *SpdyClientStream) OnDataAvailable(data []byte, isClosed bool) {
stream.pendingReads.Append(data)
if isClosed {
stream.readFinished = true
}
}
// called on Stream closing. This may be called when both read/write side is closed or there is some error so that stream is force closed (in libquic side).
func (stream *SpdyClientStream) OnClose() {
stream.closed = true
}
func (stream *SpdyClientStream) Header() (http.Header, error) {
for stream.pendingReads.Empty() {
stream.conn.waitForEvents()
}
if stream.headerParsed {
return stream.header, nil
} else {
return http.Header{}, errors.New("Cannot read header")
}
}
func (stream *SpdyClientStream) Trailer() http.Header {
if !stream.closed {
for stream.pendingReads.Empty() {
stream.conn.waitForEvents()
}
}
if stream.trailerParsed {
return stream.trailer
} else {
return http.Header{}
}
}
func (stream *SpdyClientStream) Read(p []byte) (int, error) {
stream.conn.processEventsWithDeadline(time.Now()) // Process any pending events
// We made sure we've processed all events. So pendingReads.Empty() means that it is really empty
if stream.closed && stream.pendingReads.Empty() {
return 0, io.EOF
}
// Wait for body
for stream.pendingReads.Empty() {
stream.conn.waitForEvents()
if stream.closed && stream.pendingReads.Empty() {
return 0, io.EOF
}
}
buffer := stream.pendingReads.Shift().([]byte)
if len(p) < len(buffer) {
stream.pendingReads.Prepend(buffer[len(p):])
return copy(p, buffer[:len(p)]), nil
} else {
return copy(p, buffer), nil
}
}
func (stream *SpdyClientStream) WriteHeader(header http.Header, isBodyEmpty bool) {
stream.quicClientStream.WriteHeader(header, isBodyEmpty)
if isBodyEmpty {
stream.writeFinished = true
}
}
func (stream *SpdyClientStream) Write(buf []byte) (int, error) {
if stream.writeFinished {
return 0, errors.New("Write already finished")
}
stream.quicClientStream.WriteOrBufferData(buf, false)
return len(buf), nil
}
func (stream *SpdyClientStream) FinWrite() error {
if stream.writeFinished {
return errors.New("Write already finished")
}
stream.quicClientStream.WriteOrBufferData(nil, true)
stream.writeFinished = true
return nil
}