torrent/dialer.go

35 lines
830 B
Go
Raw Normal View History

package torrent
import (
"context"
"net"
)
2021-06-21 03:29:26 +00:00
// Dialers have the network locked in.
type Dialer interface {
Dial(_ context.Context, addr string) (net.Conn, error)
2021-06-21 02:54:57 +00:00
DialerNetwork() string
}
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)
}
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
}
2021-06-21 03:29:26 +00:00
func (me NetworkDialer) DialerNetwork() string {
return me.Network
}
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)
}