Skip to content

Commit

Permalink
feat: msg add expire
Browse files Browse the repository at this point in the history
  • Loading branch information
tangtaoit committed Sep 20, 2023
1 parent 848a7e7 commit 362f214
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 7 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@

WuKongIM Protocol(WKProto)是一套基于TCP的二进制协议,用于客户端与服务端之间的通信。WKProto的设计目标是简单、高效、易扩展。

## 2. 协议格式
## 2. 使用

```
go get github.com/WuKongIM/WuKongIMGoProto
```

```
import (
wkproto "github.com/WuKongIM/WuKongIMGoProto"
...
)
```

## 3. 协议格式

[协议文档](https://githubim.com/guide/proto.html)
1 change: 1 addition & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ const (
ActionByteSize = 1
StreamSeqByteSize = 4
StreamFlagByteSize = 1
ExpireByteSize = 4
)

const (
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 = 2
const LatestVersion = 3

// MaxRemaingLength 最大剩余长度 // 1<<28 - 1
const MaxRemaingLength uint32 = 1024 * 1024
Expand Down
16 changes: 15 additions & 1 deletion recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type RecvPacket struct {
Framer
Setting Setting
MsgKey string // 用于验证此消息是否合法(仿中间人篡改)
Expire uint32 // 消息过期时间 0 表示永不过期
MessageID int64 // 服务端的消息ID(全局唯一)
MessageSeq uint32 // 消息序列号 (用户唯一,有序递增)
ClientMsgNo string // 客户端唯一标示
Expand Down Expand Up @@ -46,7 +47,7 @@ func (r *RecvPacket) VerityString() string {
return fmt.Sprintf("%d%d%s%d%s%s%d%s", r.MessageID, r.MessageSeq, r.ClientMsgNo, r.Timestamp, r.FromUID, r.ChannelID, r.ChannelType, string(r.Payload))
}
func (r *RecvPacket) String() string {
return fmt.Sprintf("recv Header:%s Setting:%d MessageID:%d MessageSeq:%d Timestamp:%d FromUid:%s ChannelID:%s ChannelType:%d Topic:%s Payload:%s", r.Framer, r.Setting, r.MessageID, r.MessageSeq, r.Timestamp, r.FromUID, r.ChannelID, r.ChannelType, r.Topic, string(r.Payload))
return fmt.Sprintf("recv Header:%s Setting:%d MessageID:%d MessageSeq:%d Timestamp:%d Expire:%d FromUid:%s ChannelID:%s ChannelType:%d Topic:%s Payload:%s", r.Framer, r.Setting, r.MessageID, r.MessageSeq, r.Timestamp, r.Expire, r.FromUID, r.ChannelID, r.ChannelType, r.Topic, string(r.Payload))
}

func decodeRecv(frame Frame, data []byte, version uint8) (Frame, error) {
Expand Down Expand Up @@ -75,6 +76,13 @@ func decodeRecv(frame Frame, data []byte, version uint8) (Frame, error) {
if recvPacket.ChannelType, err = dec.Uint8(); err != nil {
return nil, errors.Wrap(err, "解码ChannelType失败!")
}
if version >= 3 {
var expire uint32
if expire, err = dec.Uint32(); err != nil {
return nil, errors.Wrap(err, "解码Expire失败!")
}
recvPacket.Expire = expire
}
// 客户端唯一标示
if recvPacket.ClientMsgNo, err = dec.String(); err != nil {
return nil, errors.Wrap(err, "解码ClientMsgNo失败!")
Expand Down Expand Up @@ -144,6 +152,9 @@ func encodeRecv(recvPacket *RecvPacket, enc *Encoder, version uint8) error {
enc.WriteString(recvPacket.ChannelID)
// 频道类型
enc.WriteUint8(recvPacket.ChannelType)
if version >= 3 {
enc.WriteUint32(recvPacket.Expire)
}
// 客户端唯一标示
enc.WriteString(recvPacket.ClientMsgNo)
// 流消息
Expand Down Expand Up @@ -175,6 +186,9 @@ func encodeRecvSize(packet *RecvPacket, version uint8) int {
size += (len(packet.FromUID) + StringFixLenByteSize)
size += (len(packet.ChannelID) + StringFixLenByteSize)
size += ChannelTypeByteSize
if version >= 3 {
size += ExpireByteSize
}
size += (len(packet.ClientMsgNo) + StringFixLenByteSize)
if version >= 2 && packet.Setting.IsSet(SettingStream) {
size += (len(packet.StreamNo) + StringFixLenByteSize)
Expand Down
6 changes: 4 additions & 2 deletions recv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestRecvEncodeAndDecode(t *testing.T) {

packet := &RecvPacket{
MessageID: 1223,
Expire: 10,
MessageSeq: 9238934,
Timestamp: int32(time.Now().Unix()),
ChannelID: "3434",
Expand All @@ -26,11 +27,11 @@ func TestRecvEncodeAndDecode(t *testing.T) {

codec := New()
// 编码
packetBytes, err := codec.EncodeFrame(packet, 1)
packetBytes, err := codec.EncodeFrame(packet, 3)
assert.NoError(t, err)

// 解码
resultPacket, _, err := codec.DecodeFrame(packetBytes, 1)
resultPacket, _, err := codec.DecodeFrame(packetBytes, 3)
assert.NoError(t, err)
resultRecvPacket, ok := resultPacket.(*RecvPacket)
fmt.Println("resultRecvPacket--->", resultRecvPacket)
Expand All @@ -43,6 +44,7 @@ func TestRecvEncodeAndDecode(t *testing.T) {
assert.Equal(t, packet.ChannelID, resultRecvPacket.ChannelID)
assert.Equal(t, packet.ChannelType, resultRecvPacket.ChannelType)
assert.Equal(t, packet.Payload, resultRecvPacket.Payload)
assert.Equal(t, packet.Expire, resultRecvPacket.Expire)

assert.Equal(t, packet.Framer.GetNoPersist(), resultRecvPacket.Framer.GetNoPersist())
assert.Equal(t, packet.Framer.GetRedDot(), resultRecvPacket.Framer.GetRedDot())
Expand Down
17 changes: 16 additions & 1 deletion send.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type SendPacket struct {
Framer
Setting Setting
MsgKey string // 用于验证此消息是否合法(仿中间人篡改)
Expire uint32 // 消息过期时间 0 表示永不过期
ClientSeq uint64 // 客户端提供的序列号,在客户端内唯一
ClientMsgNo string // 客户端消息唯一编号一般是uuid,为了去重
StreamNo string // 流式编号
Expand All @@ -31,7 +32,7 @@ func (s *SendPacket) GetFrameType() FrameType {
}

func (s *SendPacket) String() string {
return fmt.Sprintf("Setting:%v MsgKey:%s ClientSeq:%d ClientMsgNo:%s ChannelId:%s ChannelType:%d Topic:%s Payload:%s", s.Setting, s.MsgKey, s.ClientSeq, s.ClientMsgNo, s.ChannelID, s.ChannelType, s.Topic, string(s.Payload))
return fmt.Sprintf("Setting:%v MsgKey:%s Expire: %d ClientSeq:%d ClientMsgNo:%s ChannelId:%s ChannelType:%d Topic:%s Payload:%s", s.Setting, s.MsgKey, s.Expire, s.ClientSeq, s.ClientMsgNo, s.ChannelID, s.ChannelType, s.Topic, string(s.Payload))
}

// func (s *SendPacket) reset() {
Expand Down Expand Up @@ -106,6 +107,13 @@ func decodeSend(frame Frame, data []byte, version uint8) (Frame, error) {

return nil, errors.Wrap(err, "解码ChannelType失败!")
}
// 消息过期时间
if version >= 3 {
if sendPacket.Expire, err = dec.Uint32(); err != nil {
return nil, errors.Wrap(err, "解码Expire失败!")
}
}

// msg key
if sendPacket.MsgKey, err = dec.String(); err != nil {
return nil, errors.Wrap(err, "解码MsgKey失败!")
Expand Down Expand Up @@ -139,6 +147,10 @@ func encodeSend(frame Frame, enc *Encoder, version uint8) error {
enc.WriteString(sendPacket.ChannelID)
// 频道类型
enc.WriteUint8(sendPacket.ChannelType)
// 消息过期时间
if version >= 3 {
enc.WriteUint32(sendPacket.Expire)
}
// msgKey
enc.WriteString(sendPacket.MsgKey)

Expand All @@ -162,6 +174,9 @@ func encodeSendSize(frame Frame, version uint8) int {
}
size += (len(sendPacket.ChannelID) + StringFixLenByteSize)
size += ChannelTypeByteSize
if version >= 3 {
size += ExpireByteSize
}
size += (len(sendPacket.MsgKey) + StringFixLenByteSize)
if sendPacket.Setting.IsSet(SettingTopic) {
size += (len(sendPacket.Topic) + StringFixLenByteSize)
Expand Down
4 changes: 3 additions & 1 deletion send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ func TestSendEncodeAndDecode(t *testing.T) {
Framer: Framer{
RedDot: true,
},
Expire: 100,
Setting: setting,
ClientSeq: 2,
ChannelID: "34341",
ChannelType: 2,
Payload: []byte("dsdsdsd"),
Payload: []byte("dsdsdsd1"),
}
packet.RedDot = true

Expand All @@ -36,6 +37,7 @@ func TestSendEncodeAndDecode(t *testing.T) {
assert.Equal(t, packet.ClientSeq, resultSendPacket.ClientSeq)
assert.Equal(t, packet.ChannelID, resultSendPacket.ChannelID)
assert.Equal(t, packet.ChannelType, resultSendPacket.ChannelType)
assert.Equal(t, packet.Expire, resultSendPacket.Expire)
assert.Equal(t, packet.RedDot, resultSendPacket.RedDot)
assert.Equal(t, packet.Payload, resultSendPacket.Payload)
assert.Equal(t, packet.Setting, resultSendPacket.Setting)
Expand Down

0 comments on commit 362f214

Please sign in to comment.