Skip to content

Commit

Permalink
feat: add server version
Browse files Browse the repository at this point in the history
  • Loading branch information
tangtaoit committed Oct 9, 2023
1 parent 9cee8d8 commit 552aa03
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 20 deletions.
27 changes: 19 additions & 8 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@ import "fmt"

// Framer 包的基础framer
type Framer struct {
FrameType FrameType
RemainingLength uint32 // 控制报文总长度等于固定报头的长度加上剩余长度
NoPersist bool // 是否不持久化
RedDot bool // 是否显示红点
SyncOnce bool // 此消息只被同步或被消费一次
DUP bool // 是否是重发消息
FrameType FrameType
RemainingLength uint32 // 控制报文总长度等于固定报头的长度加上剩余长度
NoPersist bool // 是否不持久化
RedDot bool // 是否显示红点
SyncOnce bool // 此消息只被同步或被消费一次
DUP bool // 是否是重发消息
HasServerVersion bool // 是否有服务端版本 connack包用到

FrameSize int64
}

// ToFixHeaderUint8 ToFixHeaderUint8
func ToFixHeaderUint8(f Frame) uint8 {
typeAndFlags := encodeBool(f.GetDUP())<<3 | encodeBool(f.GetsyncOnce())<<2 | encodeBool(f.GetRedDot())<<1 | encodeBool(f.GetNoPersist())

if f.GetFrameType() == CONNACK {
typeAndFlags = encodeBool(f.GetHasServerVersion())
}
return byte(int(f.GetFrameType()<<4) | typeAndFlags)
}

Expand All @@ -29,6 +32,9 @@ func FramerFromUint8(v uint8) Framer {
p.SyncOnce = (v >> 2 & 0x01) > 0
p.DUP = (v >> 3 & 0x01) > 0
p.FrameType = FrameType(v >> 4)
if p.FrameType == CONNACK {
p.HasServerVersion = (v & 0x01) > 0
}

return p
}
Expand Down Expand Up @@ -67,6 +73,10 @@ func (f Framer) GetDUP() bool {
return f.DUP
}

func (f Framer) GetHasServerVersion() bool {
return f.HasServerVersion
}

func (f Framer) String() string {
return fmt.Sprintf("packetType: %s remainingLength:%d NoPersist:%v redDot:%v syncOnce:%v DUP:%v", f.GetFrameType().String(), f.RemainingLength, f.NoPersist, f.RedDot, f.SyncOnce, f.DUP)
}
Expand Down Expand Up @@ -294,7 +304,8 @@ type Frame interface {
GetsyncOnce() bool
// 是否是重发的消息
GetDUP() bool
GetFrameSize() int64 // 总个frame的大小(不参与编码解码)
GetFrameSize() int64 // 总个frame的大小(不参与编码解码)
GetHasServerVersion() bool // 是否有服务端版本 connack包用到
}

type Channel struct {
Expand Down
6 changes: 3 additions & 3 deletions connack.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c ConnackPacket) String() string {
}

