132 lines
3.8 KiB
Go
132 lines
3.8 KiB
Go
|
package frame
|
||
|
|
||
|
import (
|
||
|
coder "git.hpds.cc/Component/mq_coder"
|
||
|
)
|
||
|
|
||
|
// HandshakeFrame is a coder encoded.
|
||
|
type HandshakeFrame struct {
|
||
|
// Name is client name
|
||
|
Name string
|
||
|
// ClientId represents client id
|
||
|
ClientId string
|
||
|
// ClientType represents client type (Protocol Gateway | Stream Function)
|
||
|
ClientType byte
|
||
|
// ObserveDataTags are the client data tag list.
|
||
|
ObserveDataTags []byte
|
||
|
// auth
|
||
|
authName string
|
||
|
authPayload string
|
||
|
}
|
||
|
|
||
|
// NewHandshakeFrame creates a new HandshakeFrame.
|
||
|
func NewHandshakeFrame(name string, clientId string, clientType byte, observeDataTags []byte, authName string, authPayload string) *HandshakeFrame {
|
||
|
return &HandshakeFrame{
|
||
|
Name: name,
|
||
|
ClientId: clientId,
|
||
|
ClientType: clientType,
|
||
|
ObserveDataTags: observeDataTags,
|
||
|
authName: authName,
|
||
|
authPayload: authPayload,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Type gets the type of Frame.
|
||
|
func (h *HandshakeFrame) Type() Type {
|
||
|
return TagOfHandshakeFrame
|
||
|
}
|
||
|
|
||
|
// Encode to coder encoding.
|
||
|
func (h *HandshakeFrame) Encode() []byte {
|
||
|
// name
|
||
|
nameBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeName))
|
||
|
nameBlock.SetStringValue(h.Name)
|
||
|
// client ID
|
||
|
idBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeId))
|
||
|
idBlock.SetStringValue(h.ClientId)
|
||
|
// client type
|
||
|
typeBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeType))
|
||
|
typeBlock.SetBytesValue([]byte{h.ClientType})
|
||
|
// observe data tags
|
||
|
observeDataTagsBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeObserveDataTags))
|
||
|
observeDataTagsBlock.SetBytesValue(h.ObserveDataTags)
|
||
|
// auth
|
||
|
authNameBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeAuthName))
|
||
|
authNameBlock.SetStringValue(h.authName)
|
||
|
authPayloadBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeAuthPayload))
|
||
|
authPayloadBlock.SetStringValue(h.authPayload)
|
||
|
// handshake frame
|
||
|
handshake := coder.NewNodePacketEncoder(byte(h.Type()))
|
||
|
handshake.AddPrimitivePacket(nameBlock)
|
||
|
handshake.AddPrimitivePacket(idBlock)
|
||
|
handshake.AddPrimitivePacket(typeBlock)
|
||
|
handshake.AddPrimitivePacket(observeDataTagsBlock)
|
||
|
handshake.AddPrimitivePacket(authNameBlock)
|
||
|
handshake.AddPrimitivePacket(authPayloadBlock)
|
||
|
|
||
|
return handshake.Encode()
|
||
|
}
|
||
|
|
||
|
// DecodeToHandshakeFrame decodes coder encoded bytes to HandshakeFrame.
|
||
|
func DecodeToHandshakeFrame(buf []byte) (*HandshakeFrame, error) {
|
||
|
node := coder.NodePacket{}
|
||
|
_, err := coder.DecodeToNodePacket(buf, &node)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
handshake := &HandshakeFrame{}
|
||
|
// name
|
||
|
if nameBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeName)]; ok {
|
||
|
name, err := nameBlock.ToUTF8String()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
handshake.Name = name
|
||
|
}
|
||
|
// client id
|
||
|
if idBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeId)]; ok {
|
||
|
id, err := idBlock.ToUTF8String()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
handshake.ClientId = id
|
||
|
}
|
||
|
// client type
|
||
|
if typeBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeType)]; ok {
|
||
|
clientType := typeBlock.ToBytes()
|
||
|
handshake.ClientType = clientType[0]
|
||
|
}
|
||
|
// observe data tag list
|
||
|
if observeDataTagsBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeObserveDataTags)]; ok {
|
||
|
handshake.ObserveDataTags = observeDataTagsBlock.ToBytes()
|
||
|
}
|
||
|
// auth
|
||
|
if authNameBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeAuthName)]; ok {
|
||
|
authName, err := authNameBlock.ToUTF8String()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
handshake.authName = authName
|
||
|
}
|
||
|
if authPayloadBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeAuthPayload)]; ok {
|
||
|
authPayload, err := authPayloadBlock.ToUTF8String()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
handshake.authPayload = authPayload
|
||
|
}
|
||
|
|
||
|
return handshake, nil
|
||
|
}
|
||
|
|
||
|
// AuthPayload authentication payload
|
||
|
func (h *HandshakeFrame) AuthPayload() string {
|
||
|
return h.authPayload
|
||
|
}
|
||
|
|
||
|
// AuthName authentication name
|
||
|
func (h *HandshakeFrame) AuthName() string {
|
||
|
return h.authName
|
||
|
}
|