p2p: push handshake containing chainId for early disconnect. Closes #12

This commit is contained in:
Ethan Buchman
2015-03-17 01:20:39 -07:00
committed by Jae Kwon
parent 9c4692c071
commit 4abca6e963
3 changed files with 36 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package p2p
import (
"encoding/hex"
"errors"
"fmt"
"net"
@ -37,6 +38,7 @@ type Switch struct {
quit chan struct{}
started uint32
stopped uint32
chainId string
}
var (
@ -129,6 +131,10 @@ func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool) (*Peer, er
// Notify listeners.
sw.doAddPeer(peer)
// Send handshake
msg := &pexHandshakeMessage{ChainId: sw.chainId}
peer.Send(PexCh, msg)
return peer, nil
}
@ -216,6 +222,10 @@ func (sw *Switch) StopPeerGracefully(peer *Peer) {
sw.doRemovePeer(peer, nil)
}
func (sw *Switch) SetChainId(hash []byte, network string) {
sw.chainId = hex.EncodeToString(hash) + "-" + network
}
func (sw *Switch) IsListening() bool {
return sw.listeners.Size() > 0
}