protocol

package
v0.0.0-...-edd3e93 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Magic   = [6]byte{2, 0, 7, 7, 1, 3}
	Version = [2]byte{'V', '1'}
)

Functions

func HasCommand

func HasCommand(cmd Command) bool

func HasIdentity

func HasIdentity(sender Identity) bool

func IsValidMagic

func IsValidMagic(magic []byte) bool

IsValidMagic 检查幻数是否有效

func Write

func Write(writer io.Writer, cmd Command, identity Identity, host string, data any) error

Types

type Command

type Command uint16

Command 定义请求命令的数据类型

const (
	// 公共指令
	CMD_OK   Command = iota // 请求成功,可有可无
	CMD_FAIL                // 请求失败,需要数据

	CMD_AUTH       // 认证,需要数据
	CMD_REGISTER   // 注册,需要数据
	CMD_HEARTBEAT  // 心跳,无需数据
	CMD_UNREGISTER // 注销,无需数据

	// 同步指令
	CMD_SYNC_REG // 同步注册中心的数据,需要数据(通常新的客户端注册到注册中心后会执行该指令)

	// 通知指令
	CMD_NOTIFY_REGISTER_INS  // 注册中心广播通知注册应用实例,需要数据(应用实例注册后,注册中心节点需要广播该应用实例)
	CMD_NOTIFY_REGISTER_REG  // 注册中心广播通知注册注册中心,需要数据(注册中心注册后,注册中心节点需要广播该注册中心)
	CMD_NOTIFY_REGISTER_GATE // 注册中心广播通知注册网关平台,需要数据(网关平台注册后,注册中心节点需要广播该网关平台)

	CMD_NOTIFY_UNREGISTER_INS  // 注册中心广播通知注销应用实例,需要数据
	CMD_NOTIFY_UNREGISTER_REG  // 注册中心广播通知注销注册中心,需要数据
	CMD_NOTIFY_UNREGISTER_GATE // 注册中心广播通知注销网关平台,需要数据
)

func (Command) String

func (own Command) String() string

type Identity

type Identity uint8
const (
	IDE_GATEWAY Identity = iota
	IDE_INSTANCE
	IDE_REGISTRAR
)

func (Identity) String

func (own Identity) String() string

type Procotol

type Procotol struct {
	Version    [2]byte  // 协议版本: V1
	Command    Command  // 请求方指令
	Identity   Identity // 请求方身份: 0-Gateway; 1-Instance; 2-Registrar
	Timestamp  int64    // 时间戳
	HostLength int16    // 主机长度
	Host       []byte   // 主机
	DataLength int16    // 数据体长度
	Data       []byte   // 数据体:JSON
}

func NewPolarisProtocol

func NewPolarisProtocol(cmd Command, identity Identity, host string, data any) (*Procotol, error)

func Read

func Read(reader io.Reader) (*Procotol, error)

func (*Procotol) ParseBody

func (own *Procotol) ParseBody(data any) error

ParseBody 解析数据体

func (*Procotol) Read

func (own *Procotol) Read(reader io.Reader) error

Read 从指定reader中读取协议数据

func (*Procotol) String

func (own *Procotol) String() string

func (*Procotol) Validate

func (own *Procotol) Validate() error

Validate 验证协议是否有效

func (*Procotol) Write

func (own *Procotol) Write(writer io.Writer) error

Write 将协议数据写入指定的writer

Jump to

Keyboard shortcuts

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