fatchoy

package module
v0.1.19 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2021 License: BSD-3-Clause Imports: 15 Imported by: 0

README

fatchoy

Gung Hay Fat Choy(恭喜發財)

A set of libraries to create online game server.

go get -u -v qchen.fun/fatchoy

各目录说明

目录 描述
codec 编解码
codes 错误码
collections 数据结构
debug 调试API
discovery 服务发现
packet 消息结构
l0g 日志
qnet 网络通信
sched 执行器
x 工具包

开发规范指南

GUIDE

Documentation

Index

Constants

View Source
const (
	SERVICE_ALL  = 0xFF   // 所有服务
	INSTANCE_ALL = 0xFFFF // 所有实例
)
View Source
const (
	NodeServiceShift = 16
	NodeTypeShift    = 31
	NodeServiceMask  = 0x00FF0000
	NodeInstanceMask = 0x0000FFFF
)
View Source
const (
	StateInit       = 0
	StateRunning    = 1
	StateShutdown   = 2
	StateTerminated = 3
)
View Source
const ClockEpoch int64 = 1577836800 // 2020-01-01 00:00:00 UTC

epoch of clock

View Source
const VERSION = "0.1.19"

版本号

Variables

This section is empty.

Functions

func DateTime

func DateTime() string

func Now

func Now() time.Time

func NowString

func NowString() string

func StartClock

func StartClock()

开启时钟

func StopClock

func StopClock()

关闭时钟

func WallClock

func WallClock() *datetime.Clock

Types

type Endpoint

type Endpoint interface {
	MessageEndpoint

	// 原始连接对象
	RawConn() net.Conn

	// 发送/接收计数数据
	Stats() *stats.Stats

	// 开启read/write线程
	Go(EndpointFlag)

	// 设置加解密
	SetEncryptPair(cipher.BlockCryptor, cipher.BlockCryptor)
}

网络端点

type EndpointFlag

type EndpointFlag uint32

开启reader/writer标记

const (
	EndpointReader     EndpointFlag = 0x01 // 只开启reader
	EndpointWriter     EndpointFlag = 0x02 // 只开启writer
	EndpointReadWriter EndpointFlag = 0x03 // 开启reader和writer
)

type EndpointMap

type EndpointMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

线程安全的endpoint map

func NewEndpointMap

func NewEndpointMap() *EndpointMap

func (*EndpointMap) Add

func (e *EndpointMap) Add(node NodeID, endpoint Endpoint)

添加一个endpoint

func (*EndpointMap) Delete

func (e *EndpointMap) Delete(node NodeID) bool

删除一个endpoint

func (*EndpointMap) Get

func (e *EndpointMap) Get(node NodeID) Endpoint

查找一个endpoint

func (*EndpointMap) List

func (e *EndpointMap) List() []Endpoint

返回切片拷贝

func (*EndpointMap) Range

func (e *EndpointMap) Range(f func(Endpoint) bool)

遍历

func (*EndpointMap) Reset

func (e *EndpointMap) Reset()

重置

func (*EndpointMap) Size

func (e *EndpointMap) Size() int

type IPacket

type IPacket interface {
	// 消息命令(ID)
	Command() int32
	SetCommand(int32)

	// session序列号
	Seq() uint16
	SetSeq(uint16)

	// 消息类型
	Type() PacketType
	SetType(PacketType)

	// 消息标记
	Flag() PacketFlag
	SetFlag(PacketFlag)

	// 消息错误码
	Errno() int32
	SetErrno(ec int32)

	// 节点ID
	Node() NodeID
	SetNode(NodeID)

	// 引用节点
	Refers() []NodeID
	SetRefers([]NodeID)
	AddRefers(...NodeID)

	// 绑定的endpoint
	Endpoint() MessageEndpoint
	SetEndpoint(MessageEndpoint)

	// clone一个packet
	Clone() IPacket

	// 消息body,仅支持int64/float64/string/bytes/proto.Message类型
	Body() interface{}
	SetBody(v interface{})

	// body类型转换
	BodyToInt() int64
	BodyToFloat() float64
	BodyToString() string
	BodyToBytes() []byte

	// 自动解码为pb消息
	Decode() error

	// 解码body到`msg`里
	DecodeTo(msg proto.Message) error

	// 响应ack消息
	ReplyWith(command int32, body interface{}) error
	Reply(ack proto.Message) error

	// 响应错误码
	RefuseWith(command, errno int32) error
	Refuse(errno int32) error
}

