/
dial.go
68 lines (59 loc) · 1.74 KB
/
dial.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
package utp
import (
"errors"
"net"
"time"
)
func Dial(n, addr string) (*UTPConn, error) {
raddr, err := ResolveUTPAddr(n, addr)
if err != nil {
return nil, err
}
return DialUTP(n, nil, raddr)
}
func DialUTP(n string, laddr, raddr *UTPAddr) (*UTPConn, error) {
return dial(n, laddr, raddr, 0)
}
func DialUTPTimeout(n string, laddr, raddr *UTPAddr, timeout time.Duration) (*UTPConn, error) {
return dial(n, laddr, raddr, timeout)
}
// A Dialer contains options for connecting to an address.
//
// The zero value for each field is equivalent to dialing without
// that option. Dialing with the zero value of Dialer is therefore
// equivalent to just calling the Dial function.
type Dialer struct {
// Timeout is the maximum amount of time a dial will wait for
// a connect to complete. If Deadline is also set, it may fail
// earlier.
//
// The default is no timeout.
//
// With or without a timeout, the operating system may impose
// its own earlier timeout. For instance, TCP timeouts are
// often around 3 minutes.
Timeout time.Duration
// LocalAddr is the local address to use when dialing an
// address. The address must be of a compatible type for the
// network being dialed.
// If nil, a local address is automatically chosen.
LocalAddr net.Addr
}
// Dial connects to the address on the named network.
//
// See func Dial for a description of the network and address parameters.
func (d *Dialer) Dial(n, addr string) (*UTPConn, error) {
raddr, err := ResolveUTPAddr(n, addr)
if err != nil {
return nil, err
}
var laddr *UTPAddr
if d.LocalAddr != nil {
var ok bool
laddr, ok = d.LocalAddr.(*UTPAddr)
if !ok {
return nil, errors.New("Dialer.LocalAddr is not a UTPAddr")
}
}
return DialUTPTimeout(n, laddr, raddr, d.Timeout)
}