entity

package
v0.0.0-...-d853fe5 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TotalBitLen   = 64 // (proxy+inst+id)
	ProxyIDBitNum = 20
	InstIDBitNum  = 8
	IDBitNum      = 36

	ProxyIDMask = 1<<ProxyIDBitNum - 1
	InstIDMask  = 1<<InstIDBitNum - 1
	IDBitMask   = 1<<IDBitNum - 1
)
View Source
const (
	GAME_SERVICE_TICK_INTERVAL = 5 * time.Millisecond
)

Variables

View Source
var BbqSysEntityDesc = EntityDesc{
	TypeName:    "BbqSysEntity",
	HandlerType: (*BbqSysEntity)(nil),
	Methods: map[string]MethodDesc{

		"SysWatch": {
			MethodName:   "SysWatch",
			Handler:      _BbqSysEntity_SysWatch_Remote_Handler,
			LocalHandler: _BbqSysEntity_SysWatch_Local_Handler,
		},

		"SysUnwatch": {
			MethodName:   "SysUnwatch",
			Handler:      _BbqSysEntity_SysUnwatch_Remote_Handler,
			LocalHandler: _BbqSysEntity_SysUnwatch_Local_Handler,
		},

		"SysNotify": {
			MethodName:   "SysNotify",
			Handler:      _BbqSysEntity_SysNotify_Remote_Handler,
			LocalHandler: _BbqSysEntity_SysNotify_Local_Handler,
		},
	},

	Metadata: "bbqsys.proto",
}
View Source
var File_bbqsys_proto protoreflect.FileDescriptor

Functions

func DispatchPkt

func DispatchPkt(e IEntity, pkt *nets.Packet)

func NotMyMethod

func NotMyMethod(err error) bool

func RegisterBbqSysEntity

func RegisterBbqSysEntity(etyMgr *EntityManager, impl BbqSysEntity)

func RegisterCallback

func RegisterCallback(c Context, requestID string, cb Callback)

func Run

func Run(e IEntity)

func SetEntityDesc

func SetEntityDesc(e IEntity, desc *EntityDesc)

func SetProxy

func SetProxy(c Context, rem Proxy)

func SetServiceDesc

func SetServiceDesc(s IService, desc *EntityDesc)

Types

type BbqSys

type BbqSys struct {
	EntityID EntityID
}

func NewBbqSys

func NewBbqSys(c Context) (*BbqSys, error)

func NewBbqSysClient

func NewBbqSysClient(eid EntityID) *BbqSys

func NewBbqSysWithID

func NewBbqSysWithID(c Context, id EntityID) (*BbqSys, error)

func (*BbqSys) SysNotify

func (t *BbqSys) SysNotify(c Context, req *WatchRequest) error

func (*BbqSys) SysUnwatch

func (t *BbqSys) SysUnwatch(c Context, req *WatchRequest) error

func (*BbqSys) SysWatch

func (t *BbqSys) SysWatch(c Context, req *WatchRequest) error

type BbqSysEntity

type BbqSysEntity interface {
	IEntity

	// SysWatch
	SysWatch(c Context, req *WatchRequest) error

	// SysUnwatch
	SysUnwatch(c Context, req *WatchRequest) error

	// SysNotify
	SysNotify(c Context, req *WatchRequest) error
}

BbqSysEntity

type Callback

type Callback func(pkt *nets.Packet)

请求回调

func PopCallback

func PopCallback(c Context, requestID string) (Callback, bool)

type Context

