service

package
v0.4.26 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: LGPL-2.1 Imports: 18 Imported by: 46

Documentation

Overview

Package service 为服务环境提供的一些接口与函数。

  • 服务上下文(context)
  • 并发实体管理器(EntityManager)

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrContext       = fmt.Errorf("%w: service-context", exception.ErrCore) // 服务上下文错误
	ErrEntityManager = fmt.Errorf("%w: entity-manager", ErrContext)         // 实体管理器错误
)
View Source
var With _ContextOption

Functions

func UnsafeContext deprecated

func UnsafeContext(ctx Context) _UnsafeContext

Deprecated: UnsafeContext 访问服务上下文内部方法

Types

type Caller

type Caller interface {
	// CallAsync 查找实体并异步调用函数,有返回值。不会阻塞当前线程,会返回AsyncRet。
	//
	//	注意:
	//	- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
	//	- 调用过程中的panic信息,均会转换为error返回。
	CallAsync(entityId uid.Id, fun generic.FuncVar1[ec.Entity, any, async.Ret], args ...any) async.AsyncRet

	// CallDelegateAsync 查找实体并异步调用委托,有返回值。不会阻塞当前线程,会返回AsyncRet。
	//
	//	注意:
	//	- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
	//	- 调用过程中的panic信息,均会转换为error返回。
	CallDelegateAsync(entityId uid.Id, fun generic.DelegateVar1[ec.Entity, any, async.Ret], args ...any) async.AsyncRet

	// CallVoidAsync 查找实体并异步调用函数,无返回值。在运行时中。不会阻塞当前线程,会返回AsyncRet。
	//
	//	注意:
	//	- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
	//	- 调用过程中的panic信息,均会转换为error返回。
	CallVoidAsync(entityId uid.Id, fun generic.ActionVar1[ec.Entity, any], args ...any) async.AsyncRet

	// CallDelegateVoidAsync 查找实体并异步调用委托,无返回值。在运行时中。不会阻塞当前线程,会返回AsyncRet。
	//
	//	注意:
	//	- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
	//	- 调用过程中的panic信息,均会转换为error返回。
	CallDelegateVoidAsync(entityId uid.Id, fun generic.DelegateVoidVar1[ec.Entity, any], args ...any) async.AsyncRet
}

Caller 异步调用发起者

type Context

type Context interface {
	corectx.Context
	reinterpret.InstanceProvider
	extension.AddInProvider
	pt.EntityPTProvider
	Caller
	fmt.Stringer

	// Name 获取名称
	Name() string
	// Id 获取服务Id
	Id() uid.Id
	// Reflected 获取反射值
	Reflected() reflect.Value
	// EntityManager 获取实体管理器
	EntityManager() EntityManager
	// contains filtered or unexported methods
}

Context 服务上下文

func Current

func Current(provider corectx.ConcurrentContextProvider) Context

Current 获取服务上下文

func NewContext

func NewContext(settings ...option.Setting[ContextOptions]) Context

NewContext 创建服务上下文

func UnsafeNewContext deprecated

func UnsafeNewContext(options ContextOptions) Context

Deprecated: UnsafeNewContext 内部创建服务上下文

type ContextBehavior

type ContextBehavior struct {
	corectx.ContextBehavior
	// contains filtered or unexported fields
}

ContextBehavior 服务上下文行为,在扩展服务上下文能力时,匿名嵌入至服务上下文结构体中

func (*ContextBehavior) AddInManager added in v0.4.25

func (ctx *ContextBehavior) AddInManager() extension.AddInManager

AddInManager 获取插件管理器

func (*ContextBehavior) CallAsync added in v0.3.89

func (ctx *ContextBehavior) CallAsync(entityId uid.Id, fun generic.FuncVar1[ec.Entity, any, async.Ret], args ...any) async.AsyncRet

CallAsync 查找实体并异步调用函数,有返回值。不会阻塞当前线程,会返回AsyncRet。

注意:
- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
- 调用过程中的panic信息,均会转换为error返回。

func (*ContextBehavior) CallDelegateAsync added in v0.3.89

func (ctx *ContextBehavior) CallDelegateAsync(entityId uid.Id, fun generic.DelegateVar1[ec.Entity, any, async.Ret], args ...any) async.AsyncRet

CallDelegateAsync 查找实体并异步调用委托,有返回值。不会阻塞当前线程,会返回AsyncRet。

注意:
- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
- 调用过程中的panic信息,均会转换为error返回。

