libnmdc: send protocol-level keepalives

This commit is contained in:
mappu 2016-04-04 18:57:50 +12:00
parent 3dfef302ee
commit a2b94e8724

View File

@ -13,8 +13,9 @@ import (
) )
const ( const (
DEFAULT_CLIENT_TAG string = "libnmdc.go" DEFAULT_CLIENT_TAG string = "libnmdc.go"
DEFAULT_HUB_NAME string = "(unknown)" DEFAULT_HUB_NAME string = "(unknown)"
SEND_KEEPALIVE_EVERY_SECONDS int = 29
) )
type ConnectionState int type ConnectionState int
@ -370,7 +371,17 @@ func (this *HubConnection) worker() {
// Read from socket into our local buffer (blocking) // Read from socket into our local buffer (blocking)
if this.connValid { if this.connValid {
readBuff := make([]byte, 1024) readBuff := make([]byte, 1024)
this.conn.SetReadDeadline(time.Now().Add(SEND_KEEPALIVE_EVERY_SECONDS * time.Second))
nbytes, err = this.conn.Read(readBuff) nbytes, err = this.conn.Read(readBuff)
if err != nil && err.(net.Error).Timeout() {
// No data before read deadline
err = nil
// Send KA packet
_, err = this.conn.Write([]byte("|"))
}
if nbytes > 0 { if nbytes > 0 {
fullBuffer += string(readBuff[0:nbytes]) fullBuffer += string(readBuff[0:nbytes])
} }