Do not reconnect on "connection replaced" stream errors

Fix #45
This commit is contained in:
Mickael Remond
2019-06-08 11:15:51 +02:00
parent 3689448c90
commit b7461ae97f
3 changed files with 27 additions and 4 deletions
+19 -1
View File
@@ -21,6 +21,7 @@ const (
StateDisconnected ConnState = iota
StateConnected
StateSessionEstablished
StateStreamError
)
// Event is a structure use to convey event changes related to client state. This
@@ -28,6 +29,7 @@ const (
type Event struct {
State ConnState
Description string
StreamError string
}
// EventHandler is use to pass events about state of the connection to
@@ -49,6 +51,13 @@ func (em EventManager) updateState(state ConnState) {
}
}
func (em EventManager) streamError(error, desc string) {
em.CurrentState = StateStreamError
if em.Handler != nil {
em.Handler(Event{State: em.CurrentState, StreamError: error, Description: desc})
}
}
// Client
// ============================================================================
@@ -164,8 +173,17 @@ func (c *Client) recv() (err error) {
c.updateState(StateDisconnected)
return err
}
// Handle stream errors
switch packet := val.(type) {
case StreamError:
c.RecvChannel <- val
close(c.RecvChannel)
c.streamError(packet.Error.Local, packet.Text)
return errors.New("stream error: " + packet.Error.Local)
}
c.RecvChannel <- val
val = nil
}
}