network/frame/handshake_frame.go

132 lines
3.8 KiB
Go
Raw Normal View History

2022-10-11 17:36:09 +08:00
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
2023-03-26 23:18:55 +08:00
nameBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeName))
2022-10-11 17:36:09 +08:00
nameBlock.SetStringValue(h.Name)
// client ID
2023-03-26 23:18:55 +08:00
idBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeId))
2022-10-11 17:36:09 +08:00
idBlock.SetStringValue(h.ClientId)
// client type
2023-03-26 23:18:55 +08:00
typeBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeType))
2022-10-11 17:36:09 +08:00
typeBlock.SetBytesValue([]byte{h.ClientType})
// observe data tags
2023-03-26 23:18:55 +08:00
observeDataTagsBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeObserveDataTags))
2022-10-11 17:36:09 +08:00
observeDataTagsBlock.SetBytesValue(h.ObserveDataTags)
// auth
2023-03-26 23:18:55 +08:00
authNameBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeAuthName))
2022-10-11 17:36:09 +08:00
authNameBlock.SetStringValue(h.authName)
2023-03-26 23:18:55 +08:00
authPayloadBlock := coder.NewPrimitivePacketEncoder(byte(TagOfHandshakeAuthPayload))
2022-10-11 17:36:09 +08:00
authPayloadBlock.SetStringValue(h.authPayload)
// handshake frame
2023-03-26 23:18:55 +08:00
handshake := coder.NewNodePacketEncoder(byte(h.Type()))
2022-10-11 17:36:09 +08:00
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
2023-03-26 23:18:55 +08:00
if nameBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeName)]; ok {
2022-10-11 17:36:09 +08:00
name, err := nameBlock.ToUTF8String()
if err != nil {
return nil, err
}
handshake.Name = name
}
// client id
2023-03-26 23:18:55 +08:00
if idBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeId)]; ok {
2022-10-11 17:36:09 +08:00
id, err := idBlock.ToUTF8String()
if err != nil {
return nil, err
}
handshake.ClientId = id
}
// client type
2023-03-26 23:18:55 +08:00
if typeBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeType)]; ok {
2022-10-11 17:36:09 +08:00
clientType := typeBlock.ToBytes()
handshake.ClientType = clientType[0]
}
// observe data tag list
2023-03-26 23:18:55 +08:00
if observeDataTagsBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeObserveDataTags)]; ok {
2022-10-11 17:36:09 +08:00
handshake.ObserveDataTags = observeDataTagsBlock.ToBytes()
}
// auth
2023-03-26 23:18:55 +08:00
if authNameBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeAuthName)]; ok {
2022-10-11 17:36:09 +08:00
authName, err := authNameBlock.ToUTF8String()
if err != nil {
return nil, err
}
handshake.authName = authName
}
2023-03-26 23:18:55 +08:00
if authPayloadBlock, ok := node.PrimitivePackets[byte(TagOfHandshakeAuthPayload)]; ok {
2022-10-11 17:36:09 +08:00
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
}