2020-02-20 05:47:37 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2021-06-21 03:29:26 +00:00
|
|
|
// Dialers have the network locked in.
|
2020-02-20 05:47:37 +00:00
|
|
|
type Dialer interface {
|
|
|
|
Dial(_ context.Context, addr string) (net.Conn, error)
|
2021-06-21 02:54:57 +00:00
|
|
|
DialerNetwork() string
|
2020-02-20 05:47:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 03:29:26 +00:00
|
|
|
// An interface to ease wrapping dialers that explicitly include a network parameter.
|
|
|
|
type DialContexter interface {
|
|
|
|
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
|
2020-02-20 05:47:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 03:29:26 +00:00
|
|
|
// Used by wrappers of standard library network types.
|
|
|
|
var DefaultNetDialer = &net.Dialer{}
|
2021-06-21 02:54:57 +00:00
|
|
|
|
2021-06-21 03:29:26 +00:00
|
|
|
// Adapts a DialContexter to the Dial interface in this package.
|
|
|
|
type NetworkDialer struct {
|
|
|
|
Network string
|
|
|
|
Dialer DialContexter
|
2020-02-20 05:47:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 03:29:26 +00:00
|
|
|
func (me NetworkDialer) DialerNetwork() string {
|
|
|
|
return me.Network
|
2020-02-20 05:47:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 03:29:26 +00:00
|
|
|
func (me NetworkDialer) Dial(ctx context.Context, addr string) (_ net.Conn, err error) {
|
|
|
|
return me.Dialer.DialContext(ctx, me.Network, addr)
|
2020-02-20 05:47:37 +00:00
|
|
|
}
|