]> Sergey Matveev's repositories - btrtrc.git/blob - dialer.go
Expose DialFirst
[btrtrc.git] / dialer.go
1 package torrent
2
3 import (
4         "context"
5         "net"
6
7         "github.com/anacrolix/missinggo/perf"
8 )
9
10 type Dialer interface {
11         Dial(_ context.Context, addr string) (net.Conn, error)
12         DialerNetwork() string
13 }
14
15 type NetDialer struct {
16         Network string
17         Dialer  net.Dialer
18 }
19
20 func (me NetDialer) DialerNetwork() string {
21         return me.Network
22 }
23
24 func (me NetDialer) Dial(ctx context.Context, addr string) (_ net.Conn, err error) {
25         defer perf.ScopeTimerErr(&err)()
26         return me.Dialer.DialContext(ctx, me.Network, addr)
27 }
28
29 func (me NetDialer) LocalAddr() net.Addr {
30         return netDialerLocalAddr{me.Network, me.Dialer.LocalAddr}
31 }
32
33 type netDialerLocalAddr struct {
34         network string
35         addr    net.Addr
36 }
37
38 func (me netDialerLocalAddr) Network() string { return me.network }
39
40 func (me netDialerLocalAddr) String() string {
41         if me.addr == nil {
42                 return ""
43         }
44         return me.addr.String()
45 }