-
Notifications
You must be signed in to change notification settings - Fork 15
/
conn.go
78 lines (63 loc) · 1.39 KB
/
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
package quicconn
import (
"context"
"net"
"time"
quic "github.com/lucas-clemente/quic-go"
)
type conn struct {
session quic.Session
receiveStream quic.Stream
sendStream quic.Stream
}
func newConn(sess quic.Session) (*conn, error) {
stream, err := sess.OpenStream()
if err != nil {
return nil, err
}
return &conn{
session: sess,
sendStream: stream,
}, nil
}
func (c *conn) Read(b []byte) (int, error) {
if c.receiveStream == nil {
var err error
c.receiveStream, err = c.session.AcceptStream(context.Background())
// TODO: check stream id
if err != nil {
return 0, err
}
// quic.Stream.Close() closes the stream for writing
err = c.receiveStream.Close()
if err != nil {
return 0, err
}
}
return c.receiveStream.Read(b)
}
func (c *conn) Write(b []byte) (int, error) {
return c.sendStream.Write(b)
}
// LocalAddr returns the local network address.
// needed to fulfill the net.Conn interface
func (c *conn) LocalAddr() net.Addr {
return c.session.LocalAddr()
}
// RemoteAddr returns the remote network address.
func (c *conn) RemoteAddr() net.Addr {
return c.session.RemoteAddr()
}
func (c *conn) Close() error {
return c.session.Close()
}
func (c *conn) SetDeadline(t time.Time) error {
return nil
}
func (c *conn) SetReadDeadline(t time.Time) error {
return nil
}
func (c *conn) SetWriteDeadline(t time.Time) error {
return nil
}
var _ net.Conn = &conn{}