type Context interface {

	/************************************/
	/***** GOLANG.ORG/X/NET/CONTEXT *****/
	/************************************/
	context.Context

	/************************************/
	/******** ENTITY MANAGEMENT********/
	/************************************/
	Copy() (Context, releaseCtx)

	Entity() IEntity

	EntityID() EntityID

	Packet() *nets.Packet

	SrcEntity() EntityID

	/************************************/
	/******** METADATA MANAGEMENT********/
	/************************************/
	SetError(err error)

	// Set is used to store a new key/value pair exclusively for this context.
	// It also lazy initializes  c.Keys if it was not used previously.
	Set(key string, value any)

	// Get returns the value for the given key, ie: (value, true).
	// If the value does not exist it returns (nil, false)
	Get(key string) (value any, exists bool)

	// MustGet returns the value for the given key if it exists, otherwise it panics.
	MustGet(key string) any

	// GetString returns the value associated with the key as a string.
	GetString(key string) (s string)

	// GetBool returns the value associated with the key as a boolean.
	GetBool(key string) (b bool)

	// GetInt returns the value associated with the key as an integer.
	GetInt(key string) (i int)

	// GetInt64 returns the value associated with the key as an integer.
	GetInt64(key string) (i64 int64)

	// GetUint returns the value associated with the key as an unsigned integer.
	GetUint(key string) (ui uint)

	// GetUint64 returns the value associated with the key as an unsigned integer.
	GetUint64(key string) (ui64 uint64)

	// GetFloat64 returns the value associated with the key as a float64.
	GetFloat64(key string) (f64 float64)
	// GetTime returns the value associated with the key as time.
	GetTime(key string) (t time.Time)

	// GetDuration returns the value associated with the key as a duration.
	GetDuration(key string) (d time.Duration)

	// GetStringSlice returns the value associated with the key as a slice of strings.
	GetStringSlice(key string) (ss []string)

	// GetStringMap returns the value associated with the key as a map of interfaces.
	GetStringMap(key string) (sm map[string]any)

	// GetStringMapString returns the value associated with the key as a map of strings.
	GetStringMapString(key string) (sms map[string]string)

	// GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.
	GetStringMapStringSlice(key string) (smss map[string][]string)
	// contains filtered or unexported methods
}

func NewContext

func NewContext(parent context.Context) (Context, releaseCtx)

NewPacket allocates a new packet

type Empty

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

func (*Empty) Descriptor deprecated

func (*Empty) Descriptor() ([]byte, []int)

Deprecated: Use Empty.ProtoReflect.Descriptor instead.

func (*Empty) ProtoMessage

func (*Empty) ProtoMessage()

func (*Empty) ProtoReflect

func (x *Empty) ProtoReflect() protoreflect.Message

func (*Empty) Reset

func (x *Empty) Reset()

func (*Empty) String

func (x *Empty) String() string

type Entity

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

func (*Entity) AddCallback

func (e *Entity) AddCallback(d time.Duration, callback timer.CallbackFunc)

AddOnceTimer 直接用,不需要重写,除非有特殊需求

func (*Entity) AddTimer

func (e *Entity) AddTimer(d time.Duration, callback timer.TimerCallbackFunc)

AddRepeatTimer 直接用,不需要重写,除非有特殊需求

func (*Entity) Context

func (e *Entity) Context() Context

func (*Entity) EntityDesc

func (e *Entity) EntityDesc() *EntityDesc

func (*Entity) EntityID

func (e *Entity) EntityID() EntityID

func (*Entity) OnDestroy

func (e *Entity) OnDestroy()

func (*Entity) OnInit

func (e *Entity) OnInit()

func (*Entity) OnMessage

func (e *Entity) OnMessage(c Context, pkt *nets.Packet) bool

func (*Entity) OnMigrateIn

func (e *Entity) OnMigrateIn()

func (*Entity) OnMigrateOut

func (e *Entity) OnMigrateOut()

func (*Entity) OnNotify

func (e *Entity) OnNotify(w NotifyInfo)

Receive 默认实现

func (*Entity) OnTick

func (e *Entity) OnTick()

func (*Entity) Redirect

func (e *Entity) Redirect(c Context, pkt *nets.Packet, dst EntityID) error

Redirect 重定向请求到指定的dst Entity

func (*Entity) SetTickIntervel

func (e *Entity) SetTickIntervel(t time.Duration)

func (*Entity) Stop

func (e *Entity) Stop()

func (*Entity) SysNotify

