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-10 23:49:52 +08:00
|
|
|
rejected := coder.NewNodePacketEncoder(f.Type())
|
2022-10-11 17:36:09 +08:00
|
|
|
// message
|
2023-03-10 23:49:52 +08:00
|
|
|
msgBlock := coder.NewPrimitivePacketEncoder(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-10 23:49:52 +08:00
|
|
|
if msgBlock, ok := node.PrimitivePackets[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
|
|
|
|
}
|