func (*ContextBehavior) CallDelegateVoidAsync added in v0.3.89

func (ctx *ContextBehavior) CallDelegateVoidAsync(entityId uid.Id, fun generic.DelegateVoidVar1[ec.Entity, any], args ...any) async.AsyncRet

CallDelegateVoidAsync 查找实体并异步调用委托,无返回值。在运行时中。不会阻塞当前线程,会返回AsyncRet。

注意:
- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
- 调用过程中的panic信息,均会转换为error返回。

func (*ContextBehavior) CallVoidAsync added in v0.3.89

func (ctx *ContextBehavior) CallVoidAsync(entityId uid.Id, fun generic.ActionVar1[ec.Entity, any], args ...any) async.AsyncRet

CallVoidAsync 查找实体并异步调用函数,无返回值。在运行时中。不会阻塞当前线程,会返回AsyncRet。

注意:
- 代码片段中的线程安全问题,如临界区访问、线程死锁等。
- 调用过程中的panic信息,均会转换为error返回。

func (*ContextBehavior) EntityLib added in v0.4.25

func (ctx *ContextBehavior) EntityLib() pt.EntityLib

EntityLib 获取实体原型库

func (*ContextBehavior) EntityManager added in v0.4.25

func (ctx *ContextBehavior) EntityManager() EntityManager

EntityManager 获取实体管理器

func (*ContextBehavior) Id added in v0.4.25

func (ctx *ContextBehavior) Id() uid.Id

Id 获取服务Id

func (*ContextBehavior) InstanceFaceCache added in v0.4.25

func (ctx *ContextBehavior) InstanceFaceCache() iface.Cache

InstanceFaceCache 支持重新解释类型

func (*ContextBehavior) Name added in v0.4.25

func (ctx *ContextBehavior) Name() string

Name 获取名称

func (*ContextBehavior) Reflected added in v0.4.25

func (ctx *ContextBehavior) Reflected() reflect.Value

Reflected 获取反射值

func (*ContextBehavior) String

func (ctx *ContextBehavior) String() string

String implements fmt.Stringer

type ContextOptions

type ContextOptions struct {
	InstanceFace   iface.Face[Context]           // 实例,用于扩展服务上下文能力
	Context        context.Context               // 父Context
	AutoRecover    bool                          // 是否开启panic时自动恢复
	ReportError    chan error                    // panic时错误写入的error channel
	Name           string                        // 服务名称
	PersistId      uid.Id                        // 服务持久化Id
	EntityLib      pt.EntityLib                  // 实体原型库
	AddInManager   extension.ServiceAddInManager // 插件管理器
	RunningEventCB RunningEventCB                // 运行事件回调
}

ContextOptions 创建服务上下文的所有选项

type EntityManager added in v0.3.66

type EntityManager interface {
	// GetEntity 查询实体
	GetEntity(id uid.Id) (ec.ConcurrentEntity, bool)
	// GetOrAddEntity 查询或添加实体
	GetOrAddEntity(entity ec.ConcurrentEntity) (ec.ConcurrentEntity, bool, error)
	// RemoveEntity 删除实体
	RemoveEntity(id uid.Id)
}

EntityManager 实体管理器接口

type RunningEvent added in v0.4.25

type RunningEvent int32

RunningEvent 运行事件

const (
	RunningEvent_Birth               RunningEvent = iota // 出生
	RunningEvent_Starting                                // 开始启动
	RunningEvent_Started                                 // 已启动
	RunningEvent_Heartbeat                               // 心跳
	RunningEvent_Terminating                             // 开始停止
	RunningEvent_Terminated                              // 已停止
	RunningEvent_AddInActivating                         // 开始激活插件
	RunningEvent_AddInActivated                          // 已激活插件
	RunningEvent_AddInDeactivating                       // 开始去激活插件
	RunningEvent_AddInDeactivated                        // 已去激活插件
	RunningEvent_EntityPTDeclared                        // 实体原型已声明
	RunningEvent_ComponentPTDeclared                     // 组件原型已声明
	RunningEvent_EntityRegistered                        // 实体已注册
	RunningEvent_EntityDeregistered                      // 实体已注销
)

func (RunningEvent) String added in v0.4.25

func (i RunningEvent) String() string

type RunningEventCB added in v0.4.25

type RunningEventCB = generic.ActionVar2[Context, RunningEvent, any] // 运行事件回调

Jump to

Keyboard shortcuts

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