cfacade

package
v1.0.20 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CodecProtobuf and CodecJSON are stable wire values carried by AGP and
	// cluster envelopes. Do not reuse them for another encoding.
	CodecProtobuf int32 = 1
	CodecJSON     int32 = 2
)
View Source
const (
	// 大区ID 10位,偏移量 54(18+18+18)
	BigWorldIdShift = 54
	// 小区ID 18位,偏移量 36(18+18)
	WorldIdShift = 36
	// 进程类型 18位,偏移量 18
	NodeTypeShift = 18
	// 进程实例 18位,偏移量 0
	NodeInstShift = 0

	// 位掩码:用于截取对应字段(18位全1=0x3FFFF,10位全1=0x3FF)
	BigWorldIdMask = uint64(0x3FF)   // 10位掩码
	CommonMask     = uint64(0x3FFFF) // 18位掩码(小区/进程类型/进程实例通用)
)

位偏移常量定义(从高位到低位)

Variables

This section is empty.

Functions

func GenNodeId added in v1.0.12

func GenNodeId(bigworldId uint32, worldId uint32, NodeType uint32, NodeInst uint32) uint64

GenNodeId 生成服务器ID bigworldId: 大区ID (0~1023,10位最大值) worldId: 小区ID (0~262143,18位最大值) NodeType: 进程类型 (0~262143,18位最大值) NodeInst: 进程实例 (0~262143,18位最大值)

func GenNodeIdByStr added in v1.0.12

func GenNodeIdByStr(nodeIdStr string) (uint64, error)

func GetBigWorldId added in v1.0.12

func GetBigWorldId(nodeId uint64) uint32

BigWorldId 解析大区ID

func GetNodeInst added in v1.0.12

func GetNodeInst(nodeId uint64) uint32

NodeInst 解析进程实例

func GetNodeType added in v1.0.12

func GetNodeType(nodeId uint64) uint32

NodeType 解析进程类型

func GetWorldId added in v1.0.12

func GetWorldId(nodeId uint64) uint32

WorldId 解析小区ID

func NewChildPath

func NewChildPath(nodeID, actorID, childID any) string

NewChildPath formats either a parent path or a child path when childID is set.

func NewPath

func NewPath(nodeID, actorID any) string

NewPath formats a top-level Actor path.

func ToNodeIdStr added in v1.0.12

func ToNodeIdStr(nodeId uint64) string

String 格式化输出所有字段

Types

type ActorPath

type ActorPath struct {
	NodeID  string
	ActorID string
	ChildID string
}

ActorPath identifies a top-level Actor or one of its dynamic children. ActorPath = NodeID . ActorID ActorPath = NodeID . ActorID . ChildID A generated NodeID itself contains four dot-separated numeric segments.

func NewActorPath

func NewActorPath(nodeID, actorID, childID string) *ActorPath

NewActorPath constructs a parsed Actor path without validating its parts.

func ToActorPath

func ToActorPath(path string) (*ActorPath, error)

ToActorPath accepts both short node IDs and generated four-segment node IDs.

func (*ActorPath) IsChild

func (p *ActorPath) IsChild() bool

func (*ActorPath) IsParent

func (p *ActorPath) IsParent() bool

func (*ActorPath) String

func (p *ActorPath) String() string

String formats the path accepted by ToActorPath. String

type Component

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

Component base component

func (*Component) App

func (p *Component) App() IApplication

func (*Component) Init

func (*Component) Init()

func (*Component) Name

func (*Component) Name() string

func (*Component) OnAfterInit

func (*Component) OnAfterInit()

func (*Component) OnBeforeStop

func (*Component) OnBeforeStop()

func (*Component) OnStop

func (*Component) OnStop()

func (*Component) Set

func (p *Component) Set(app IApplication)

type IActor

type IActor interface {
	App() IApplication
	ActorID() string
	Path() *ActorPath
	LastAt() int64
	Invoke(ctx *RequestContext, methodID uint32, payload any) *InvokeResult
	Notify(ctx *RequestContext, methodID uint32, payload any) *InvokeResult
	Exit()
}

IActor is the business-facing handle for one serialized Actor instance.

type IActorChild

type IActorChild interface {
	Create(id string, handler IActorHandler) (IActor, error) // 创建子Actor
	Get(id string) (IActor, bool)                            // 获取子Actor
	Remove(id string)                                        // 称除子Actor
	Each(fn func(i IActor))                                  // 遍历所有子Actor
}

IActorChild manages the dynamic children owned by one parent Actor.

type IActorHandler

type IActorHandler interface {
	AliasID() string                       // actorID
	OnInit()                               // 当Actor启动前触发该函数
	OnStop()                               // 当Actor停止前触发该函数
	OnFindChild(m *Message) (IActor, bool) // 当actor查找子Actor时触发该函数
}

IActorHandler defines an Actor's identity and lifecycle hooks.

type IActorSystem

