nnet

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Unknown 未知包类型,无意义
	Unknown Type = iota

	// Handshake 握手数据(服务端主动发起)
	Handshake = 0x01

	// HandshakeAck 握手回复(客户端回复)
	HandshakeAck = 0x02

	// Heartbeat 心跳(服务端发起)
	Heartbeat = 0x03

	// Data 数据传输
	Data = 0x04

	// Kick 服务端主动断开连接
	Kick = 0x05
)
View Source
const (
	Request  MsgType = 0x00
	Notify           = 0x01
	Response         = 0x02
	Push             = 0x03
)

Message types

Variables

View Source
var (
	ErrPacketSizeExceed  = errors.New("packer: packet size exceed")
	ErrWrongMessageType  = errors.New("wrong message type")
	ErrRouteInfoNotFound = errors.New("route info not found in dictionary")
	ErrWrongMessage      = errors.New("wrong message")
)

Functions

func NewRouter

func NewRouter() rt.Router

func WithNNetClientEvents added in v1.1.4

func WithNNetClientEvents(onReady OnReadyFunc, packer packet.Packer) nnet.RunOption

func WithNNetClientProtocol

func WithNNetClientProtocol(onReady OnReadyFunc) []nnet.RunOption

func WithNNetProtocol

func WithNNetProtocol(config Config) []nnet.RunOption

Types

type Config

type Config struct {
	HeartbeatInterval  time.Duration
	HandshakeValidator HandshakeValidatorFunc
}

type HandshakeAckPayloadFunc

type HandshakeAckPayloadFunc func() any

type HandshakeReq

type HandshakeReq struct {
	Version      string `json:"version"`      // 客户端版本,服务器以此判断是否合适与客户端通信
	Type         string `json:"type"`         // 客户端类型,与客户端版本号一起来确定客户端是否合适
	ClientId     string `json:"clientId"`     // 客户端ID,服务器以此取值
	ClientSecret string `json:"clientSecret"` // 客户端密钥,服务器以此判定客户端是否可用

	// 透传信息
	Payload any `json:"payload,optional,omitempty"`
}

type HandshakeResp

type HandshakeResp struct {
	// 心跳间隔,单位秒 0表示不需要心跳
	Heartbeat int64 `json:"heartbeat"`

	*RouteMap

	// 透传信息
	Payload any `json:"payload,optional,omitempty"`
}

type HandshakeValidatorFunc

type HandshakeValidatorFunc func(*HandshakeReq) error
type Header struct {
	PacketType    Type   // 数据帧 类型
	Length        uint32 // 数据长度
	MessageHeader        // 消息头
}

type Match

type Match struct {
	Route string // 路由
	Code  uint16 // 路由编码
}

type MessageHeader

type MessageHeader struct {
	MsgType MsgType // message type (flag)
	ID      uint64  // unique id, zero while notify mode
	Route   string  // route for locating service
	// contains filtered or unexported fields
}

type MsgType

type MsgType byte

func (MsgType) String

func (t MsgType) String() string

type OnReadyFunc

type OnReadyFunc func()

type Packer

type Packer struct {
	// contains filtered or unexported fields
}

func NewPacker

func NewPacker(routeMap *RouteMap) *Packer

func (*Packer) Pack

func (d *Packer) Pack(header any, data []byte) ([]byte, error)

func (*Packer) Unpack

func (d *Packer) Unpack(data []byte) ([]packet.IPacket, error)

type Packet

type Packet struct {
	Header
	Data []byte // 原始数据
}

func (*Packet) GetBody

func (p *Packet) GetBody() []byte

func (*Packet) GetHeader

func (p *Packet) GetHeader() any

func (*Packet) GetLen

func (p *Packet) GetLen() uint64

func (*Packet) String

func (p *Packet) String() string

type RouteMap

type RouteMap struct {
	// 路由
	Routes map[string]uint16 `json:"routes"` // route map to code
	Codes  map[uint16]string `json:"codes"`  // code map to route
}

func NewRouteMap

func NewRouteMap() *RouteMap

type Type

type Type byte

Type 数据帧类型,如:握手,心跳,数据 等

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL