Skip to content

Commit

Permalink
feat: upgrade proto version
Browse files Browse the repository at this point in the history
  • Loading branch information
tangtaoit committed Oct 8, 2023
1 parent 362f214 commit 9cee8d8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
2 changes: 2 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
}

Expand Down
23 changes: 18 additions & 5 deletions connack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 获取包类型
Expand All @@ -24,15 +25,21 @@ 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
}

func encodeConnackSize(packet *ConnackPacket, version uint8) int {
size := 0
if version > 3 {
size += VersionByteSize
}
size += TimeDiffByteSize
size += ReasonCodeByteSize
size += (len(packet.ServerKey) + StringFixLenByteSize)
Expand All @@ -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失败!")
}
Expand Down
10 changes: 6 additions & 4 deletions connack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
// 编码
Expand All @@ -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)
}
6 changes: 0 additions & 6 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 9cee8d8

Please sign in to comment.