Skip to content

Commit

Permalink
修改pubish解包时,有效载荷长度小于2时发送panic的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ybq committed Mar 20, 2019
1 parent 050e6ab commit e5a27f4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
9 changes: 9 additions & 0 deletions packet/const.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package packet

import "errors"

const (
//publish类型 缓存最小大小
PUBLISH_CACHE_MINLENGTH = 1024 * 1024 * 1024
Expand Down Expand Up @@ -120,3 +122,10 @@ const (
PARSE_NOT_ENOUGH_LACK //解析字节不够,下次提供新增字节
PARSE_REMAINDER //剩余字节
)

var (
// ErrNoTopic PUBLISH 未设置主题
ErrNoTopic = errors.New("PUBLISH 未设置主题")
// 主题包含U+0000字符
ErrTopicU0000 = errors.New("主题包含U+0000字符")
)
2 changes: 1 addition & 1 deletion packet/packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestPublishPacketAndUnPacket(t *testing.T) {
newconn.remlen = int(datalen)
fmt.Printf("%X\n", tmp[1+clen:])
newconn.UnPacket(tmp[0], tmp[1+clen:])
fmt.Printf("解析后结果:\n%s", newconn)
fmt.Printf("解析后结果:\n%v", newconn)

}

Expand Down
8 changes: 5 additions & 3 deletions packet/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package packet

import (
"errors"
"fmt"
"io"
"time"
Expand Down Expand Up @@ -209,14 +208,17 @@ func formatPayload(payload []byte) string {
return fmt.Sprintf("(%d)%X", len(payload), payload[0:n])
}
func (c *Publish) String() string {
return fmt.Sprintf("PUBLISH重发标志:%t 服务质量等级(Qos):%d 保留标志:%t 主题名:%s 报文标志:%d 接收时间:%v有效载荷:%s", c.GetDupFlag(), c.GetQos(), c.GetRetain(), c.topic, c.packetId, c.ReciveStartTime, formatPayload(c.payload))
return fmt.Sprintf("PUBLISH DUP:%t QoS:%d Retain:%t Topic:%s id:%d time:%v payload:%s", c.GetDupFlag(), c.GetQos(), c.GetRetain(), c.topic, c.packetId, c.ReciveStartTime, formatPayload(c.payload))
}

//解包
func (c *Publish) UnPacket(header byte, msg []byte) error {
c.cache = false

c.remlen = len(msg)
if c.remlen < 2 {
return ErrNoTopic
}
// c.SetRetain(header&0x1 == 0x1)
// c.SetQos(QoS(header >> 1 & 0x3))
// c.SetDupFlag(header&0x8 == 0x8)
Expand All @@ -232,7 +234,7 @@ func (c *Publish) UnPacket(header byte, msg []byte) error {
}

if !util.VerifyZeroByMqtt([]rune(c.GetTopicByString())) {
return errors.New("主题包含U+0000字符关闭连接")
return ErrTopicU0000
}
//c.setBuffByRemdata(msg)
return nil
Expand Down

0 comments on commit e5a27f4

Please sign in to comment.