network/frame/goaway_frame.go

58 lines
1.3 KiB
Go

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 {
goaway := coder.NewNodePacketEncoder(f.Type())
// message
msgBlock := coder.NewPrimitivePacketEncoder(TagOfGoawayMessage)
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
if msgBlock, ok := node.PrimitivePackets[TagOfGoawayMessage]; ok {
msg, err := msgBlock.ToUTF8String()
if err != nil {
return nil, err
}
goaway.message = msg
}
return goaway, nil
}