2022-10-11 17:36:09 +08:00
|
|
|
package frame
|
|
|
|
|
|
|
|
import (
|
|
|
|
coder "git.hpds.cc/Component/mq_coder"
|
|
|
|
)
|
|
|
|
|
2023-04-05 16:15:59 +08:00
|
|
|
type Tag uint32
|
|
|
|
|
2022-10-11 17:36:09 +08:00
|
|
|
// PayloadFrame is a coder encoded bytes, Tag is a fixed value TYPE_ID_PAYLOAD_FRAME
|
|
|
|
// the Len is the length of Val. Val is also a coder encoded PrimitivePacket, storing
|
|
|
|
// raw bytes as user's data
|
|
|
|
type PayloadFrame struct {
|
2023-04-05 16:15:59 +08:00
|
|
|
Tag Tag
|
2022-10-11 17:36:09 +08:00
|
|
|
Carriage []byte
|
|
|
|
}
|
|
|
|
|
2023-04-05 16:15:59 +08:00
|
|
|
//// NewPayloadFrame creates a new PayloadFrame with a given TagId of user's data
|
|
|
|
//func NewPayloadFrame(tag byte) *PayloadFrame {
|
|
|
|
// return &PayloadFrame{
|
|
|
|
// Tag: tag,
|
|
|
|
// }
|
|
|
|
//}
|
2022-10-11 17:36:09 +08:00
|
|
|
|
|
|
|
// SetCarriage sets the user's raw data
|
|
|
|
func (m *PayloadFrame) SetCarriage(buf []byte) *PayloadFrame {
|
|
|
|
m.Carriage = buf
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode to coder encoded bytes
|
|
|
|
func (m *PayloadFrame) Encode() []byte {
|
2023-04-05 16:15:59 +08:00
|
|
|
tag := coder.NewPrimitivePacketEncoder(byte(TagOfPayloadDataTag))
|
|
|
|
tag.SetUInt32Value(uint32(m.Tag))
|
|
|
|
|
|
|
|
carriage := coder.NewPrimitivePacketEncoder(byte(TagOfPayloadCarriage))
|
2022-10-11 17:36:09 +08:00
|
|
|
carriage.SetBytesValue(m.Carriage)
|
|
|
|
|
2023-03-26 23:18:55 +08:00
|
|
|
payload := coder.NewNodePacketEncoder(byte(TagOfPayloadFrame))
|
2023-04-05 16:15:59 +08:00
|
|
|
payload.AddPrimitivePacket(tag)
|
2022-10-11 17:36:09 +08:00
|
|
|
payload.AddPrimitivePacket(carriage)
|
|
|
|
|
|
|
|
return payload.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeToPayloadFrame decodes coder encoded bytes to PayloadFrame
|
2023-04-05 16:15:59 +08:00
|
|
|
func DecodeToPayloadFrame(buf []byte, payload *PayloadFrame) error {
|
2022-10-11 17:36:09 +08:00
|
|
|
nodeBlock := coder.NodePacket{}
|
|
|
|
_, err := coder.DecodeToNodePacket(buf, &nodeBlock)
|
|
|
|
if err != nil {
|
2023-04-05 16:15:59 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p, ok := nodeBlock.PrimitivePackets[byte(TagOfPayloadDataTag)]; ok {
|
|
|
|
tag, err := p.ToUInt32()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
payload.Tag = Tag(tag)
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
|
2023-04-05 16:15:59 +08:00
|
|
|
if p, ok := nodeBlock.PrimitivePackets[byte(TagOfPayloadCarriage)]; ok {
|
|
|
|
payload.Carriage = p.GetValBuf()
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
|
2023-04-05 16:15:59 +08:00
|
|
|
return nil
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|