This commit is contained in:
esrrhs 2018-12-23 06:08:26 +08:00
parent 8efea89357
commit 51aba43633
2 changed files with 24 additions and 11 deletions

View File

@ -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
}

View File

@ -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)