]> Sergey Matveev's repositories - btrtrc.git/blob - dialer.go
Add support for non-IP-based networks
[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         // The network is implied by the instance.
12         Dial(_ context.Context, addr string) (net.Conn, error)
13         // This is required for registering with the connection tracker (router connection table
14         // emulating rate-limiter) before dialing. TODO: What about connections that wouldn't infringe
15         // on routers, like localhost or unix sockets.
16         LocalAddr() net.Addr
17 }
18
19 type NetDialer struct {
20         Network string
21         Dialer  net.Dialer
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 }