/
ping.go
105 lines (94 loc) · 2.64 KB
/
ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright (c) 2013 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Seth Hoenig
* Allan Stockdill-Mander
* Mike Robertson
*/
package mqtt
import (
"errors"
"fmt"
"github.com/antlinker/mqtt-cli/packets"
"sync"
"time"
)
type lastcontact struct {
sync.Mutex
lasttime time.Time
}
func (l *lastcontact) update() {
l.Lock()
defer l.Unlock()
l.lasttime = time.Now()
}
func (l *lastcontact) get() time.Time {
l.Lock()
defer l.Unlock()
return l.lasttime
}
func keepalive(c *Client) {
var err error
defer func(c *Client) {
c.workers.Done()
if err != nil {
ERROR.Println(NET, c.options.ClientID+":keepalive closed :", err)
c.internalConnLost(err)
return
}
if e := recover(); e != nil {
ERROR.Println(NET, c.options.ClientID, ":", "keepalive panic closed :", e)
c.internalConnLost(fmt.Errorf("读取数据出现严重错误:%v", e))
return
}
ERROR.Println(NET, c.options.ClientID+":keepalive closed ")
}(c)
DEBUG.Println(PNG, "keepalive starting")
c.pingOutstanding = false
waittime := c.options.KeepAlive
for {
// keeptime := time.Now().Add(c.options.KeepAlive)
// keep := uint(time.Since(c.lastContact.get()).Seconds())
// if keep > c.options.KeepAlive.Seconds() {
// keeptime = time.Now().Add(c.options.KeepAlive)
// } else {
// time.Now().Add(c.options.KeepAlive)
// }
select {
case <-c.stop:
DEBUG.Println(PNG, "keepalive stopped")
//c.workers.Done()
return
case <-time.After(waittime):
last := uint(time.Since(c.lastContact.get()).Seconds())
//DEBUG.Printf("%s last contact: %d (timeout: %d) %b", PNG, last, uint(c.options.KeepAlive.Seconds()), c.pingOutstanding)
if last >= uint(c.options.KeepAlive.Seconds()) {
if !c.pingOutstanding {
//DEBUG.Println(NET, "keepalive sending ping")
ping := packets.NewControlPacket(packets.Pingreq).(*packets.PingreqPacket)
c.pingOutstanding = true
//We don't want to wait behind large messages being sent, the Write call
//will block until it it able to send the packet.
//ping.Write(c.conn)
sendServer(c, ping, nil)
waittime = c.options.KeepAlive / 4
if waittime == 0 {
waittime = 1 * time.Second
}
continue
} else {
//CRITICAL.Println(PNG, "pingresp not received, disconnecting")
err = errors.New("pingresp not received, disconnecting")
return
}
}
waittime = c.options.KeepAlive - time.Since(c.lastContact.get())
}
}
}