network/frame/goaway_frame.go

58 lines
1.3 KiB
Go
Raw Normal View History

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