2020-02-20 13:47:37 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2021-06-21 11:29:26 +08:00
|
|
|
// Dialers have the network locked in.
|
2020-02-20 13:47:37 +08:00
|
|
|
type Dialer interface {
|
|
|
|
Dial(_ context.Context, addr string) (net.Conn, error)
|
2021-06-21 10:54:57 +08:00
|
|
|
DialerNetwork() string
|
2020-02-20 13:47:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:29:26 +08: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 13:47:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:29:26 +08:00
|
|
|
// Used by wrappers of standard library network types.
|
|
|
|
var DefaultNetDialer = &net.Dialer{}
|
2021-06-21 10:54:57 +08:00
|
|
|
|
2021-06-21 11:29:26 +08:00
|
|
|
// Adapts a DialContexter to the Dial interface in this package.
|
|
|
|
type NetworkDialer struct {
|
|
|
|
Network string
|
|
|
|
Dialer DialContexter
|
2020-02-20 13:47:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:29:26 +08:00
|
|
|
func (me NetworkDialer) DialerNetwork() string {
|
|
|
|
return me.Network
|
2020-02-20 13:47:37 +08:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:29:26 +08: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 13:47:37 +08:00
|
|
|
}
|