forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
66 lines (52 loc) · 1.1 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
package quic
import (
"net"
"sync"
)
type Connection interface {
Write([]byte) error
Read([]byte) (int, net.Addr, error)
Close() error
LocalAddr() net.Addr
RemoteAddr() net.Addr
SetCurrentRemoteAddr(net.Addr)
}
type Conn struct {
mutex sync.RWMutex
pconn net.PacketConn
currentAddr net.Addr
}
func NewConn(pconn net.PacketConn, remote net.Addr) *Conn {
return &Conn{pconn: pconn, currentAddr: remote}
}
var _ Connection = &Conn{}
func (c *Conn) Write(p []byte) error {
_, err := c.pconn.WriteTo(p, c.currentAddr)
return err
}
func (c *Conn) Read(p []byte) (int, net.Addr, error) {
return c.pconn.ReadFrom(p)
}
func (c *Conn) SetCurrentRemoteAddr(addr net.Addr) {
c.mutex.Lock()
c.currentAddr = addr
c.mutex.Unlock()
}
func (c *Conn) LocalAddr() net.Addr {
return c.pconn.LocalAddr()
}
func (c *Conn) RemoteAddr() net.Addr {
c.mutex.RLock()
addr := c.currentAddr
c.mutex.RUnlock()
return addr
}
func (c *Conn) Close() error {
return c.pconn.Close()
}
func (c *Conn) GetLocal() net.Addr {
return c.pconn.LocalAddr()
}
func (c *Conn) GetRemote() net.Addr {
return c.RemoteAddr()
}