Skip to content

Commit

Permalink
fixed Order bug
Browse files Browse the repository at this point in the history
  • Loading branch information
MacBookAirM2 committed Mar 14, 2023
1 parent bea1a6d commit f9a3939
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
8 changes: 5 additions & 3 deletions examples/zinx_decoder/decode/htlvcrcdecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ package decode

import (
"encoding/hex"
"fmt"
"github.com/aceld/zinx/examples/zinx_decoder/bili/utils"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zlog"
"unsafe"
)

const HEADER_SIZE = 5
Expand All @@ -50,7 +51,7 @@ func (this *HtlvCrcDecoder) Intercept(chain ziface.Chain) ziface.Response {
iMessage := iRequest.GetMessage()
if iMessage != nil {
data := iMessage.GetData()
fmt.Println("1htlvData", data)
zlog.Ins().DebugF("HTLVCRC-RawData size:%d data:%s\n", len(data), hex.EncodeToString(data))
datasize := len(data)
htlvData := HtlvCrcData{
Data: data,
Expand All @@ -62,12 +63,13 @@ func (this *HtlvCrcDecoder) Intercept(chain ziface.Chain) ziface.Response {
htlvData.Body = data[3 : datasize-2]
htlvData.Crc = data[datasize-2 : datasize]
if !utils.CheckCRC(data[:datasize-2], htlvData.Crc) {
fmt.Println("crc校验失败", hex.EncodeToString(data), hex.EncodeToString(htlvData.Crc))
zlog.Ins().DebugF("crc校验失败 %s %s\n", hex.EncodeToString(data), hex.EncodeToString(htlvData.Crc))
return nil
}
iMessage.SetMsgID(uint32(htlvData.Funcode))
iRequest.SetResponse(htlvData)
//zlog.Ins().DebugF("2htlvData %s \n", hex.EncodeToString(htlvData.data))
zlog.Ins().DebugF("HTLVCRC-DecodeData size:%d data:%+v\n", unsafe.Sizeof(htlvData), htlvData)
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions examples/zinx_decoder/decode/tlvdecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ package decode

import (
"encoding/binary"
"fmt"
"encoding/hex"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zlog"
"unsafe"
)

const TLV_HEADER_SIZE = 8 //表示TLV空包长度
Expand All @@ -45,7 +47,7 @@ func (this *TLVDecoder) Intercept(chain ziface.Chain) ziface.Response {
iMessage := iRequest.GetMessage()
if iMessage != nil {
data := iMessage.GetData()
fmt.Println("1-TLV", len(data), data)
zlog.Ins().DebugF("TLV-RawData size:%d data:%s\n", len(data), hex.EncodeToString(data))
datasize := len(data)
_data := TlvData{}
if datasize >= TLV_HEADER_SIZE {
Expand All @@ -54,7 +56,7 @@ func (this *TLVDecoder) Intercept(chain ziface.Chain) ziface.Response {
_data.Value = string(data[8 : 8+_data.Length])
iMessage.SetMsgID(_data.Tag)
iRequest.SetResponse(_data)
fmt.Println("2-TLV", _data)
zlog.Ins().DebugF("TLV-DecodeData size:%d data:%+v\n", unsafe.Sizeof(data), _data)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/zinx_decoder/router/htlvcrcbusinessrouter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package router

import (
"fmt"
"github.com/aceld/zinx/examples/zinx_decoder/decode"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zlog"
"github.com/aceld/zinx/znet"
)

Expand All @@ -12,15 +12,15 @@ type HtlvCrcBusinessRouter struct {
}

func (this *HtlvCrcBusinessRouter) Handle(request ziface.IRequest) {
fmt.Println("Call HtlvCrcBusinessRouter Handle", request.GetMessage().GetMsgID(), request.GetMessage().GetData())
zlog.Ins().DebugF("Call HtlvCrcBusinessRouter Handle %d %+v\n", request.GetMessage().GetMsgID(), request.GetMessage().GetData())
msgID := request.GetMessage().GetMsgID()
if msgID == 0x10 {
_response := request.GetResponse()
if _response != nil {
switch _response.(type) {
case decode.HtlvCrcData:
tlvData := _response.(decode.HtlvCrcData)
fmt.Println("do msgid=0x10 data business", tlvData)
zlog.Ins().DebugF("do msgid=0x10 data business %+v\n", tlvData)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/zinx_decoder/router/tlvbusinessrouter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package router

import (
"fmt"
"github.com/aceld/zinx/examples/zinx_decoder/decode"
"github.com/aceld/zinx/ziface"
"github.com/aceld/zinx/zlog"
"github.com/aceld/zinx/znet"
)

Expand All @@ -12,15 +12,15 @@ type TLVBusinessRouter struct {
}

func (this *TLVBusinessRouter) Handle(request ziface.IRequest) {
fmt.Println("Call TLVRouter Handle", request.GetMessage().GetMsgID(), request.GetMessage().GetData())
zlog.Ins().DebugF("Call TLVRouter Handle %d %+v\n", request.GetMessage().GetMsgID(), request.GetMessage().GetData())
msgID := request.GetMessage().GetMsgID()
if msgID == 0x00000001 {
_response := request.GetResponse()
if _response != nil {
switch _response.(type) {
case decode.TlvData:
tlvData := _response.(decode.TlvData)
fmt.Println("do msgid=0x00000001 data business", tlvData)
zlog.Ins().DebugF("do msgid=0x00000001 data business %+v\n", tlvData)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/zinx_decoder/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func main() {
s.AddRouter(0x00000001, &router.TLVBusinessRouter{}) //TLV协议对应业务功能

//处理HTLVCRC协议数据
s.AddInterceptor(&decode.HtlvCrcDecoder{}) //TVL协议解码器
s.AddRouter(0x10, &router.HtlvCrcBusinessRouter{}) //TLV协议对应业务功能,因为client.go中模拟数据funcode字段为0x10
//s.AddInterceptor(&decode.HtlvCrcDecoder{}) //TVL协议解码器
//s.AddRouter(0x10, &router.HtlvCrcBusinessRouter{}) //TLV协议对应业务功能,因为client.go中模拟数据funcode字段为0x10

//开启服务
s.Serve()
Expand Down
29 changes: 14 additions & 15 deletions zcode/lengthfieldframedecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ import (
// | HDR1 | Length | HDR2 | Actual Content |----->| HDR2 | Actual Content |
// | 0xCA | 0x0010 | 0xFE | "HELLO, WORLD" | | 0xFE | "HELLO, WORLD" |
// +------+--------+------+----------------+ +------+----------------+
// https://blog.csdn.net/weixin_45271492/article/details/125347939

type EncoderData struct {
lengthField ziface.LengthField
Expand All @@ -188,26 +187,27 @@ type EncoderData struct {
}

func NewLengthFieldFrameDecoderByLengthField(lengthField ziface.LengthField) ziface.IDecoder {
return &EncoderData{
c := &EncoderData{
lengthField: lengthField,
LengthFieldEndOffset: lengthField.LengthFieldOffset + lengthField.LengthFieldLength,
in: make([]byte, 0),
}
if c.lengthField.Order == nil {
c.lengthField.Order = binary.BigEndian
}
return c

}

func NewLengthFieldFrameDecoder(maxFrameLength int64, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip int) ziface.IDecoder {
return &EncoderData{
lengthField: ziface.LengthField{
MaxFrameLength: maxFrameLength,
LengthFieldOffset: lengthFieldOffset,
LengthFieldLength: lengthFieldLength,
LengthAdjustment: lengthAdjustment,
InitialBytesToStrip: initialBytesToStrip,
Order: binary.BigEndian,
},
LengthFieldEndOffset: lengthFieldOffset + lengthFieldLength,
in: make([]byte, 0),
}
return NewLengthFieldFrameDecoderByLengthField(ziface.LengthField{
MaxFrameLength: maxFrameLength,
LengthFieldOffset: lengthFieldOffset,
LengthFieldLength: lengthFieldLength,
LengthAdjustment: lengthAdjustment,
InitialBytesToStrip: initialBytesToStrip,
Order: binary.BigEndian,
})
}

func (this *EncoderData) fail(frameLength int64) {
Expand Down Expand Up @@ -334,7 +334,6 @@ func (this *EncoderData) failOnFrameLengthLessThanInitialBytesToStrip(in *bytes.
panic(fmt.Sprintf("Adjusted frame length (%d) is less than InitialBytesToStrip: %d", frameLength, initialBytesToStrip))
}

// https://blog.csdn.net/qq_39280718/article/details/125762004
func (this *EncoderData) decode(buf []byte) []byte {
in := bytes.NewBuffer(buf)
//丢弃模式
Expand Down

0 comments on commit f9a3939

Please sign in to comment.