Skip to content

Commit

Permalink
feat: ConnackPacket add NodeId
Browse files Browse the repository at this point in the history
  • Loading branch information
tangtaoit committed Apr 29, 2024
1 parent 52b7b1b commit 34e40dd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ const (
StreamSeqByteSize = 4
StreamFlagByteSize = 1
ExpireByteSize = 4
NodeIdByteSize = 8
)

const (
Expand Down
13 changes: 13 additions & 0 deletions connack.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type ConnackPacket struct {
Salt string // salt
TimeDiff int64 // 客户端时间与服务器的差值,单位毫秒。
ReasonCode ReasonCode // 原因码
NodeId uint64 // 节点Id
}

// GetFrameType 获取包类型
Expand All @@ -32,6 +33,9 @@ func encodeConnack(connack *ConnackPacket, enc *Encoder, version uint8) error {
_ = enc.WriteByte(connack.ReasonCode.Byte())
enc.WriteString(connack.ServerKey)
enc.WriteString(connack.Salt)
if version >= 4 {
enc.WriteUint64(connack.NodeId)
}
return nil
}

Expand All @@ -44,6 +48,9 @@ func encodeConnackSize(packet *ConnackPacket, version uint8) int {
size += ReasonCodeByteSize
size += (len(packet.ServerKey) + StringFixLenByteSize)
size += (len(packet.Salt) + StringFixLenByteSize)
if version >= 4 {
size += NodeIdByteSize
}
return size
}

Expand Down Expand Up @@ -76,5 +83,11 @@ func decodeConnack(frame Frame, data []byte, version uint8) (Frame, error) {
return nil, errors.Wrap(err, "解码Salt失败!")
}

if version >= 4 {
if connackPacket.NodeId, err = dec.Uint64(); err != nil {
return nil, errors.Wrap(err, "解码NodeId失败!")
}
}

return connackPacket, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
github.com/valyala/bytebufferpool v1.0.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
2 changes: 1 addition & 1 deletion protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type WKProto struct {
}

// LatestVersion 最新版本
const LatestVersion = 3
const LatestVersion = 4

// MaxRemaingLength 最大剩余长度 // 1<<28 - 1
const MaxRemaingLength uint32 = 1024 * 1024
Expand Down

0 comments on commit 34e40dd

Please sign in to comment.