mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 20:56:42 +00:00
p2p: read/write handshake concurrently
This makes it possible to run two base protocols against each other over a MsgPipe.
This commit is contained in:
parent
16f417f5af
commit
19d9cc33e4
1 changed files with 12 additions and 11 deletions
|
|
@ -89,20 +89,26 @@ type baseProtocol struct {
|
|||
|
||||
func runBaseProtocol(peer *Peer, rw MsgReadWriter) error {
|
||||
bp := &baseProtocol{rw, peer}
|
||||
if err := bp.doHandshake(rw); err != nil {
|
||||
errc := make(chan error, 1)
|
||||
go func() { errc <- rw.WriteMsg(bp.handshakeMsg()) }()
|
||||
if err := bp.readHandshake(); err != nil {
|
||||
return err
|
||||
}
|
||||
// handle write error
|
||||
if err := <-errc; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// run main loop
|
||||
quit := make(chan error, 1)
|
||||
go func() {
|
||||
for {
|
||||
if err := bp.handle(rw); err != nil {
|
||||
quit <- err
|
||||
errc <- err
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
return bp.loop(quit)
|
||||
return bp.loop(errc)
|
||||
}
|
||||
|
||||
var pingTimeout = 2 * time.Second
|
||||
|
|
@ -193,14 +199,9 @@ func (bp *baseProtocol) handle(rw MsgReadWriter) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (bp *baseProtocol) doHandshake(rw MsgReadWriter) error {
|
||||
// send our handshake
|
||||
if err := rw.WriteMsg(bp.handshakeMsg()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
func (bp *baseProtocol) readHandshake() error {
|
||||
// read and handle remote handshake
|
||||
msg, err := rw.ReadMsg()
|
||||
msg, err := bp.rw.ReadMsg()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue