-
Notifications
You must be signed in to change notification settings - Fork 99
/
client_conn.go
224 lines (191 loc) · 4.86 KB
/
client_conn.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
package goquic
import (
"bytes"
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/oleiade/lane"
)
type Conn struct {
sync.Mutex
addr *net.UDPAddr
sock *net.UDPConn
quicClient *QuicClient
readChan chan UdpData
writer *ClientWriter
buffer bytes.Buffer
header http.Header
readQuitCh chan bool
writeQuitCh chan bool
closed bool
}
type errorString struct {
s string
}
func (e *errorString) Error() string {
return e.s
}
func (c *Conn) Close() (err error) {
if !c.closed {
c.quicClient.SendConnectionClosePacket()
c.readQuitCh <- true
c.closed = true
<-c.writeQuitCh // Wait until all writing (incluing QUIC_PEER_GOING_AWAY) has done
}
return c.quicClient.Close()
}
func (c *Conn) SetDeadline(t time.Time) (err error) {
// TODO(hodduc) not supported yet
return &errorString{"Not Supported"}
}
func (c *Conn) SetReadDeadline(t time.Time) (err error) {
// TODO(hodduc) not supported yet
return &errorString{"Not Supported"}
}
func (c *Conn) SetWriteDeadline(t time.Time) (err error) {
// TODO(hodduc) not supported yet
return &errorString{"Not Supported"}
}
func (c *Conn) processEvents() {
c.processEventsWithDeadline(time.Time{})
}
func (c *Conn) processEventsWithDeadline(deadline time.Time) {
localAddr, ok := c.sock.LocalAddr().(*net.UDPAddr)
if !ok {
panic("Cannot convert localAddr")
}
var timeoutCh <-chan time.Time
if !deadline.IsZero() {
timeoutCh = time.After(-time.Since(deadline))
} else {
timeoutCh = make(chan time.Time, 1)
}
select {
case result, ok := <-c.readChan:
if len(result.Buf) == 0 {
break
}
if !ok || c.closed {
break
}
c.quicClient.ProcessPacket(localAddr, result.Addr, result.Buf)
case <-c.quicClient.taskRunner.WaitTimer():
if c.closed {
panic("debug")
break
}
c.quicClient.taskRunner.DoTasks()
case <-timeoutCh:
// Break when past deadline
}
c.quicClient.taskRunner.DoTasks()
}
func (c *Conn) waitForEvents() bool {
c.processEvents()
return c.quicClient.session.NumActiveRequests() != 0
}
func (c *Conn) Connect() bool {
qc := c.quicClient
qc.StartConnect()
for qc.EncryptionBeingEstablished() {
// Busy loop waiting for connection to be established
// TODO(serialx): Maybe we can add some tiny deadlines instead of time.Now to decrease busy waiting?
c.waitForEvents()
}
return qc.IsConnected()
}
func (c *Conn) CreateStream() *SpdyClientStream {
quicClientStream := c.quicClient.CreateReliableQuicStream()
stream := &SpdyClientStream{
conn: c,
quicClientStream: quicClientStream,
pendingReads: lane.NewDeque(),
}
quicClientStream.userStream = stream
return stream
}
func (c *Conn) Writer() *ClientWriter {
return c.writer
}
func Dial(network, address string) (c *Conn, err error) {
i := strings.LastIndex(network, ":")
if i > 0 { // has colon
return nil, &errorString{"Not supported yet"} // TODO
}
ra, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, err
}
return dialQuic(network, net.Addr(ra).(*net.UDPAddr))
}
func dialQuic(network string, addr *net.UDPAddr) (*Conn, error) {
switch network {
case "udp", "udp4", "udp6":
default:
return nil, &errorString{"Unknown network"}
}
if addr == nil {
return nil, &errorString{"Missing address"}
}
conn_udp, err := net.DialUDP(network, nil, addr)
if err != nil {
return nil, err
}
quic_conn := &Conn{
addr: addr,
sock: conn_udp,
readQuitCh: make(chan bool, 1),
writeQuitCh: make(chan bool, 1),
}
createSpdyClientSession := func() OutgoingDataStreamCreator {
return &SpdyClientSession{conn: quic_conn}
}
taskRunner := CreateTaskRunner()
proofVerifier := CreateProofVerifier()
quicClient, err := CreateQuicClient(addr, quic_conn, createSpdyClientSession, taskRunner, proofVerifier)
if err != nil {
return nil, err
}
quic_conn.quicClient = quicClient
quic_conn.readChan = make(chan UdpData)
quic_conn.writer = NewClientWriter(make(chan UdpData, 1000)) // TODO(serialx, hodduc): Optimize buffer size
go func() {
for dat := range quic_conn.writer.Ch {
quic_conn.sock.Write(dat.Buf)
}
quic_conn.sock.Close()
quic_conn.writeQuitCh <- true
}()
go func() {
buf := make([]byte, 65535)
Loop:
for {
quic_conn.sock.SetReadDeadline(time.Now().Add(time.Second / 2)) // TIMEOUT = 0.5 sec
n, peer_addr, err := quic_conn.sock.ReadFromUDP(buf)
if err == nil {
buf_new := make([]byte, n)
copy(buf_new, buf) // XXX(hodduc) buffer copy?
select {
case <-quic_conn.readQuitCh:
break Loop
case quic_conn.readChan <- UdpData{Addr: peer_addr, Buf: buf_new}:
}
} else if neterr, ok := err.(net.Error); ok && neterr.Timeout() {
select {
case <-quic_conn.readQuitCh:
break Loop
default:
}
} else {
panic(err)
}
}
close(quic_conn.writer.Ch)
}()
if quic_conn.Connect() == false {
return nil, &errorString{"Cannot connect"}
}
return quic_conn, nil
}