From 5ed93bee2bd280d020a90ea305e306b754b203fd Mon Sep 17 00:00:00 2001 From: esrrhs Date: Wed, 19 Dec 2018 15:50:21 +0800 Subject: [PATCH] add --- client.go | 20 ++++++++++++++++++++ server.go | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/client.go b/client.go index 39fcd98..7f1d07a 100644 --- a/client.go +++ b/client.go @@ -49,6 +49,11 @@ type Client struct { localAddrToConnMap map[string]*ClientConn localIdToConnMap map[string]*ClientConn + + sendPacket uint64 + recvPacket uint64 + sendPacketSize uint64 + recvPacketSize uint64 } type ClientConn struct { @@ -112,6 +117,7 @@ func (p *Client) Run() { case <-interval.C: p.checkTimeoutConn() p.ping() + p.showNet() case r := <-recv: p.processPacket(r) } @@ -151,6 +157,9 @@ func (p *Client) Accept() error { clientConn.activeTime = now sendICMP(*p.conn, p.ipaddrServer, p.targetAddr, clientConn.id, (uint32)(DATA), bytes[:n], p.sproto, p.rproto) + + p.sendPacket++ + p.sendPacketSize += (uint64)(n) } } @@ -184,6 +193,9 @@ func (p *Client) processPacket(packet *Packet) { clientConn.close = true return } + + p.recvPacket++ + p.recvPacketSize += (uint64)(len(packet.data)) } func (p *Client) Close(clientConn *ClientConn) { @@ -216,3 +228,11 @@ func (p *Client) ping() { sendICMP(*p.conn, p.ipaddrServer, p.targetAddr, "", (uint32)(PING), b, p.sproto, p.rproto) fmt.Printf("ping %s %s %d %d\n", p.addrServer, now.String(), p.sproto, p.rproto) } + +func (p *Client) showNet() { + fmt.Printf("send %d/s %dKB/s recv %d/s %dKB/s\n", p.sendPacket, p.sendPacket/1024, p.recvPacket, p.recvPacket/1024) + p.sendPacket = 0 + p.recvPacket = 0 + p.sendPacketSize = 0 + p.recvPacketSize = 0 +} diff --git a/server.go b/server.go index c6b3ecc..48d63ea 100644 --- a/server.go +++ b/server.go @@ -19,6 +19,11 @@ type Server struct { conn *icmp.PacketConn localConnMap map[string]*ServerConn + + sendPacket uint64 + recvPacket uint64 + sendPacketSize uint64 + recvPacketSize uint64 } type ServerConn struct { @@ -51,6 +56,7 @@ func (p *Server) Run() { select { case <-interval.C: p.checkTimeoutConn() + p.showNet() case r := <-recv: p.processPacket(r) } @@ -100,6 +106,9 @@ func (p *Server) processPacket(packet *Packet) { udpConn.close = true return } + + p.recvPacket++ + p.recvPacketSize += (uint64)(len(packet.data)) } func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) { @@ -128,6 +137,9 @@ func (p *Server) Recv(conn *ServerConn, id string, src *net.IPAddr) { conn.activeTime = now sendICMP(*p.conn, src, "", id, (uint32)(DATA), bytes[:n], conn.rproto, 0) + + p.sendPacket++ + p.sendPacketSize += (uint64)(n) } } @@ -155,3 +167,11 @@ func (p *Server) checkTimeoutConn() { } } } + +func (p *Server) showNet() { + fmt.Printf("send %d/s %dKB/s recv %d/s %dKB/s\n", p.sendPacket, p.sendPacket/1024, p.recvPacket, p.recvPacket/1024) + p.sendPacket = 0 + p.recvPacket = 0 + p.sendPacketSize = 0 + p.recvPacketSize = 0 +}