From 51aba4363329ba61a46e1dec5b6772d1b154539f Mon Sep 17 00:00:00 2001 From: esrrhs Date: Sun, 23 Dec 2018 06:08:26 +0800 Subject: [PATCH] add --- client.go | 23 +++++++++++++++++------ server.go | 12 +++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index 6f7b70b..21c99eb 100644 --- a/client.go +++ b/client.go @@ -61,6 +61,9 @@ type Client struct { recvPacket uint64 sendPacketSize uint64 recvPacketSize uint64 + + pingPacketSize uint64 + pongPacketSize uint64 } type ClientConn struct { @@ -119,12 +122,16 @@ func (p *Client) Run() { interval := time.NewTicker(time.Second) defer interval.Stop() + interval1 := time.NewTicker(time.Millisecond * 1) + defer interval1.Stop() + for { select { case <-interval.C: p.checkTimeoutConn() - p.ping() p.showNet() + case <-interval1.C: + p.ping() case r := <-recv: p.processPacket(r) } @@ -181,8 +188,9 @@ func (p *Client) processPacket(packet *Packet) { if packet.msgType == PING { t := time.Time{} t.UnmarshalBinary(packet.data) - d := time.Now().Sub(t) - fmt.Printf("pong from %s %s\n", packet.src.String(), d.String()) + //d := time.Now().Sub(t) + //fmt.Printf("pong from %s %s\n", packet.src.String(), d.String()) + p.pongPacketSize++ return } @@ -239,16 +247,19 @@ func (p *Client) ping() { now := time.Now() b, _ := now.MarshalBinary() sendICMP(p.id, p.sequence, *p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto) - fmt.Printf("ping %s %s %d %d %d %d\n", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence) + //fmt.Printf("ping %s %s %d %d %d %d\n", p.addrServer, now.String(), p.sproto, p.rproto, p.id, p.sequence) p.sequence++ + p.pingPacketSize++ } } func (p *Client) showNet() { - fmt.Printf("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s\n", - p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024) + fmt.Printf("send %dPacket/s %dKB/s recv %dPacket/s %dKB/s ping %d/s pong %d/s\n", + p.sendPacket, p.sendPacketSize/1024, p.recvPacket, p.recvPacketSize/1024, p.pingPacketSize, p.pongPacketSize) p.sendPacket = 0 p.recvPacket = 0 p.sendPacketSize = 0 p.recvPacketSize = 0 + p.pingPacketSize = 0 + p.pongPacketSize = 0 } diff --git a/server.go b/server.go index c3116bf..f414a43 100644 --- a/server.go +++ b/server.go @@ -24,6 +24,9 @@ type Server struct { recvPacket uint64 sendPacketSize uint64 recvPacketSize uint64 + + echoId int + echoSeq int } type ServerConn struct { @@ -33,8 +36,6 @@ type ServerConn struct { activeTime time.Time close bool rproto int - echoId int - echoSeq int } func (p *Server) Run() { @@ -67,6 +68,9 @@ func (p *Server) Run() { func (p *Server) processPacket(packet *Packet) { + p.echoId = packet.echoId + p.echoSeq = packet.echoSeq + if packet.msgType == PING { t := time.Time{} t.UnmarshalBinary(packet.data) @@ -101,8 +105,6 @@ func (p *Server) processPacket(packet *Packet) { } udpConn.activeTime = now - udpConn.echoId = packet.echoId - udpConn.echoSeq = packet.echoSeq _, err := udpConn.conn.Write(packet.data) if err != nil { @@ -140,7 +142,7 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) { now := time.Now() conn.activeTime = now - sendICMP(conn.echoId, conn.echoSeq, *p.conn, src, "", id, (uint32)(DATA), bytes[:n], conn.rproto, -1) + sendICMP(p.echoId, p.echoSeq, *p.conn, src, "", id, (uint32)(DATA), bytes[:n], conn.rproto, -1) p.sendPacket++ p.sendPacketSize += (uint64)(n)