Explorar el Código

实现pingLoop

skyfffire hace 2 años
padre
commit
c9d7f33990
Se han modificado 1 ficheros con 18 adiciones y 1 borrados
  1. 18 1
      p2p/peer.go

+ 18 - 1
p2p/peer.go

@@ -176,7 +176,24 @@ func (p *Peer) readLoop(errc chan<- error) {
 }
 
 func (p *Peer) pingLoop() {
-	// TODO 实现pingLoop
+	ping := time.NewTimer(pingInterval)
+
+	defer p.wg.Done()
+	defer ping.Stop()
+
+	for {
+		select {
+		case <-ping.C:
+			if err := SendItems(p.rw, pingMsg); err != nil {
+				p.protoErr <- err
+				return
+			}
+
+			ping.Reset(pingInterval)
+		case <-p.closed:
+			return
+		}
+	}
 }
 
 type protoRW struct {