|
|
@ -1,6 +1,7 @@
|
|
|
|
package protocol
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/core"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/core"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/entity"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/entity"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/middleware"
|
|
|
|
"git.noahlan.cn/noahlan/nnet/middleware"
|
|
|
@ -9,26 +10,62 @@ import (
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type NNetConfig struct {
|
|
|
|
type (
|
|
|
|
}
|
|
|
|
NNetConfig struct {
|
|
|
|
|
|
|
|
HeartbeatInterval time.Duration
|
|
|
|
|
|
|
|
HandshakeValidator HandshakeValidatorFunc
|
|
|
|
|
|
|
|
HandshakeAckBuilder HandshakeAckBuilderFunc
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handshakeData struct {
|
|
|
|
|
|
|
|
Version string `json:"version"` // 客户端版本,服务器以此判断是否合适与客户端通信
|
|
|
|
|
|
|
|
Type string `json:"type"` // 客户端类型,与客户端版本号一起来确定客户端是否合适
|
|
|
|
|
|
|
|
ClientId string `json:"clientId"` // 客户端ID,服务器以此取值
|
|
|
|
|
|
|
|
ClientSecret string `json:"clientSecret"` // 客户端密钥,服务器以此判定客户端是否可用
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 透传信息
|
|
|
|
|
|
|
|
Payload interface{} `json:"payload,optional,omitempty"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HandshakeAckData struct {
|
|
|
|
|
|
|
|
// 心跳间隔,单位秒 0表示不需要心跳
|
|
|
|
|
|
|
|
Heartbeat int64 `json:"heartbeat"`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 路由
|
|
|
|
|
|
|
|
Routes map[string]uint16 `json:"routes"` // route map to code
|
|
|
|
|
|
|
|
Codes map[uint16]string `json:"codes"` // code map to route
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 透传信息
|
|
|
|
|
|
|
|
Payload interface{} `json:"payload,optional,omitempty"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func WithNNetProtocol(
|
|
|
|
func WithNNetProtocol(config NNetConfig) []core.RunOption {
|
|
|
|
handshakeValidator func([]byte) error,
|
|
|
|
if config.HandshakeValidator == nil {
|
|
|
|
heartbeatInterval time.Duration,
|
|
|
|
config.HandshakeValidator = func(bytes []byte) error { return nil }
|
|
|
|
) []core.RunOption {
|
|
|
|
|
|
|
|
if handshakeValidator == nil {
|
|
|
|
|
|
|
|
handshakeValidator = func(bytes []byte) error { return nil }
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.HandshakeAckBuilder == nil {
|
|
|
|
|
|
|
|
config.HandshakeAckBuilder = func() ([]byte, error) {
|
|
|
|
|
|
|
|
defaultData := &HandshakeAckData{}
|
|
|
|
|
|
|
|
return json.Marshal(defaultData)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
opts := []core.RunOption{
|
|
|
|
|
|
|
|
WithNNetPipeline(config.HandshakeAckBuilder, config.HandshakeValidator),
|
|
|
|
|
|
|
|
core.WithRouter(NewNNetRouter()),
|
|
|
|
|
|
|
|
core.WithPacker(func() packet.Packer { return NewNNetPacker() }),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if config.HeartbeatInterval.Seconds() > 0 {
|
|
|
|
packer := NewNNetPacker()
|
|
|
|
packer := NewNNetPacker()
|
|
|
|
hbd, err := packer.Pack(Handshake, nil)
|
|
|
|
hbd, err := packer.Pack(Handshake, nil)
|
|
|
|
nlog.Must(err)
|
|
|
|
nlog.Must(err)
|
|
|
|
|
|
|
|
|
|
|
|
return []core.RunOption{
|
|
|
|
opts = append(opts, middleware.WithHeartbeat(config.HeartbeatInterval, func(_ entity.NetworkEntity) []byte {
|
|
|
|
WithNNetPipeline(handshakeValidator),
|
|
|
|
|
|
|
|
core.WithRouter(NewNNetRouter()),
|
|
|
|
|
|
|
|
core.WithPacker(func() packet.Packer { return NewNNetPacker() }),
|
|
|
|
|
|
|
|
middleware.WithHeartbeat(heartbeatInterval, func(_ entity.NetworkEntity) []byte {
|
|
|
|
|
|
|
|
return hbd
|
|
|
|
return hbd
|
|
|
|
}),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
}
|
|
|
|