cellnet

package
v0.0.0-...-d0c2239 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultQueueSize = 100

Variables

View Source
var (
	ErrMessageNotFound = errors.New("msg not exists")
	ErrCodecNotFound   = errors.New("codec not found")
)

Functions

func DecodeMessage

func DecodeMessage(msgid uint32, data []byte) (interface{}, error)

func EncodeMessage

func EncodeMessage(msg interface{}) (data []byte, msgid uint32, err error)

func MessageFullName

func MessageFullName(rtype reflect.Type) string

消息全名

func RegisterCodec

func RegisterCodec(c Codec)

func RegisterMessageMeta

func RegisterMessageMeta(codecName string, name string, msgType reflect.Type, id uint32)

注册消息元信息

Types

type Codec

type Codec interface {
	// 将数据转换为字节数组
	Encode(interface{}) ([]byte, error)

	// 将字节数组转换为数据
	Decode([]byte, interface{}) error

	// 编码器的名字
	Name() string
}

func FetchCodec

func FetchCodec(name string) Codec

type EventFunc

type EventFunc func(interface{}) interface{}

事件函数的定义

type EventQueue

type EventQueue interface {
	StartLoop()

	StopLoop(result int)

	// 等待退出
	Wait() int

	// 投递事件, 通过队列到达消费者端
	Post(callback func())

	// 是否捕获异常
	EnableCapturePanic(v bool)
}

func NewEventQueue

func NewEventQueue() EventQueue

创建默认长度的队列

func NewEventQueueByLen

func NewEventQueueByLen(l int) EventQueue

创建指定长度的队列

type MessageMeta

type MessageMeta struct {
	Type  reflect.Type // 消息类型
	Name  string       // 消息名称
	ID    uint32       // 消息ID
	Codec Codec        // 消息用到的编码
}

消息元信息

func MessageMetaByID

func MessageMetaByID(id uint32) *MessageMeta

根据id查找消息元信息

func MessageMetaByName

func MessageMetaByName(name string) *MessageMeta

根据名字查找消息元信息

func MessageMetaByType

func MessageMetaByType(t reflect.Type) *MessageMeta

根据类型查找消息元信息

type Peer

type Peer interface {

	// 开启端,传入地址
	Start(address string) Peer

	// 停止通讯端
	Stop()

	// 获取队列
	Queue() EventQueue

	// 设置事件回调函数
	SetEvent(f EventFunc)

	// 通讯端名称
	Name() string

	// 设置通讯端名称
	SetName(string)

	SessionAccessor
}

发起和接受连接的通讯端

type Session

type Session interface {
	// 发送消息,消息需要以指针格式传入
	Send(msg interface{})

	// 获得原始的Socket连接
	Raw() interface{}

	// 获得Session归属的Peer
	Peer() Peer

	// 断开
	Close()

	// 标示ID
	ID() int64
}

type SessionAccessor

type SessionAccessor interface {

	// 获取一个连接
	GetSession(int64) Session

	// 遍历连接
	VisitSession(func(Session) bool)

	// 连接数量
	SessionCount() int

	// 关闭所有连接
	CloseAllSession()
}

会话访问

Directories

Path Synopsis
codec

Jump to

Keyboard shortcuts

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