2022-10-11 17:36:09 +08:00
|
|
|
package frame
|
|
|
|
|
|
|
|
import (
|
|
|
|
coder "git.hpds.cc/Component/mq_coder"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GoawayFrame is a coder encoded bytes, Tag is a fixed value TYPE_ID_GOAWAY_FRAME
|
|
|
|
type GoawayFrame struct {
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGoawayFrame creates a new GoawayFrame
|
|
|
|
func NewGoawayFrame(msg string) *GoawayFrame {
|
|
|
|
return &GoawayFrame{message: msg}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type gets the type of Frame.
|
|
|
|
func (f *GoawayFrame) Type() Type {
|
|
|
|
return TagOfGoawayFrame
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode to coder encoded bytes
|
|
|
|
func (f *GoawayFrame) Encode() []byte {
|
2023-03-10 23:49:52 +08:00
|
|
|
goaway := coder.NewNodePacketEncoder(f.Type())
|
2022-10-11 17:36:09 +08:00
|
|
|
// message
|
2023-03-10 23:49:52 +08:00
|
|
|
msgBlock := coder.NewPrimitivePacketEncoder(TagOfGoawayMessage)
|
2022-10-11 17:36:09 +08:00
|
|
|
msgBlock.SetStringValue(f.message)
|
|
|
|
|
|
|
|
goaway.AddPrimitivePacket(msgBlock)
|
|
|
|
|
|
|
|
return goaway.Encode()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message goaway message
|
|
|
|
func (f *GoawayFrame) Message() string {
|
|
|
|
return f.message
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeToGoawayFrame decodes coder encoded bytes to GoawayFrame
|
|
|
|
func DecodeToGoawayFrame(buf []byte) (*GoawayFrame, error) {
|
|
|
|
node := coder.NodePacket{}
|
|
|
|
_, err := coder.DecodeToNodePacket(buf, &node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
goaway := &GoawayFrame{}
|
|
|
|
// message
|
2023-03-10 23:49:52 +08:00
|
|
|
if msgBlock, ok := node.PrimitivePackets[TagOfGoawayMessage]; ok {
|
2022-10-11 17:36:09 +08:00
|
|
|
msg, err := msgBlock.ToUTF8String()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
goaway.message = msg
|
|
|
|
}
|
|
|
|
return goaway, nil
|
|
|
|
}
|