type IActorSystem interface {
	GetIActor(id string) (IActor, bool)
	CreateActor(id string, handler IActorHandler) (IActor, error)
	Invoke(ctx *RequestContext, methodID uint32, payload any) *InvokeResult
	InvokeNode(ctx *RequestContext, nodeID string, methodID uint32, payload any) *InvokeResult
	InvokeTarget(ctx *RequestContext, target string, methodID uint32, payload any) *InvokeResult
	Notify(ctx *RequestContext, methodID uint32, payload any) *InvokeResult
	NotifyNode(ctx *RequestContext, nodeID string, methodID uint32, payload any) *InvokeResult
	NotifyTarget(ctx *RequestContext, target string, methodID uint32, payload any) *InvokeResult
	SetCallTimeout(d time.Duration)
	SetExecutionTimeout(t int64)
}

IActorSystem creates Actors and routes request/notify calls. Invoke and Notify resolve a top-level target by MethodID; the Target variants address an explicit Actor path, including a dynamic child.

type IApplication

type IApplication interface {
	INode
	Running() bool                     // 是否运行中
	DieChan() chan bool                // die chan
	Register(components ...IComponent) // 注册组件
	Find(name string) IComponent       // 根据name获取组件对象
	Remove(name string) IComponent     // 根据name移除组件对象
	All() []IComponent                 // 获取所有组件列表
	OnShutdown(fn ...func())           // 关闭前执行的函数
	Startup()                          // 启动应用实例
	Shutdown()                         // 关闭应用实例
	BodyCodecs() IBodyCodecRegistry    // 协议体编解码器注册表
	Discovery() IDiscovery             // 发现服务
	Cluster() ICluster                 // 集群服务
	ActorSystem() IActorSystem         // actor系统
	Methods() IMethodTable             // actor方法表
}

IApplication exposes the node-level service container shared by components.

type IBodyCodec added in v1.0.20

type IBodyCodec interface {
	ISerializer
	ID() int32
}

IBodyCodec serializes one concrete Request, Response, or Notify body. Packet and cluster envelopes are always protobuf and never pass through this interface.

type IBodyCodecRegistry added in v1.0.20

type IBodyCodecRegistry interface {
	Register(codec IBodyCodec) error
	Lookup(id int32) (IBodyCodec, bool)
	Marshal(id int32, value any) ([]byte, error)
	Unmarshal(id int32, data []byte, value any) error
	Default() int32
	SetDefault(id int32) error
}

IBodyCodecRegistry contains the finite set of codecs accepted by the framework protocol.

type ICluster

type ICluster interface {
	Init()                                                                                                        // 初始化
	Publish(nodeID string, message *cproto.ClusterMessage) error                                                  // 发布远程消息
	Request(nodeID string, message *cproto.ClusterMessage, timeout time.Duration) (*cproto.ClusterMessage, error) // 请求远程消息
	Stop()                                                                                                        // 停止
}

ICluster transports protobuf cluster messages between discovered nodes.

type IComponent

type IComponent interface {
	Name() string
	App() IApplication
	IComponentLifecycle
}

type IComponentLifecycle

type IComponentLifecycle interface {
	Set(app IApplication)
	Init()
	OnAfterInit()
	OnBeforeStop()
	OnStop()
}

type IConnector

type IConnector interface {
	IComponent
	Start()                     // 启动连接器
	Stop()                      // 停止连接器
	OnConnect(fn OnConnectFunc) // 建立新连接时触发的函数
}

IConnector 网络连接器接口

type IDiscovery

type IDiscovery interface {
	Load(app IApplication)
	Name() string                                                 // 发现服务名称
	Map() map[string]IMember                                      // 获取成员列表
	ListByType(nodeType string, filterNodeID ...string) []IMember // 根据节点类型获取列表
	Random(nodeType string) (IMember, bool)                       // 根据节点类型随机一个
	GetType(nodeID string) (nodeType string, err error)           // 根据节点id获取类型
	GetMember(nodeID string) (member IMember, found bool)         // 获取成员
	AddMember(member IMember)                                     // 添加成员
	RemoveMember(nodeID string)                                   // 移除成员
	OnAddMember(listener MemberListener)                          // 添加成员监听函数
	OnRemoveMember(listener MemberListener)                       // 移除成员监听函数
	Stop()
}

IDiscovery 发现服务接口

type IEventData

type IEventData interface {
	Name() string    // 事件名
	UniqueID() int64 // 唯一id
}

IEventData identifies an event and optionally scopes delivery by UniqueID.

type IMember

type IMember interface {
	GetNodeID() string
	GetNodeType() string
	GetAddress() string
	GetSettings() map[string]string
}

IMember describes a node advertised by discovery.

type IMethodTable added in v1.0.20