func (e *Entity) SysNotify(c Context, req *WatchRequest) error

SysNotify

func (*Entity) SysUnwatch

func (e *Entity) SysUnwatch(c Context, req *WatchRequest) error

SysUnwatch

func (*Entity) SysWatch

func (e *Entity) SysWatch(c Context, req *WatchRequest) error

SysWatch

func (*Entity) Unwatch

func (e *Entity) Unwatch(id EntityID)

func (*Entity) Watch

func (e *Entity) Watch(id EntityID)

type EntityDesc

type EntityDesc struct {
	EntityImpl any

	TypeName string
	// The pointer to the Entity interface. Used to check whether the user
	// provided implementation satisfies the interface requiremente.
	HandlerType any
	Methods     map[string]MethodDesc
	Metadata    any

	EntityMgr *EntityManager
	// contains filtered or unexported fields
}

EntityDesc represents an RPC Entity's specification.

type EntityID

type EntityID uint64

EntityID proxyid + instid + id => (22bit + 10bit + 32bit)

func DstEntity

func DstEntity(pkt *nets.Packet) EntityID

func FixedEntityID

func FixedEntityID(pid ProxyID, iid InstID, id ID) EntityID

func NewEntityID

func NewEntityID(pid ProxyID, iid InstID) EntityID

func SrcEntity

func SrcEntity(pkt *nets.Packet) EntityID

func (EntityID) ID

func (eid EntityID) ID() ID

func (EntityID) InstID

func (eid EntityID) InstID() InstID

func (EntityID) Invalid

func (eid EntityID) Invalid() bool

func (EntityID) ProxyID

func (eid EntityID) ProxyID() ProxyID

func (EntityID) String

func (eid EntityID) String() string

type EntityIDGenerator

type EntityIDGenerator interface {
	NewEntityID() EntityID
}

type EntityManager

type EntityManager struct {
	Proxy

	Services map[string]IService // service name -> service info

	Entities map[EntityID]IEntity // entity id -> entity impl

	ProxyRegister RegisterProxy

	EntityIDGenerator EntityIDGenerator
	// contains filtered or unexported fields
}

EntityManager manage entity lifecycle

func GetEntityMgr

func GetEntityMgr(c Context) *EntityManager

func NewEntityManager

func NewEntityManager() *EntityManager

func (*EntityManager) Close

func (s *EntityManager) Close(ch chan struct{}) error

func (*EntityManager) GetEntity

func (s *EntityManager) GetEntity(eid EntityID) (IEntity, bool)

需要优化, gate的id再拆分,不要直接用这个来判断是不是自己的entity

func (*EntityManager) GetService

func (s *EntityManager) GetService(typ string) (IService, bool)

func (*EntityManager) HandlePacket

func (st *EntityManager) HandlePacket(pkt *nets.Packet) error

func (*EntityManager) InitEntity

func (s *EntityManager) InitEntity(c Context, id EntityID, entity IEntity) error

func (*EntityManager) LocalCall

func (st *EntityManager) LocalCall(pkt *nets.Packet, in any, respChan chan any) error

func (*EntityManager) NewEntity

func (s *EntityManager) NewEntity(c Context, id EntityID, typ string) (IEntity, error)

func (*EntityManager) RegisterEntity

func (s *EntityManager) RegisterEntity(c Context, id EntityID, entity IEntity) error

func (*EntityManager) RegisterEntityDesc

func (s *EntityManager) RegisterEntityDesc(sd *EntityDesc, ss IEntity, intercepter ...ServerInterceptor)

func (*EntityManager) RegisterService

func (s *EntityManager) RegisterService(sd *EntityDesc, ss IService, intercepter ...ServerInterceptor)

func (*EntityManager) ReplaceEntityID

func (s *EntityManager) ReplaceEntityID(old, new EntityID) error

type Handler

type Handler func(ctx Context, req any) (any, error)

type ID

type ID uint64 // 最长IDBitNum = 36Bit

func GenID

