You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package client
|
|
|
|
import (
|
|
"strings"
|
|
|
|
protocolnnet "git.noahlan.cn/noahlan/nnet/v2/internal/protocol/nnet"
|
|
protocolpkg "git.noahlan.cn/noahlan/nnet/v2/pkg/protocol"
|
|
unpackerpkg "git.noahlan.cn/noahlan/nnet/v2/pkg/unpacker"
|
|
)
|
|
|
|
type unpackingProtocol interface {
|
|
Unpacker() unpackerpkg.Unpacker
|
|
}
|
|
|
|
func newApplicationProtocol(name string) protocolpkg.Protocol {
|
|
switch strings.ToLower(strings.TrimSpace(name)) {
|
|
case "", "none", "raw":
|
|
return nil
|
|
case "nnet":
|
|
return protocolnnet.NewNNetProtocol("1.0")
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func newApplicationUnpacker(protocol protocolpkg.Protocol) unpackerpkg.Unpacker {
|
|
if protocol == nil {
|
|
return nil
|
|
}
|
|
if provider, ok := protocol.(unpackingProtocol); ok {
|
|
return provider.Unpacker()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func encodeApplicationMessage(protocol protocolpkg.Protocol, data []byte) ([]byte, error) {
|
|
if protocol == nil {
|
|
return data, nil
|
|
}
|
|
return protocol.Encode(data, nil)
|
|
}
|
|
|
|
func decodeApplicationMessage(protocol protocolpkg.Protocol, frame []byte) ([]byte, error) {
|
|
if protocol == nil {
|
|
msg := make([]byte, len(frame))
|
|
copy(msg, frame)
|
|
return msg, nil
|
|
}
|
|
_, body, err := protocol.Decode(frame)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
msg := make([]byte, len(body))
|
|
copy(msg, body)
|
|
return msg, nil
|
|
}
|