79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
|
package frame
|
||
|
|
||
|
import (
|
||
|
coder "git.hpds.cc/Component/mq_coder"
|
||
|
)
|
||
|
|
||
|
// CloseStreamFrame is used to close a dataStream, controlStream
|
||
|
// receives CloseStreamFrame and closes dataStream according to the Frame.
|
||
|
// CloseStreamFrame is a coder encoded bytes.
|
||
|
type CloseStreamFrame struct {
|
||
|
streamID string
|
||
|
reason string
|
||
|
}
|
||
|
|
||
|
// StreamID returns the ID of the stream to be closed.
|
||
|
func (f *CloseStreamFrame) StreamID() string { return f.streamID }
|
||
|
|
||
|
// Reason returns the close reason.
|
||
|
func (f *CloseStreamFrame) Reason() string { return f.reason }
|
||
|
|
||
|
// NewCloseStreamFrame returns a CloseStreamFrame.
|
||
|
func NewCloseStreamFrame(streamID, reason string) *CloseStreamFrame {
|
||
|
return &CloseStreamFrame{
|
||
|
streamID: streamID,
|
||
|
reason: reason,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Type gets the type of the CloseStreamFrame.
|
||
|
func (f *CloseStreamFrame) Type() Type {
|
||
|
return TagOfCloseStreamFrame
|
||
|
}
|
||
|
|
||
|
// Encode encodes CloseStreamFrame to coder encoded bytes.
|
||
|
func (f *CloseStreamFrame) Encode() []byte {
|
||
|
// id
|
||
|
idBlock := coder.NewPrimitivePacketEncoder(byte(TagOfCloseStreamID))
|
||
|
idBlock.SetStringValue(f.streamID)
|
||
|
// reason
|
||
|
reasonBlock := coder.NewPrimitivePacketEncoder(byte(TagOfCloseStreamReason))
|
||
|
reasonBlock.SetStringValue(f.reason)
|
||
|
// frame
|
||
|
ack := coder.NewNodePacketEncoder(byte(f.Type()))
|
||
|
ack.AddPrimitivePacket(idBlock)
|
||
|
ack.AddPrimitivePacket(reasonBlock)
|
||
|
|
||
|
return ack.Encode()
|
||
|
}
|
||
|
|
||
|
// DecodeToCloseStreamFrame decodes coder encoded bytes to CloseStreamFrame.
|
||
|
func DecodeToCloseStreamFrame(buf []byte) (*CloseStreamFrame, error) {
|
||
|
node := coder.NodePacket{}
|
||
|
_, err := coder.DecodeToNodePacket(buf, &node)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
f := &CloseStreamFrame{}
|
||
|
|
||
|
// id
|
||
|
if idBlock, ok := node.PrimitivePackets[byte(TagOfCloseStreamID)]; ok {
|
||
|
id, err := idBlock.ToUTF8String()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
f.streamID = id
|
||
|
}
|
||
|
// reason
|
||
|
if reasonBlock, ok := node.PrimitivePackets[byte(TagOfCloseStreamReason)]; ok {
|
||
|
reason, err := reasonBlock.ToUTF8String()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
f.reason = reason
|
||
|
}
|
||
|
|
||
|
return f, nil
|
||
|
}
|