56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package frame
|
|
|
|
import (
|
|
coder "git.hpds.cc/Component/mq_coder"
|
|
)
|
|
|
|
// 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 {
|
|
Tag byte
|
|
Carriage []byte
|
|
}
|
|
|
|
// NewPayloadFrame creates a new PayloadFrame with a given TagId of user's data
|
|
func NewPayloadFrame(tag byte) *PayloadFrame {
|
|
return &PayloadFrame{
|
|
Tag: tag,
|
|
}
|
|
}
|
|
|
|
// 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 {
|
|
carriage := coder.NewPrimitivePacketEncoder(m.Tag)
|
|
carriage.SetBytesValue(m.Carriage)
|
|
|
|
payload := coder.NewNodePacketEncoder(TagOfPayloadFrame)
|
|
payload.AddPrimitivePacket(carriage)
|
|
|
|
return payload.Encode()
|
|
}
|
|
|
|
// DecodeToPayloadFrame decodes coder encoded bytes to PayloadFrame
|
|
func DecodeToPayloadFrame(buf []byte) (*PayloadFrame, error) {
|
|
nodeBlock := coder.NodePacket{}
|
|
_, err := coder.DecodeToNodePacket(buf, &nodeBlock)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
payload := &PayloadFrame{}
|
|
for _, v := range nodeBlock.PrimitivePackets {
|
|
payload.Tag = v.SeqId()
|
|
payload.Carriage = v.GetValBuf()
|
|
break
|
|
}
|
|
|
|
return payload, nil
|
|
}
|