Ping pong message

poc8
obscuren 11 years ago
parent ddf82c3337
commit 87434a0941
  1. 8
      peer.go
  2. 4
      server.go

@ -34,6 +34,9 @@ type Peer struct {
// This flag is used by writeMessage to check if messages are allowed // This flag is used by writeMessage to check if messages are allowed
// to be send or not. If no version is known all messages are ignored. // to be send or not. If no version is known all messages are ignored.
versionKnown bool versionKnown bool
// Last received pong message
lastPong int64
} }
func NewPeer(conn net.Conn, server *Server, inbound bool) *Peer { func NewPeer(conn net.Conn, server *Server, inbound bool) *Peer {
@ -109,7 +112,7 @@ func (p *Peer) writeMessage(msg *ethwire.InOutMsg) {
// Outbound message handler. Outbound messages are handled here // Outbound message handler. Outbound messages are handled here
func (p *Peer) HandleOutbound() { func (p *Peer) HandleOutbound() {
// The ping timer. Makes sure that every 2 minutes a ping is send to the peer // The ping timer. Makes sure that every 2 minutes a ping is send to the peer
tickleTimer := time.NewTimer(2 * time.Minute) tickleTimer := time.NewTicker(2 * time.Minute)
out: out:
for { for {
select { select {
@ -173,7 +176,10 @@ out:
case ethwire.MsgGetPeersTy: case ethwire.MsgGetPeersTy:
case ethwire.MsgPeersTy: case ethwire.MsgPeersTy:
case ethwire.MsgPingTy: case ethwire.MsgPingTy:
// Respond back with pong
p.writeMessage(&ethwire.InOutMsg{Type: ethwire.MsgPongTy})
case ethwire.MsgPongTy: case ethwire.MsgPongTy:
p.lastPong = time.Now().Unix()
/* /*
case "blockmine": case "blockmine":

@ -81,13 +81,13 @@ func (s *Server) Broadcast(msgType ethwire.MsgType, data []byte) {
} }
const ( const (
processReapingTimeout = 10 // TODO increase processReapingTimeout = 1 // TODO increase
) )
func (s *Server) ReapDeadPeers() { func (s *Server) ReapDeadPeers() {
for { for {
eachPeer(s.peers, func(p *Peer, e *list.Element) { eachPeer(s.peers, func(p *Peer, e *list.Element) {
if atomic.LoadInt32(&p.disconnect) == 1 { if atomic.LoadInt32(&p.disconnect) == 1 || (p.inbound && (time.Now().Unix()-p.lastPong) > int64(5*time.Minute)) {
log.Println("Dead peer found .. reaping") log.Println("Dead peer found .. reaping")
s.peers.Remove(e) s.peers.Remove(e)

Loading…
Cancel
Save