1 Star 0 Fork 0

jmesyan/impetus

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
message.go 2.98 KB
一键复制 编辑 原始数据 按行查看 历史
jmesyan 提交于 2020-12-12 22:03 . init
package message
import (
"fmt"
"gitee.com/jmesyan/impetus/protocol"
"gitee.com/jmesyan/impetus/protocol/pomelo/pkg"
)
const (
TYPE_REQUEST = iota
TYPE_NOTIFY //1
TYPE_RESPONSE //2
TYPE_PUSH //3
MSG_FLAG_BYTES = 1
MSG_ROUTE_CODE_BYTES = 2
MSG_ROUTE_CODE_MAX = 0xffff
MSG_COMPRESS_ROUTE_MASK = 0x1
MSG_TYPE_MASK = 0x7
)
// Message protocol encode.
func Encode(id int, msgType int, route uint16, msg []byte) []byte {
idBytes := 0
if msgHasId(msgType) {
idBytes = caculateMsgIdBytes(id)
}
msgLen := MSG_FLAG_BYTES + idBytes;
if msgHasRoute(msgType) {
msgLen += MSG_ROUTE_CODE_BYTES
}
buffer := protocol.GetPoolBuffer(msgLen + len(msg) + pkg.PKG_HEAD_BYTES)
offset := 0
// add flag
offset = encodeMsgFlag(msgType, (*buffer)[pkg.PKG_HEAD_BYTES:], offset)
// add message id
if msgHasId(msgType) {
offset = encodeMsgId(id, idBytes, (*buffer)[pkg.PKG_HEAD_BYTES:], offset)
}
// add route
if msgHasRoute(msgType) {
offset = encodeMsgRoute(route, (*buffer)[pkg.PKG_HEAD_BYTES:], offset)
}
copy((*buffer)[pkg.PKG_HEAD_BYTES+msgLen:], msg)
return *buffer
}
// Message protocol decode.
func Decode(buffer []byte) (id int, msgType int, route uint16, body []byte){
offset := 0
flag := buffer[offset]
offset++
//compressRoute := flag & MSG_COMPRESS_ROUTE_MASK
msgType = int((flag >> 1) & MSG_TYPE_MASK)
if msgHasId(msgType) {
var i uint32 = 0
m := int(buffer[offset])
id += (m & 0x7f) << (7*i)
offset++
i++
for ;m >= 128; {
m = int(buffer[offset])
id += (m & 0x7f) << (7*i)
offset++
i++
}
}
if msgHasRoute(msgType) {
route = uint16(buffer[offset] << 8 | buffer[offset+1])
offset += 2
}
body = buffer[offset:]
return
}
func msgHasId(msgType int) bool {
return msgType == TYPE_REQUEST || msgType == TYPE_RESPONSE
}
func msgHasRoute(msgType int) bool {
return msgType == TYPE_REQUEST || msgType == TYPE_NOTIFY || msgType == TYPE_PUSH
}
func caculateMsgIdBytes(id int) int {
l := 0
for ;id>0; {
l += 1
id >>= 7
}
return l
}
func encodeMsgFlag(msgType int, buffer []byte, offset int) int {
if msgType != TYPE_REQUEST && msgType != TYPE_NOTIFY && msgType != TYPE_RESPONSE && msgType != TYPE_PUSH {
fmt.Printf("unkonw message type: %d", msgType)
return offset
}
compressRoute := 1
if !msgHasRoute(msgType) {
compressRoute = 0
}
buffer[offset] = byte((msgType << 1) | compressRoute)
return offset + MSG_FLAG_BYTES
}
func encodeMsgId(id int, idBytes int, buffer []byte, offset int) int {
tmp := id % 128
next := int(id/128)
if next != 0 {
tmp += 128
}
buffer[offset] = byte(tmp)
offset++
id = next
for ;id != 0; {
tmp = id % 128
next = int(id/128)
if next != 0 {
tmp += 128
}
buffer[offset] = byte(tmp)
offset++
id = next
}
return offset
}
func encodeMsgRoute(route uint16, buffer []byte, offset int) int {
if route > MSG_ROUTE_CODE_MAX {
fmt.Println("route number is overflow")
return offset
}
buffer[offset] = byte((route >> 8) & 0xff)
offset++
buffer[offset] = byte(route & 0xff);
offset++
return offset;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jmesyan/impetus.git
git@gitee.com:jmesyan/impetus.git
jmesyan
impetus
impetus
v1.1.4

搜索帮助

D67c1975 1850385 1daf7b77 1850385