2022-10-11 17:36:09 +08:00
|
|
|
package frame
|
|
|
|
|
|
|
|
import (
|
|
|
|
coder "git.hpds.cc/Component/mq_coder"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BackFlowFrame is a coder encoded bytes
|
|
|
|
// It's used to receive stream function processed result
|
|
|
|
type BackFlowFrame struct {
|
2023-04-05 16:15:59 +08:00
|
|
|
Tag Tag
|
2022-10-11 17:36:09 +08:00
|
|
|
Carriage []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBackFlowFrame creates a new BackFlowFrame with a given tag and carriage
|
2023-04-05 16:15:59 +08:00
|
|
|
func NewBackFlowFrame(tag Tag, carriage []byte) *BackFlowFrame {
|
2022-10-11 17:36:09 +08:00
|
|
|
return &BackFlowFrame{
|
|
|
|
Tag: tag,
|
|
|
|
Carriage: carriage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type gets the type of Frame.
|
|
|
|
func (f *BackFlowFrame) Type() Type {
|
|
|
|
return TagOfBackFlowFrame
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCarriage sets the user's raw data
|
|
|
|
func (f *BackFlowFrame) SetCarriage(buf []byte) *BackFlowFrame {
|
|
|
|
f.Carriage = buf
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode to coder encoded bytes
|
|
|
|
func (f *BackFlowFrame) Encode() []byte {
|
2023-04-05 16:15:59 +08:00
|
|
|
tag := coder.NewPrimitivePacketEncoder(byte(TagOfBackFlowDataTag))
|
|
|
|
tag.SetUInt32Value(uint32(f.Tag))
|
|
|
|
carriage := coder.NewPrimitivePacketEncoder(byte(TagOfBackFlowCarriage))
|
2022-10-11 17:36:09 +08:00
|
|
|
carriage.SetBytesValue(f.Carriage)
|
|
|
|
|
|
|
|
node := coder.NewNodePacketEncoder(byte(TagOfBackFlowFrame))
|
2023-04-05 16:15:59 +08:00
|
|
|
node.AddPrimitivePacket(tag)
|
2022-10-11 17:36:09 +08:00
|
|
|
node.AddPrimitivePacket(carriage)
|
|
|
|
|
|
|
|
return node.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDataTag return the Tag of user's data
|
2023-04-05 16:15:59 +08:00
|
|
|
func (f *BackFlowFrame) GetDataTag() Tag {
|
2022-10-11 17:36:09 +08:00
|
|
|
return f.Tag
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCarriage return data
|
|
|
|
func (f *BackFlowFrame) GetCarriage() []byte {
|
|
|
|
return f.Carriage
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeToBackFlowFrame decodes coder encoded bytes to BackFlowFrame
|
|
|
|
func DecodeToBackFlowFrame(buf []byte) (*BackFlowFrame, error) {
|
|
|
|
nodeBlock := coder.NodePacket{}
|
|
|
|
_, err := coder.DecodeToNodePacket(buf, &nodeBlock)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := &BackFlowFrame{}
|
2023-04-05 16:15:59 +08:00
|
|
|
if p, ok := nodeBlock.PrimitivePackets[byte(TagOfBackFlowDataTag)]; ok {
|
|
|
|
tag, err := p.ToUInt32()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
payload.Tag = Tag(tag)
|
|
|
|
}
|
|
|
|
if p, ok := nodeBlock.PrimitivePackets[byte(TagOfBackFlowCarriage)]; ok {
|
|
|
|
payload.Carriage = p.GetValBuf()
|
2022-10-11 17:36:09 +08:00
|
|
|
}
|
|
|
|
return payload, nil
|
|
|
|
}
|