mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 01:13:45 +00:00
p2p: Add NodeDialer interface
Signed-off-by: Lewis Marshall <lewis@lmars.net>
This commit is contained in:
parent
38af4b0acc
commit
0227fd1941
3 changed files with 22 additions and 5 deletions
21
p2p/dial.go
21
p2p/dial.go
|
|
@ -47,6 +47,24 @@ const (
|
||||||
maxResolveDelay = time.Hour
|
maxResolveDelay = time.Hour
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NodeDialer is used to connect to nodes in the network, typically by using
|
||||||
|
// an underlying net.Dialer but also using net.Pipe in tests
|
||||||
|
type NodeDialer interface {
|
||||||
|
Dial(*discover.Node) (net.Conn, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TCPDialer implements the NodeDialer interface by using a net.Dialer to
|
||||||
|
// create TCP connections to nodes in the network
|
||||||
|
type TCPDialer struct {
|
||||||
|
*net.Dialer
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dial creates a TCP connection to the node
|
||||||
|
func (t TCPDialer) Dial(dest *discover.Node) (net.Conn, error) {
|
||||||
|
addr := &net.TCPAddr{IP: dest.IP, Port: int(dest.TCP)}
|
||||||
|
return t.Dialer.Dial("tcp", addr.String())
|
||||||
|
}
|
||||||
|
|
||||||
// dialstate schedules dials and discovery lookups.
|
// dialstate schedules dials and discovery lookups.
|
||||||
// it get's a chance to compute new tasks on every iteration
|
// it get's a chance to compute new tasks on every iteration
|
||||||
// of the main loop in Server.run.
|
// of the main loop in Server.run.
|
||||||
|
|
@ -318,8 +336,7 @@ func (t *dialTask) resolve(srv *Server) bool {
|
||||||
|
|
||||||
// dial performs the actual connection attempt.
|
// dial performs the actual connection attempt.
|
||||||
func (t *dialTask) dial(srv *Server, dest *discover.Node) bool {
|
func (t *dialTask) dial(srv *Server, dest *discover.Node) bool {
|
||||||
addr := &net.TCPAddr{IP: dest.IP, Port: int(dest.TCP)}
|
fd, err := srv.Dialer.Dial(dest)
|
||||||
fd, err := srv.Dialer.Dial("tcp", addr.String())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Trace("Dial error", "task", t, "err", err)
|
log.Trace("Dial error", "task", t, "err", err)
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
|
|
@ -597,7 +597,7 @@ func TestDialResolve(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now run the task, it should resolve the ID once.
|
// Now run the task, it should resolve the ID once.
|
||||||
config := Config{Dialer: &net.Dialer{Deadline: time.Now().Add(-5 * time.Minute)}}
|
config := Config{Dialer: TCPDialer{&net.Dialer{Deadline: time.Now().Add(-5 * time.Minute)}}}
|
||||||
srv := &Server{ntab: table, Config: config}
|
srv := &Server{ntab: table, Config: config}
|
||||||
tasks[0].Do(srv)
|
tasks[0].Do(srv)
|
||||||
if !reflect.DeepEqual(table.resolveCalls, []discover.NodeID{dest.ID}) {
|
if !reflect.DeepEqual(table.resolveCalls, []discover.NodeID{dest.ID}) {
|
||||||
|
|
|
||||||
|
|
@ -131,7 +131,7 @@ type Config struct {
|
||||||
|
|
||||||
// If Dialer is set to a non-nil value, the given Dialer
|
// If Dialer is set to a non-nil value, the given Dialer
|
||||||
// is used to dial outbound peer connections.
|
// is used to dial outbound peer connections.
|
||||||
Dialer *net.Dialer `toml:"-"`
|
Dialer NodeDialer `toml:"-"`
|
||||||
|
|
||||||
// If NoDial is true, the server will not dial any peers.
|
// If NoDial is true, the server will not dial any peers.
|
||||||
NoDial bool `toml:",omitempty"`
|
NoDial bool `toml:",omitempty"`
|
||||||
|
|
@ -369,7 +369,7 @@ func (srv *Server) Start() (err error) {
|
||||||
srv.newTransport = newRLPX
|
srv.newTransport = newRLPX
|
||||||
}
|
}
|
||||||
if srv.Dialer == nil {
|
if srv.Dialer == nil {
|
||||||
srv.Dialer = &net.Dialer{Timeout: defaultDialTimeout}
|
srv.Dialer = TCPDialer{&net.Dialer{Timeout: defaultDialTimeout}}
|
||||||
}
|
}
|
||||||
srv.quit = make(chan struct{})
|
srv.quit = make(chan struct{})
|
||||||
srv.addpeer = make(chan *conn)
|
srv.addpeer = make(chan *conn)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue