p2p/protocols: Wait for handshake to be sent before returning

Signed-off-by: Lewis Marshall <lewis@lmars.net>
This commit is contained in:
Lewis Marshall 2017-06-25 12:29:07 +02:00
parent e145bda359
commit 5d8c55b077

View file

@ -280,29 +280,24 @@ func (self *Peer) Handshake(ctx context.Context, hs interface{}) (interface{}, e
} }
errc := make(chan error, 2) errc := make(chan error, 2)
go func() { go func() {
if err := self.Send(hs); err != nil { errc <- self.Send(hs)
errc <- errorf(ErrHandshake, "cannot send: %v", err)
}
}() }()
hsc := make(chan interface{}) var rhs interface{}
go func() { go func() {
var rhs interface{} errc <- self.handleIncoming(func(msg interface{}) error {
err := self.handleIncoming(func(msg interface{}) error {
rhs = msg rhs = msg
return nil return nil
}) })
if err != nil {
errc <- err
return
}
hsc <- rhs
}() }()
select { for i := 0; i < 2; i++ {
case rhs := <-hsc: select {
return rhs, nil case err := <-errc:
case <-ctx.Done(): if err != nil {
return nil, ctx.Err() return nil, errorf(ErrHandshake, err.Error())
case err := <-errc: }
return nil, err case <-ctx.Done():
return nil, ctx.Err()
}
} }
return rhs, nil
} }