forked from getlantern/go-udtwrapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
listen.go
74 lines (63 loc) · 1.94 KB
/
listen.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 udt
import (
"net"
// sockaddr "github.com/jbenet/go-sockaddr"
// sockaddrnet "github.com/jbenet/go-sockaddr/net"
)
// UDTListener is a network listener for UDT.
type UDTListener struct {
net.Listener
fd *udtFD
}
func (l *UDTListener) Accept() (c net.Conn, err error) {
cfd, err := l.fd.accept()
if err != nil {
return nil, err
}
return &UDTConn{
udtFD: cfd,
net: l.fd.LocalAddr().Network(),
}, nil
}
func (l *UDTListener) Close() error {
return l.fd.Close()
}
func (l *UDTListener) Addr() net.Addr {
return l.fd.LocalAddr()
}
// ListenUDT listens for incoming UDT packets addressed to the local
// address laddr. Net must be "udt", "udt4", or "udt6". If laddr has
// a port of 0, ListenUDT will choose an available port.
// The LocalAddr method of the returned UDTConn can be used to
// discover the port. The returned connection's ReadFrom and WriteTo
// methods can be used to receive and send UDT packets with per-packet
// addressing.
func ListenUDT(network string, laddr *UDTAddr) (*UDTListener, error) {
switch network {
case "udt", "udt4", "udt6":
default:
return nil, &net.OpError{Op: "listen", Net: network, Addr: laddr, Err: net.UnknownNetworkError(network)}
}
if laddr == nil {
laddr = &UDTAddr{addr: &net.UDPAddr{}}
}
fdl, err := listenFD(laddr)
if err != nil {
return nil, err
}
return &UDTListener{fd: fdl}, nil
}
// Listen listens for incoming UDT packets addressed to the local
// address laddr. Net must be "udt", "udt4", or "udt6". If laddr has
// a port of 0, ListenUDT will choose an available port.
// The LocalAddr method of the returned UDTConn can be used to
// discover the port. The returned connection's ReadFrom and WriteTo
// methods can be used to receive and send UDT packets with per-packet
// addressing.
func Listen(network, address string) (net.Listener, error) {
laddr, err := ResolveUDTAddr(network, address)
if err != nil {
return nil, err
}
return ListenUDT(network, laddr)
}