func encodeConnack(connack *ConnackPacket, enc *Encoder, version uint8) error {
if version > 3 {
if connack.GetHasServerVersion() {
enc.WriteUint8(connack.ServerVersion)
}
enc.WriteInt64(connack.TimeDiff)
Expand All @@ -37,7 +37,7 @@ func encodeConnack(connack *ConnackPacket, enc *Encoder, version uint8) error {

func encodeConnackSize(packet *ConnackPacket, version uint8) int {
size := 0
if version > 3 {
if packet.GetHasServerVersion() {
size += VersionByteSize
}
size += TimeDiffByteSize
Expand All @@ -54,7 +54,7 @@ func decodeConnack(frame Frame, data []byte, version uint8) (Frame, error) {

var err error

if version > 3 {
if frame.GetHasServerVersion() {
if connackPacket.ServerVersion, err = dec.Uint8(); err != nil {
return nil, errors.Wrap(err, "解码version失败!")
}
Expand Down
1 change: 1 addition & 0 deletions connack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestConnackEncodeAndDecode(t *testing.T) {
Salt: "Salt",
ServerVersion: 100,
}
packet.HasServerVersion = true
codec := New()
// 编码
packetBytes, err := codec.EncodeFrame(packet, 4)
Expand Down
4 changes: 2 additions & 2 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (e *Encoder) WriteInt(i int) error {

// WriteUint8 WriteUint8
func (e *Encoder) WriteUint8(i uint8) {
e.WriteInt(int(i))
_ = e.WriteInt(int(i))
}

// WriteInt16 WriteInt16
Expand Down Expand Up @@ -95,7 +95,7 @@ func (e *Encoder) WriteUint64(i uint64) {

// WriteUint32 WriteUint32
func (e *Encoder) WriteUint32(i uint32) {
WriteUint32(i, e.w)
_ = WriteUint32(i, e.w)
}

// WriteString WriteString
Expand Down
8 changes: 4 additions & 4 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type WKProto struct {
}

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

// MaxRemaingLength 最大剩余长度 // 1<<28 - 1
const MaxRemaingLength uint32 = 1024 * 1024
Expand Down Expand Up @@ -243,7 +243,7 @@ func (l *WKProto) EncodeFrame(frame Frame, version uint8) ([]byte, error) {

func (l *WKProto) encodeFrame(f Frame, enc *Encoder, remainingLength uint32) {

enc.WriteByte(ToFixHeaderUint8(f))
_ = enc.WriteByte(ToFixHeaderUint8(f))

encodeVariable2(remainingLength, enc)
}
Expand Down Expand Up @@ -317,7 +317,7 @@ func encodeVariable2(size uint32, enc *Encoder) {
if size > 0 {
digit |= 0x80
}
enc.WriteByte(digit)
_ = enc.WriteByte(digit)
}
}
func decodeLength(data []byte) (uint32, uint32, error) {
Expand All @@ -343,7 +343,7 @@ func decodeLengthWithConn(r io.Reader) int {
var multiplier uint32
for multiplier < 27 { //fix: Infinite '(digit & 128) == 1' will cause the dead loop
b := make([]byte, 1)
io.ReadFull(r, b)
_, _ = io.ReadFull(r, b)
digit := b[0]
rLength |= uint32(digit&127) << multiplier
if (digit & 128) == 0 {
Expand Down
2 changes: 1 addition & 1 deletion recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func decodeRecv(frame Frame, data []byte, version uint8) (Frame, error) {

func encodeRecv(recvPacket *RecvPacket, enc *Encoder, version uint8) error {
// setting
enc.WriteByte(recvPacket.Setting.Uint8())
_ = enc.WriteByte(recvPacket.Setting.Uint8())
// MsgKey
enc.WriteString(recvPacket.MsgKey)
// 发送者
Expand Down
2 changes: 1 addition & 1 deletion send.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func decodeSend(frame Frame, data []byte, version uint8) (Frame, error) {
func encodeSend(frame Frame, enc *Encoder, version uint8) error {
sendPacket := frame.(*SendPacket)

enc.WriteByte(sendPacket.Setting.Uint8())
_ = enc.WriteByte(sendPacket.Setting.Uint8())
// 消息序列号(客户端维护)
enc.WriteUint32(uint32(sendPacket.ClientSeq))
// 客户端唯一标示
Expand Down
2 changes: 1 addition & 1 deletion sub.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func decodeSub(frame Frame, data []byte, version uint8) (Frame, error) {

func encodeSub(frame Frame, enc *Encoder, version uint8) error {
subPacket := frame.(*SubPacket)
enc.WriteByte(subPacket.Setting.Uint8())
_ = enc.WriteByte(subPacket.Setting.Uint8())
// 客户端消息编号
enc.WriteString(subPacket.SubNo)
// 频道ID
Expand Down

0 comments on commit 552aa03

Please sign in to comment.