func GenID() ID

type IEntity

type IEntity interface {

	// EntityID
	EntityID() EntityID
	Context() Context

	// Entity Lifetime
	OnInit()    // Called when initializing entity struct, override to initialize entity custom fields
	OnDestroy() // Called when entity is destroying (just before destroy)

	// OnMessage 收到请求,返回false则不处理,默认true
	OnMessage(c Context, pkt *nets.Packet) bool

	// Migration
	OnMigrateOut() // Called just before entity is migrating out
	OnMigrateIn()  // Called just after entity is migrating in

	// AddCallback 直接用,不需要重写,除非有特殊需求
	AddCallback(d time.Duration, callback timer.CallbackFunc)
	// AddTimer 直接用,不需要重写,除非有特殊需求
	AddTimer(d time.Duration, callback timer.TimerCallbackFunc)
	// OnTick entity执行过一次事件之后执行一次OnTick, 实时性很高要求的可以通过tick实现
	// 如果没有事件发生,则定时调用,默认5ms,可以重写OnInit()调用SetTickIntervel()自定义间隔时间
	OnTick()
	// SetTickIntervel 自定义OnTick()的间隔时间,只能在OnInit()中调用
	SetTickIntervel(t time.Duration)

	// 关注entity, 如果entity退出或者状态变更会通过OnNotify接收到状态变更通知
	Watch(id EntityID)
	Unwatch(id EntityID)
	OnNotify(NotifyInfo)

	// Redirect 重定向请求到指定的dst Entity
	Redirect(c Context, pkt *nets.Packet, dst EntityID) error

	// 主动结束, 主动调用结束entity的生命周期
	Stop()
	// contains filtered or unexported methods
}

On开头的表示可重写

type IService

type IService interface {
	IEntity

	ServiceDesc() *EntityDesc
	// contains filtered or unexported methods
}

type InstID

type InstID uint32

type MethodDesc

type MethodDesc struct {
	MethodName   string
	Handler      methodHandler
	LocalHandler methodLocalHandler
}

MethodDesc represents an RPC Entity's method specification.

type NotifyInfo

type NotifyInfo struct {
	EntityID EntityID
}

type Proxy

type Proxy interface {
	// for remote call, just send request packet, dont handle response
	SendPacket(pkt *nets.Packet) error
}

func GetProxy

func GetProxy(c Context) Proxy

type ProxyID

type ProxyID uint32

type RegisterProxy

type RegisterProxy interface {
	// RegisterEntityToProxy(eid EntityID) error
	RegisterServiceToProxy(svcName string) error
}

type ServerInfo

type ServerInfo struct {
	// Server is the service implementation the user provides. This is read-only.
	Server any
	// FullMethod is the full RPC method string, i.e., /package.service/method.
	FullMethod string
}

type ServerInterceptor

type ServerInterceptor func(ctx Context, req any, info *ServerInfo, next Handler) (any, error)

type Service

type Service struct {
	Entity
}

func (*Service) ServiceDesc

func (s *Service) ServiceDesc() *EntityDesc

type WatchRequest

type WatchRequest struct {
	EntityID uint64 `protobuf:"varint,1,opt,name=EntityID,proto3" json:"EntityID,omitempty" bson:"EntityID"`
	// contains filtered or unexported fields
}

func (*WatchRequest) Descriptor deprecated

func (*WatchRequest) Descriptor() ([]byte, []int)

Deprecated: Use WatchRequest.ProtoReflect.Descriptor instead.

func (*WatchRequest) GetEntityID

func (x *WatchRequest) GetEntityID() uint64

func (*WatchRequest) ProtoMessage

func (*WatchRequest) ProtoMessage()

func (*WatchRequest) ProtoReflect

func (x *WatchRequest) ProtoReflect() protoreflect.Message

func (*WatchRequest) Reset

func (x *WatchRequest) Reset()

func (*WatchRequest) String

func (x *WatchRequest) String() string

Jump to

Keyboard shortcuts

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