From 34e40dd5e080caebb386ce8482f5ac735a0b0654 Mon Sep 17 00:00:00 2001 From: tangtaoit Date: Mon, 29 Apr 2024 22:25:45 +0800 Subject: [PATCH] feat: ConnackPacket add NodeId --- common.go | 1 + connack.go | 13 +++++++++++++ go.mod | 1 + go.sum | 2 ++ protocol.go | 2 +- 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/common.go b/common.go index 0eb9f37..c8c58b6 100755 --- a/common.go +++ b/common.go @@ -333,6 +333,7 @@ const ( StreamSeqByteSize = 4 StreamFlagByteSize = 1 ExpireByteSize = 4 + NodeIdByteSize = 8 ) const ( diff --git a/connack.go b/connack.go index fd0c571..415ac19 100755 --- a/connack.go +++ b/connack.go @@ -14,6 +14,7 @@ type ConnackPacket struct { Salt string // salt TimeDiff int64 // 客户端时间与服务器的差值,单位毫秒。 ReasonCode ReasonCode // 原因码 + NodeId uint64 // 节点Id } // GetFrameType 获取包类型 @@ -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 } @@ -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 } @@ -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 } diff --git a/go.mod b/go.mod index ed5ac84..49990d6 100644 --- a/go.mod +++ b/go.mod @@ -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 ( diff --git a/go.sum b/go.sum index 57d96e6..663ac41 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/protocol.go b/protocol.go index 05ab7bf..788560f 100755 --- a/protocol.go +++ b/protocol.go @@ -27,7 +27,7 @@ type WKProto struct { } // LatestVersion 最新版本 -const LatestVersion = 3 +const LatestVersion = 4 // MaxRemaingLength 最大剩余长度 // 1<<28 - 1 const MaxRemaingLength uint32 = 1024 * 1024