Skip to content

Commit

Permalink
Merge pull request #82 from cloudstruct/fix/protocol-exit-loops-on-error
Browse files Browse the repository at this point in the history
fix: exit send/recv loops in Protocol on error
  • Loading branch information
agaffney authored May 5, 2022
2 parents c56bf6c + 295937c commit 04d0a38
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func (p *Protocol) sendLoop() {
newState, err = p.getNewState(msg)
if err != nil {
p.SendError(fmt.Errorf("%s: error sending message: %s", p.config.Name, err))
return
}
setNewState = true
// If there are no queued messages, set the new state now
Expand Down Expand Up @@ -159,13 +160,15 @@ func (p *Protocol) sendLoop() {
data, err = utils.CborEncode(msg)
if err != nil {
p.SendError(err)
return
}
}
payloadBuf.Write(data)
if !setNewState {
newState, err = p.getNewState(msg)
if err != nil {
p.SendError(fmt.Errorf("%s: error sending message: %s", p.config.Name, err))
return
}
setNewState = true
}
Expand Down Expand Up @@ -247,21 +250,25 @@ func (p *Protocol) recvLoop() {
p.recvReadyChan <- true
continue
}
p.config.ErrorChan <- fmt.Errorf("%s: decode error: %s", p.config.Name, err)
p.SendError(fmt.Errorf("%s: decode error: %s", p.config.Name, err))
return
}
// Create Message object from CBOR
msgType := uint(tmpMsg[0].(uint64))
msgData := p.recvBuffer.Bytes()[:numBytesRead]
msg, err := p.config.MessageFromCborFunc(msgType, msgData)
if err != nil {
p.config.ErrorChan <- err
p.SendError(err)
return
}
if msg == nil {
p.config.ErrorChan <- fmt.Errorf("%s: received unknown message type: %#v", p.config.Name, tmpMsg)
p.SendError(fmt.Errorf("%s: received unknown message type: %#v", p.config.Name, tmpMsg))
return
}
// Handle message
if err := p.handleMessage(msg, isResponse); err != nil {
p.config.ErrorChan <- err
p.SendError(err)
return
}
if numBytesRead < p.recvBuffer.Len() {
// There is another message in the same muxer segment, so we reset the buffer with just
Expand Down

0 comments on commit 04d0a38

Please sign in to comment.