network/frame/rejected_frame.go

57 lines
1.3 KiB
Go
Raw Permalink Normal View History

2022-10-11 17:36:09 +08:00
package frame
import (
coder "git.hpds.cc/Component/mq_coder"
)
// RejectedFrame is a coder encoded bytes, Tag is a fixed value TYPE_ID_REJECTED_FRAME
type RejectedFrame struct {
message string
}
// NewRejectedFrame creates a new RejectedFrame with a given TagId of user's data
func NewRejectedFrame(msg string) *RejectedFrame {
return &RejectedFrame{message: msg}
}
// Type gets the type of Frame.
func (f *RejectedFrame) Type() Type {
return TagOfRejectedFrame
}
// Encode to coder encoded bytes
func (f *RejectedFrame) Encode() []byte {
2023-03-26 23:18:55 +08:00
rejected := coder.NewNodePacketEncoder(byte(f.Type()))
2022-10-11 17:36:09 +08:00
// message
2023-03-26 23:18:55 +08:00
msgBlock := coder.NewPrimitivePacketEncoder(byte(TagOfRejectedMessage))
2022-10-11 17:36:09 +08:00
msgBlock.SetStringValue(f.message)
rejected.AddPrimitivePacket(msgBlock)
return rejected.Encode()
}
// Message rejected message
func (f *RejectedFrame) Message() string {
return f.message
}
// DecodeToRejectedFrame decodes coder encoded bytes to RejectedFrame
func DecodeToRejectedFrame(buf []byte) (*RejectedFrame, error) {
node := coder.NodePacket{}
_, err := coder.DecodeToNodePacket(buf, &node)
if err != nil {
return nil, err
}
rejected := &RejectedFrame{}
// message
2023-03-26 23:18:55 +08:00
if msgBlock, ok := node.PrimitivePackets[byte(TagOfRejectedMessage)]; ok {
2022-10-11 17:36:09 +08:00
msg, e := msgBlock.ToUTF8String()
if e != nil {
return nil, e
}
rejected.message = msg
}
return rejected, nil
}