60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
|
package frame
|
||
|
|
||
|
import (
|
||
|
coder "git.hpds.cc/Component/mq_coder"
|
||
|
)
|
||
|
|
||
|
// HandshakeAckFrame is used to ack handshake, It is always that the first frame
|
||
|
// is HandshakeAckFrame after client acquire a new stream.
|
||
|
// HandshakeAckFrame is a coder encoded bytes.
|
||
|
type HandshakeAckFrame struct {
|
||
|
streamId string
|
||
|
}
|
||
|
|
||
|
// NewHandshakeAckFrame returns a HandshakeAckFrame.
|
||
|
func NewHandshakeAckFrame(streamId string) *HandshakeAckFrame {
|
||
|
return &HandshakeAckFrame{streamId}
|
||
|
}
|
||
|
|
||
|
// Type gets the type of the HandshakeAckFrame.
|
||
|
func (f *HandshakeAckFrame) Type() Type {
|
||
|
return TagOfHandshakeAckFrame
|
||
|
}
|
||
|
|
||
|
// StreamId returns the id of stream be acked.
|
||
|
func (f *HandshakeAckFrame) StreamId() string {
|
||
|
return f.streamId
|
||
|
}
|
||
|
|
||
|
// Encode encodes HandshakeAckFrame to coder encoded bytes.
|
||
|
func (f *HandshakeAckFrame) Encode() []byte {
|
||
|
ack := coder.NewNodePacketEncoder(byte(f.Type()))
|
||
|
// streamId
|
||
|
streamIDBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeAckStreamId))
|
||
|
streamIDBlock.SetStringValue(f.streamId)
|
||
|
|
||
|
ack.AddPrimitivePacket(streamIDBlock)
|
||
|
|
||
|
return ack.Encode()
|
||
|
}
|
||
|
|
||
|
// DecodeToHandshakeAckFrame decodes coder encoded bytes to HandshakeAckFrame
|
||
|
func DecodeToHandshakeAckFrame(buf []byte) (*HandshakeAckFrame, error) {
|
||
|
node := coder.NodePacket{}
|
||
|
_, err := coder.DecodeToNodePacket(buf, &node)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
ack := &HandshakeAckFrame{}
|
||
|
// streamID
|
||
|
if streamIDBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeAckStreamId)]; ok {
|
||
|
streamId, err := streamIDBlock.ToUTF8String()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
ack.streamId = streamId
|
||
|
}
|
||
|
return ack, nil
|
||
|
}
|