From a2b94e8724846ae85a3f71eae61fcc1461b7a07c Mon Sep 17 00:00:00 2001 From: mappu Date: Mon, 4 Apr 2016 18:57:50 +1200 Subject: [PATCH] libnmdc: send protocol-level keepalives --- src/libnmdc/libnmdc.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/libnmdc/libnmdc.go b/src/libnmdc/libnmdc.go index 8855c2c..f6cd88d 100644 --- a/src/libnmdc/libnmdc.go +++ b/src/libnmdc/libnmdc.go @@ -13,8 +13,9 @@ import ( ) const ( - DEFAULT_CLIENT_TAG string = "libnmdc.go" - DEFAULT_HUB_NAME string = "(unknown)" + DEFAULT_CLIENT_TAG string = "libnmdc.go" + DEFAULT_HUB_NAME string = "(unknown)" + SEND_KEEPALIVE_EVERY_SECONDS int = 29 ) type ConnectionState int @@ -370,7 +371,17 @@ func (this *HubConnection) worker() { // Read from socket into our local buffer (blocking) if this.connValid { readBuff := make([]byte, 1024) + this.conn.SetReadDeadline(time.Now().Add(SEND_KEEPALIVE_EVERY_SECONDS * time.Second)) 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 { fullBuffer += string(readBuff[0:nbytes]) }