定义应用层消息接口

type MessageEndpoint

type MessageEndpoint interface {
	// 节点ID
	NodeID() NodeID
	SetNodeID(NodeID)

	// 远端地址
	RemoteAddr() string

	// 发送消息
	SendPacket(IPacket) error

	// 关闭读/写
	Close() error
	ForceClose(error)
	IsClosing() bool

	// 绑定自定义数据
	SetUserData(interface{})
	UserData() interface{}
}

绑定到消息上的endpoint

type NodeID

type NodeID uint32

节点ID 一个32位整数表示的节点号,用以标识一个service(最高位为0),或者一个客户端session(最高位为1) 如果是服务编号:8位服务编号,16位服务实例编号

服务实例二进制布局
	--------------------------------------
	|  reserved |  service  |  instance  |
	--------------------------------------
	32          24         16            0

func MakeNodeID

func MakeNodeID(service uint8, instance uint16) NodeID

根据服务号和实例号创建一个节点ID

func MustParseNodeID

func MustParseNodeID(s string) NodeID

解析16进制字符串的节点ID

func (NodeID) Instance

func (n NodeID) Instance() uint16

service节点的实例编号

func (NodeID) IsTypeBackend

func (n NodeID) IsTypeBackend() bool

是否service节点

func (NodeID) Service

func (n NodeID) Service() uint8

service节点的service类型

func (NodeID) String

func (n NodeID) String() string

type NodeIDSet

type NodeIDSet = collections.OrderedIDSet

没有重复ID的有序集合

type PacketFlag

type PacketFlag uint8

消息标志位

const (
	PFlagCompressed PacketFlag = 0x01 // 压缩
	PFlagEncrypted  PacketFlag = 0x02 // 加密
	PFlagError      PacketFlag = 0x10 // 错误标记
	PFlagRpc        PacketFlag = 0x20 // RPC标记
)

type PacketHandler

type PacketHandler func(IPacket) error

消息处理器

type PacketType

type PacketType int8

消息编码类型

const (
	PTypePacket    PacketType = 0 // 应用消息
	PTypeRoute     PacketType = 1 // 路由消息
	PTypeMulticast PacketType = 2 // 组播消息
)

type Service

type Service interface {
	Type() uint8
	Name() string

	NodeID() NodeID
	SetNodeID(id NodeID)

	Context() *ServiceContext

	Init(*ServiceContext) error
	Startup(context.Context) error
}

应用层服务

type ServiceContext

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

服务的上下文

func NewServiceContext

func NewServiceContext(srv Service, queueSize int) *ServiceContext

func (*ServiceContext) Close

func (c *ServiceContext) Close()

关闭context

func (*ServiceContext) InboundQueue added in v0.1.6

func (c *ServiceContext) InboundQueue() chan<- IPacket

消息队列,仅接收

func (*ServiceContext) InitRegistrar added in v0.1.5

func (c *ServiceContext) InitRegistrar(hostAddr, namespace string) error

初始化registrar

func (*ServiceContext) Instance added in v0.1.2

func (c *ServiceContext) Instance() Service

service实例

func (*ServiceContext) MessageQueue

func (c *ServiceContext) MessageQueue() <-chan IPacket

消息队列,仅消费

func (*ServiceContext) Registrar added in v0.1.5

func (c *ServiceContext) Registrar() *discovery.Client

服务注册器

func (*ServiceContext) RunID added in v0.1.5

func (c *ServiceContext) RunID() string

唯一运行ID

func (*ServiceContext) Send added in v0.1.5

func (c *ServiceContext) Send(pkt IPacket)

投递一条消息到context

func (*ServiceContext) WaitDone added in v0.1.2

func (c *ServiceContext) WaitDone() <-chan struct{}

等待close完成

type State added in v0.1.19

type State int32

service state

func (*State) CAS added in v0.1.19

func (s *State) CAS(old, new int32) bool

func (*State) Get added in v0.1.19

func (s *State) Get() int32

func (State) IsRunning added in v0.1.19

func (s State) IsRunning() bool

func (State) IsShuttingDown added in v0.1.19

func (s State) IsShuttingDown() bool

func (State) IsTerminated added in v0.1.19

func (s State) IsTerminated() bool

func (*State) Set added in v0.1.19

func (s *State) Set(n int32)

Jump to

Keyboard shortcuts

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