forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keepalive.go
63 lines (49 loc) · 1.14 KB
/
keepalive.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
package xmpp
import (
"encoding/xml"
"io"
"log"
"net"
"time"
"github.com/twstrike/coyim/xmpp/data"
)
var (
keepaliveInterval = 10 * time.Second
keepaliveTimeout = 30 * time.Second
)
const logKeepAlives = false
// Manage whitespace keepalives as specified in RFC 6120, section 4.6.1
func (c *conn) watchKeepAlive(conn net.Conn) {
tick := time.NewTicker(keepaliveInterval)
defer tick.Stop()
defer log.Println("xmpp: no more watching keepalives")
for _ = range tick.C {
if c.closed {
return
}
if c.sendKeepalive() {
if logKeepAlives {
log.Println("xmpp: keepalive sent")
}
continue
}
log.Println("xmpp: keepalive failed")
go c.sendStreamError(data.StreamError{
DefinedCondition: data.ConnectionTimeout,
})
return
}
}
func (c *conn) sendKeepalive() bool {
_, err := c.keepaliveOut.Write([]byte{0x20})
return c.closed || err == nil || err == io.EOF
}
func (c *conn) sendStreamError(streamError data.StreamError) error {
enc, err := xml.Marshal(streamError)
if err != nil {
return err
}
//This is expected to error since the connection may be unreliable at this moment
c.out.Write(enc)
return c.Close()
}