Skip to content

Commit

Permalink
perf: disconnect early when an invalid message was received
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Sep 8, 2021
1 parent c4a6406 commit a0c4f76
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ import (
)

const (
msgTypeRegister = 0x00
msgTypeLogin = 0x01
msgTypeLogout = 0x02
msgTypeTermData = 0x03
msgTypeWinsize = 0x04
msgTypeCmd = 0x05
msgTypeHeartbeat = 0x06
msgTypeFile = 0x07
msgTypeWeb = 0x08
msgTypeRegister = iota
msgTypeLogin
msgTypeLogout
msgTypeTermData
msgTypeWinsize
msgTypeCmd
msgTypeHeartbeat
msgTypeFile
msgTypeWeb
msgTypeMax = msgTypeWeb
)

const heartbeatInterval = time.Second * 5
Expand Down Expand Up @@ -189,6 +190,12 @@ func (dev *device) readLoop() {
br.Discard(3)

typ := b[0]

if typ > msgTypeMax {
log.Error().Msgf("invalid msg type: %d", typ)
return
}

msgLen := binary.BigEndian.Uint16(b[1:])

b = make([]byte, msgLen)
Expand Down

0 comments on commit a0c4f76

Please sign in to comment.