type IMethodTable interface {
	Register(methodID uint32, target string, msgType cproto.MsgType) error
	UnregisterTarget(target string)
	MsgType(methodID uint32) (cproto.MsgType, bool)
	// Target returns the top-level Actor registered for a MethodID.
	Target(methodID uint32) (string, bool)
	// Dispatch resolves the top-level Actor exclusively by MethodID.
	Dispatch(ctx *RequestContext, methodID uint32, body []byte, msgType cproto.MsgType) *InvokeResult
}

IMethodTable is populated by Actor.Methods().Register. A MethodID maps to one top-level Actor method and must be globally unique. Child Actor methods stay in their own mailbox and are never exposed through an external transport method table.

type INode

type INode interface {
	NodeID() string        // 节点id(全局唯一)
	NodeType() string      // 节点类型
	Address() string       // 对外网络监听地址(前端节点用)
	RpcAddress() string    // rpc监听地址(未用)
	Settings() ProfileJSON // 节点配置参数
	Enabled() bool         // 是否启用
}

INode 节点信息

type ISerializer

type ISerializer interface {
	Marshal(any) ([]byte, error) // 编码
	Unmarshal([]byte, any) error // 解码
	Name() string                // 序列化类型的名称
}

ISerializer 消息序列化

type InvokeResult added in v1.0.20

type InvokeResult struct {
	Payload any
	Code    int32
	Message string
}

InvokeResult is the transport-neutral result returned by an Actor method. Local calls return a concrete response; remote calls return encoded bytes.

func ErrorResult added in v1.0.20

func ErrorResult(code cproto.StatusCode, message string) *InvokeResult

ErrorResult builds a failed Actor invocation result with an AGP status code.

func OKResult added in v1.0.20

func OKResult(payload any) *InvokeResult

OKResult builds a successful Actor invocation result.

func (*InvokeResult) Decode added in v1.0.20

func (r *InvokeResult) Decode(codecs IBodyCodecRegistry, codec int32, target any) error

Decode unmarshals a successful result into target. It also accepts concrete local results, giving callers one decode path for local and remote invokes.

func (*InvokeResult) OK added in v1.0.20

func (r *InvokeResult) OK() bool

OK reports whether the result represents success.

type MemberListener

type MemberListener func(member IMember) // MemberListener 成员增、删监听函数

type Message

type Message struct {
	MethodID uint32 // 请求调用的方法id
	Target   string // 目标actor path

	Context          *RequestContext    // 请求上下文
	Payload          any                // 请求的参数
	ChanInvokeResult chan *InvokeResult // 请求结果通道
	Cancel           context.CancelFunc // 释放 Actor 持有的通知上下文
	// contains filtered or unexported fields
}

Message is the pooled internal delivery unit shared by local, HTTP/AGP, and cluster entry points. Its final consumer must call Recycle exactly once.

func GetMessage

func GetMessage() *Message

GetMessage acquires a cleared delivery message from the shared pool.

func (*Message) Recycle added in v1.0.6

func (m *Message) Recycle()

Recycle releases the message-owned context and clears references before reuse.

func (*Message) TargetPath

func (m *Message) TargetPath() *ActorPath

TargetPath parses Target once and caches the result for Actor routing.

type OnConnectFunc

type OnConnectFunc func(conn net.Conn)

OnConnectFunc 建立连接时监听的函数

type ProfileJSON

type ProfileJSON interface {
	jsoniter.Any
	GetConfig(path ...any) ProfileJSON
	GetString(path any, defaultVal ...string) string
	GetBool(path any, defaultVal ...bool) bool
	GetInt(path any, defaultVal ...int) int
	GetInt32(path any, defaultVal ...int32) int32
	GetInt64(path any, defaultVal ...int64) int64
	GetDuration(path any, defaultVal ...time.Duration) time.Duration
	Unmarshal(ptrVal any) error
}

ProfileJSON profile配置文件读取接口

type RequestContext added in v1.0.20

type RequestContext struct {
	context.Context

	RequestID uint32
	Transport TransportType
	Codec     int32

	Session  *cproto.Session
	Metadata map[string][]byte
}

RequestContext carries cancellation, transport and caller state through all transport, cluster and Actor boundaries.

func NewRequestContext added in v1.0.20

func NewRequestContext(parent context.Context) *RequestContext

NewRequestContext wraps parent and substitutes context.Background for nil.

func (*RequestContext) Clone added in v1.0.20

func (c *RequestContext) Clone(parent context.Context) *RequestContext

Clone copies mutable request state and replaces the cancellation parent. Actor mailboxes use it so queued work never shares mutable transport state.

type SID

type SID = string // session unique id

type TransportType added in v1.0.20

type TransportType uint8

TransportType identifies the request entry protocol.

const (
	TransportUnknown TransportType = iota
	TransportAGP
	TransportHTTP
	TransportCluster
)

type TypedInvoke added in v1.0.20

type TypedInvoke func(ctx *RequestContext, payload any) *InvokeResult

TypedInvoke is the normalized form stored in an Actor mailbox.

type UID

type UID = int64 // user unique id

Jump to

Keyboard shortcuts

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