forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_conn.go
74 lines (59 loc) · 1.28 KB
/
send_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
package quic
import (
"net"
)
// A sendConn allows sending using a simple Write() on a non-connected packet conn.
type sendConn interface {
Write([]byte) error
Close() error
LocalAddr() net.Addr
RemoteAddr() net.Addr
}
type sconn struct {
connection
remoteAddr net.Addr
info *packetInfo
oob []byte
}
var _ sendConn = &sconn{}
func newSendConn(c connection, remote net.Addr, info *packetInfo) sendConn {
return &sconn{
connection: c,
remoteAddr: remote,
info: info,
oob: info.OOB(),
}
}
func (c *sconn) Write(p []byte) error {
_, err := c.WritePacket(p, c.remoteAddr, c.oob)
return err
}
func (c *sconn) RemoteAddr() net.Addr {
return c.remoteAddr
}
func (c *sconn) LocalAddr() net.Addr {
addr := c.connection.LocalAddr()
if c.info != nil {
if udpAddr, ok := addr.(*net.UDPAddr); ok {
addrCopy := *udpAddr
addrCopy.IP = c.info.addr
addr = &addrCopy
}
}
return addr
}
type spconn struct {
net.PacketConn
remoteAddr net.Addr
}
var _ sendConn = &spconn{}
func newSendPconn(c net.PacketConn, remote net.Addr) sendConn {
return &spconn{PacketConn: c, remoteAddr: remote}
}
func (c *spconn) Write(p []byte) error {
_, err := c.WriteTo(p, c.remoteAddr)
return err
}
func (c *spconn) RemoteAddr() net.Addr {
return c.remoteAddr
}