diff --git a/common.go b/common.go index 828d5f7..46e5ec2 100755 --- a/common.go +++ b/common.go @@ -17,6 +17,7 @@ type Framer struct { // ToFixHeaderUint8 ToFixHeaderUint8 func ToFixHeaderUint8(f Frame) uint8 { typeAndFlags := encodeBool(f.GetDUP())<<3 | encodeBool(f.GetsyncOnce())<<2 | encodeBool(f.GetRedDot())<<1 | encodeBool(f.GetNoPersist()) + return byte(int(f.GetFrameType()<<4) | typeAndFlags) } @@ -28,6 +29,7 @@ func FramerFromUint8(v uint8) Framer { p.SyncOnce = (v >> 2 & 0x01) > 0 p.DUP = (v >> 3 & 0x01) > 0 p.FrameType = FrameType(v >> 4) + return p } diff --git a/connack.go b/connack.go index ef6abe7..cae56a9 100755 --- a/connack.go +++ b/connack.go @@ -9,10 +9,11 @@ import ( // ConnackPacket 连接回执包 type ConnackPacket struct { Framer - ServerKey string // 服务端的DH公钥 - Salt string // salt - TimeDiff int64 // 客户端时间与服务器的差值,单位毫秒。 - ReasonCode ReasonCode // 原因码 + ServerVersion uint8 // 服务端版本 + ServerKey string // 服务端的DH公钥 + Salt string // salt + TimeDiff int64 // 客户端时间与服务器的差值,单位毫秒。 + ReasonCode ReasonCode // 原因码 } // GetFrameType 获取包类型 @@ -24,8 +25,11 @@ func (c ConnackPacket) String() string { } func encodeConnack(connack *ConnackPacket, enc *Encoder, version uint8) error { + if version > 3 { + enc.WriteUint8(connack.ServerVersion) + } enc.WriteInt64(connack.TimeDiff) - enc.WriteByte(connack.ReasonCode.Byte()) + _ = enc.WriteByte(connack.ReasonCode.Byte()) enc.WriteString(connack.ServerKey) enc.WriteString(connack.Salt) return nil @@ -33,6 +37,9 @@ func encodeConnack(connack *ConnackPacket, enc *Encoder, version uint8) error { func encodeConnackSize(packet *ConnackPacket, version uint8) int { size := 0 + if version > 3 { + size += VersionByteSize + } size += TimeDiffByteSize size += ReasonCodeByteSize size += (len(packet.ServerKey) + StringFixLenByteSize) @@ -47,6 +54,12 @@ func decodeConnack(frame Frame, data []byte, version uint8) (Frame, error) { var err error + if version > 3 { + if connackPacket.ServerVersion, err = dec.Uint8(); err != nil { + return nil, errors.Wrap(err, "解码version失败!") + } + } + if connackPacket.TimeDiff, err = dec.Int64(); err != nil { return nil, errors.Wrap(err, "解码TimeDiff失败!") } diff --git a/connack_test.go b/connack_test.go index 4f2c8dd..75d2f02 100755 --- a/connack_test.go +++ b/connack_test.go @@ -8,10 +8,11 @@ import ( func TestConnackEncodeAndDecode(t *testing.T) { packet := &ConnackPacket{ - TimeDiff: 12345, - ReasonCode: ReasonSuccess, - ServerKey: "ServerKey", - Salt: "Salt", + TimeDiff: 12345, + ReasonCode: ReasonSuccess, + ServerKey: "ServerKey", + Salt: "Salt", + ServerVersion: 100, } codec := New() // 编码 @@ -28,4 +29,5 @@ func TestConnackEncodeAndDecode(t *testing.T) { assert.Equal(t, packet.ReasonCode, resultConnackPacket.ReasonCode) assert.Equal(t, packet.ServerKey, resultConnackPacket.ServerKey) assert.Equal(t, packet.Salt, resultConnackPacket.Salt) + assert.Equal(t, packet.ServerVersion, resultConnackPacket.ServerVersion) } diff --git a/connect.go b/connect.go index 43fb4e4..7709deb 100755 --- a/connect.go +++ b/connect.go @@ -18,12 +18,6 @@ type ConnectPacket struct { Token string // token } -// ToFixHeaderUint8 ToFixHeaderUint8 -func (c ConnectPacket) ToFixHeaderUint8() uint8 { - typeAndFlags := encodeBool(c.GetDUP())<<3 | encodeBool(c.GetsyncOnce())<<2 | encodeBool(c.GetRedDot())<<1 | encodeBool(c.GetNoPersist()) - return byte(int(c.GetFrameType()<<4) | typeAndFlags) -} - // GetFrameType 包类型 func (c ConnectPacket) GetFrameType() FrameType { return CONNECT diff --git a/protocol.go b/protocol.go index 1ab19bd..b34ce1c 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