-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
61 lines (50 loc) · 1.14 KB
/
connection.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
package nat
import (
"net"
"time"
)
type Conn struct {
conn *net.UDPConn
local, remote net.Addr
}
func newConn(sock *net.UDPConn, local, remote net.Addr) *Conn {
sock.SetDeadline(time.Time{})
return &Conn{sock, local, remote}
}
func (c *Conn) Read(b []byte) (int, error) {
for {
n, addr, err := c.conn.ReadFrom(b)
// Generic non-address related errors.
if addr == nil && err != nil {
return n, err
}
// Filter out anything not related to the address we care
// about.
if addr.Network() != c.remote.Network() || addr.String() != c.remote.String() {
continue
}
return n, err
}
panic("unreachable")
}
func (c *Conn) Write(b []byte) (int, error) {
return c.conn.WriteTo(b, c.remote)
}
func (c *Conn) Close() error {
return c.conn.Close()
}
func (c *Conn) LocalAddr() net.Addr {
return c.local
}
func (c *Conn) RemoteAddr() net.Addr {
return c.remote
}
func (c *Conn) SetDeadline(t time.Time) error {
return c.conn.SetDeadline(t)
}
func (c *Conn) SetReadDeadline(t time.Time) error {
return c.conn.SetReadDeadline(t)
}
func (c *Conn) SetWriteDeadline(t time.Time) error {
return c.conn.SetWriteDeadline(t)
}