From 019f67a7de8fec62231403957b4829d14b181be7 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 3 Apr 2016 18:54:56 +1200 Subject: [PATCH] libnmdc: ensure we always flush protocol commands that happened alongside a disconnection event --- src/libnmdc/libnmdc.go | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/libnmdc/libnmdc.go b/src/libnmdc/libnmdc.go index 427517c..4a990b7 100644 --- a/src/libnmdc/libnmdc.go +++ b/src/libnmdc/libnmdc.go @@ -375,17 +375,6 @@ func (this *HubConnection) worker() { } } - // Maybe we disconnected - if err != nil { - this.State = CONNECTIONSTATE_DISCONNECTED - this.conn = nil - this.connValid = false - this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()}) - - time.Sleep(30 * time.Second) // Wait before reconnect - continue - } - // Attempt to parse a message block for len(fullBuffer) > 0 { for len(fullBuffer) > 0 && fullBuffer[0] == '|' { @@ -400,6 +389,19 @@ func (this *HubConnection) worker() { } } + // Maybe we disconnected + // Perform this check *last*, to ensure we've had a final shot at + // clearing out any queued messages + if err != nil { + this.State = CONNECTIONSTATE_DISCONNECTED + this.conn = nil + this.connValid = false + this.processEvent(HubEvent{EventType: EVENT_CONNECTION_STATE_CHANGED, StateChange: CONNECTIONSTATE_DISCONNECTED, Message: err.Error()}) + + time.Sleep(30 * time.Second) // Wait before reconnect + continue + } + } }