lokas

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2021 License: MIT Imports: 16 Imported by: 3

README

lokas

a go implementation of entity-component system

Documentation

Index

Constants

View Source
const (
	DEFAULT_PACKET_LEN = 2048 * 4
	DEFAULT_ACTOR_TTL  = 15
)

Variables

View Source
var (
	ErrDataNil = errors.New("data nil")
)

Functions

This section is empty.

Types

type ActorState

type ActorState int //Actor health state
const (
	ACTOR_STARTING ActorState = iota + 1
	ACTOR_HEALTHY
	ACTOR_UNHEALTHY
	ACTOR_ERRORED
	ACTOR_STOPPED
)

type AsyncCallBack

type AsyncCallBack func(context IReqContext)

type Context

type Context struct {
	SessionCreator        func(conn IConn) ISession
	Splitter              bufio.SplitFunc      // packet splitter
	IPChecker             func(ip string) bool // check if an accepted connection is allowed
	IdleTimeAfterOpen     time.Duration        // idle time when open, conn will be closed if not activated after this time
	ReadBufferSize        int                  // buffer size for reading
	WriteBufferSize       int                  // buffer size for writing
	UseNoneBlockingChan   bool                 // use none blocking chan
	ChanSize              int                  // chan size for bufferring
	MaxMessageSize        int                  // max message size for a single packet
	MergedWriteBufferSize int                  // buffer size for merged write
	DisableMergedWrite    bool                 // disable merge multiple message to a single net.Write
	EnableStatistics      bool                 // enable statistics of packets send and recv
	Extra                 interface{}          // used for special cases when custom data is needed
	LongPacketPicker      LongPacketPicker     // check and pick long packet when recv
	LongPacketCreator     LongPacketCreator    // create long packet for send
	MaxPacketWriteLen     int                  // data size for long packet
}

Content context for create a dialer or a listener

type IActor

type IActor interface {
	IEntity
	IProxy
	IModule
	GetLeaseId() (clientv3.LeaseID, bool, error)
	Update(dt time.Duration, now time.Time)
}

IActor standard interface for actor

type IActorContainer

type IActorContainer interface {
	AddActor(actor IActor)
	RemoveActor(actor IActor)
	RemoveActorById(id util.ID) IActor
	GetActorIds() []util.ID
	GetActor(id util.ID) IActor
}

type IComponent

type IComponent interface {
	protocol.ISerializable
	GetEntity() IEntity
	SetDirty(d bool)
	SetRuntime(engine IRuntime)
	GetRuntime() IRuntime
	SetEntity(e IEntity)
	OnAdd(e IEntity, r IRuntime)
	OnRemove(e IEntity, r IRuntime)
	OnCreate(r IRuntime)
	OnDestroy(r IRuntime)
	GetSibling(t protocol.BINARY_TAG) IComponent
}

type IComponentPool

type IComponentPool interface {
	Recycle(comp IComponent)
	Get() IComponent
	Create(args ...interface{}) IComponent
	Destroy()
}

type IConfig

type IConfig interface {
	Save() error
	Load() error
	LoadFromRemote() error
	SetRemoteConfig(p string, etcd string)
	Set(key string, value interface{})
	Get(key string) interface{}
	Sub(key string) IConfig
	GetBool(key string) bool
	GetFloat64(key string) float64
	GetInt(key string) int
	GetString(key string) string
	GetStringMap(key string) map[string]interface{}
	GetStringMapString(key string) map[string]string
	GetStringMapStringSlice(key string) map[string][]string
	GetIntSlice(key string) []int
	GetSizeInBytes(key string) uint
	GetStringSlice(key string) []string
	GetTime(key string) time.Time
	GetDuration(key string) time.Duration
	IsSet(key string) bool
	AllSettings() map[string]interface{}
}

type IConn

type IConn interface {
	net.Conn
	SetUserData(userData interface{}) // set application layer reference data
	GetUserData() interface{}         // get application layer reference data
	GetSession() ISession             // get the bound session
	GetConnTime() time.Time           // get connection init time
	Activate()                        // activate the conn, used with idle check
	// GraceClose() error                // gracefully close the connection, after all the pending packets sent to the peer
	Wait() // wait for close
}

IConn interface for a connection, either a client connection or server accepted connection

type IContext

type IContext interface {
	Get(key string) interface{}
	GetIdType(key string) util.ID
	GetProcessIdType(key string) util.ProcessId
	GetString(key string) string
	Set(key string, value interface{})
}

IContext context interface

type IEntity

type IEntity interface {
	Dirty() bool
	SetDirty(bool)
	Get(t protocol.BINARY_TAG) IComponent
	GetOrCreate(t protocol.BINARY_TAG) IComponent
	Add(c IComponent)
	Remove(t protocol.BINARY_TAG) IComponent
	SetId(id util.ID)
	GetId() util.ID
	Components() map[protocol.BINARY_TAG]IComponent
}

type IModel

type IModel interface {
	GetId() util.ID
	Deserialize(a IProcess) error
	Serialize(a IProcess) error
}

type IModule

type IModule interface {
	Type() string
	Load(conf IConfig) error
	Unload() error
	GetProcess() IProcess
	SetProcess(IProcess)
	Start() *promise.Promise
	Stop() *promise.Promise
	OnStart() error
	OnStop() error
}

IModule module interface

type IModuleCtor

type IModuleCtor interface {
	Type() string
	Create() IModule
}

IModuleCtor module export interface

type INPUT_EVENT added in v0.0.6

type INPUT_EVENT protocol.Enum
const (
	KEY_EVENT_DOWN INPUT_EVENT = iota + 1
	KEY_EVENT_UP
	KEY_EVENT_CLICK
	MOUSE_EVENT_BUTTON_DOWN
	MOUSE_EVENT_BUTTON_UP
	MOUSE_EVENT_BUTTON_CLICK
	MOUSE_EVENT_SCROLL
	MOUSE_EVENT_MOVE
	MOUSE_EVENT_ENTER
	MOUSE_EVENT_LEAVE
	MOUSE_EVENT_CANCEL
)

func (INPUT_EVENT) IsKeyEvent added in v0.0.6

func (this INPUT_EVENT) IsKeyEvent() bool

func (INPUT_EVENT) String added in v0.0.6

func (this INPUT_EVENT) String() string

type INetClient

type INetClient interface {
	events.EventEmmiter
	Connect(addr string) *promise.Promise
	Disconnect(bool) *promise.Promise
	Request(req interface{}) *promise.Promise
	Connected() bool
	OnRecvCmd(cmdId protocol.BINARY_TAG, time time.Duration) *promise.Promise
	OnRecv(conn IConn, data []byte)
}

INetClient interface for client

type IProcess

type IProcess interface {
	IRegistry
	IActorContainer
	IRouter
	Add(modules IModule) IModule                                //add a module
	RegisterModule(ctor IModuleCtor)                            //register a module ctor
	LoadAllModule(IProcessConfig) error                         //load all module from config
	LoadMod(name string, conf IConfig) error                    //load a module with config
	UnloadMod(name string) error                                //unload a module
	Get(name string) IModule                                    //get a module
	Load(IProcessConfig) error                                  //load config
	Start() error                                               //start process
	Stop() error                                                //stop process
	Id() util.ProcessId                                         //ProcessId
	GenId() util.ID                                             //gen snowflake Id,goroutine safe
	GetLogger() *log.ComposeLogger                              //get process logger
	GetMongo() *qmgo.Database                                   //get mongo client
	GetRedis() *redisclient.Client                              //get redis client
	GetEtcd() *etcdclient.Client                                //get etcd client
	GlobalMutex(key string, ttl int) (*etcdclient.Mutex, error) //create a distributed global mutex based on etcd
	Config() IConfig                                            //get config
	GameId() string                                             //get game id
	ServerId() int32                                            //get server id
	GameServerId() string                                       //get game and server id
	Version() string                                            //get version
}

IProcess the interface for application entry

type IProcessConfig

type IProcessConfig interface {
	IConfig
	GetName() string    //config name
	ServerName() string //serverName
	GetProcessId() util.ProcessId
	GetGameId() string
	GetServerId() int32
	GetVersion() string
	GetDb(string) interface{}
	GetAllSub() []IConfig //get all config for sub modules
}

type IProxy

type IProxy interface {
	ReceiveMessage(msg *protocol.RouteMessage)
	OnMessage(msg *protocol.RouteMessage)
	SendMessage(actorId util.ID, transId uint32, msg protocol.ISerializable) error
	Call(actorId util.ID, req protocol.ISerializable) (protocol.ISerializable, error)
}

IProxy universal module interface for connection

type IRegistry

type IRegistry interface {
	RegisterProcessInfo() error
	GetProcessIdByActor(actorId util.ID) (util.ProcessId, error)
	RegisterActors() error
	RegisterActorRemote(actor IActor) error
	UnregisterActorRemote(actor IActor) error
	RegisterServiceRemote(service *Service) error
	UnregisterServiceRemote(service *Service) error
	RegisterActorLocal(actor IActor) error
	UnregisterActorLocal(actor IActor) error
	RegisterServiceLocal(service *Service) error
	UnregisterServiceLocal(service *Service) error
	GetActorIdsByTypeAndServerId(serverId int32, typ string) []util.ID
}

type IReqContext

type IReqContext interface {
	context.Context
	IContext
	GetTransId() uint32
	GetResp() interface{}
	SetResp(interface{})
	SetCallback(cb AsyncCallBack)
	GetCallback() AsyncCallBack
	Cancel(err error)
	Finish()
}

type IRouter

type IRouter interface {
	RouteMsg(msg *protocol.RouteMessage)
}

IRouter interface for router

type IRuntime

type IRuntime interface {
	Init(updateTime int64, timeScale float32, server bool)
	GetContext(name string) interface{}
	SetContext(name string, value interface{})
	GetEntity(util.ID) IEntity
	CurrentTick() int64
	Start()
	Stop()
	RunningTime() int64
	GetTimeScale() float32
	SetTimeScale(scale float32)
	RegisterComponent(name string, c IComponent)
	RegisterSingleton(name string, c IComponent)
	GetComponentType(name string) reflect.Type
	IsSyncAble(compName string) bool
	CreateEntity() IEntity
	IsServer() bool
	//private
	MarkDirtyEntity(e IEntity)
}

type ISession

type ISession interface {
	GetId() util.ID
	GetConn() IConn                 // get corresponding IConn
	OnOpen(conn IConn)              // called when IConn is opened
	OnClose(conn IConn)             // called when IConn is closed
	OnRecv(conn IConn, data []byte) // called when IConn receives data, ATTENTION: data is a slice and must be consumed immediately
}

ISession connection session interface

type ISessionManager

type ISessionManager interface {
	AddSession(id util.ID, session ISession)
	RemoveSession(id util.ID)
	GetSession(id util.ID) ISession
	GetSessionCount() int
	Clear()
	Range(f func(id util.ID, session ISession) bool)
}

type ITestCase

type ITestCase interface {
	Test() *promise.Promise
	TestWithContext(ctx IContext) *promise.Promise
	Name() string
	SetName(string)
	GetTestCase(num int) ITestCase
	GetTestCaseByName(name string) ITestCase
	AddTestCase(name string, testCase ITestCase)
	GetLength() int
}

type KEY added in v0.0.6

type KEY protocol.Enum
const (
	KEY_BACK    KEY = 51  //   8
	KEY_TAB     KEY = 48  //   9
	KEY_RETURN  KEY = 36  //  13
	KEY_MENU    KEY = 110 //  18
	KEY_PAUSE   KEY = 113 //  19
	KEY_CAPITAL KEY = 57  //  20
	KEY_ESCAPE  KEY = 53  //   27
	KEY_SPACE   KEY = 49  //   32
	KEY_PGUP    KEY = 116 //   33
	KEY_PGDN    KEY = 121 //   34
	KEY_END     KEY = 119 //   35
	KEY_HOME    KEY = 115 //   36
	KEY_LEFT    KEY = 123 //   37
	KEY_UP      KEY = 126 //   38
	KEY_RIGHT   KEY = 124 //   39
	KEY_DOWN    KEY = 125 //   40
	KEY_SELECT  KEY = 10  //   41
	KEY_PRINT   KEY = 105 //   42
	KEY_INSERT  KEY = 114 //   45
	KEY_DELETE  KEY = 117 //   46
	KEY_HELP    KEY = -1  //   47
	// VK0 THRU VK9 ARE THE SAME AS ASCII '0' THRU '9' (0X30 - 0X39)
	KEY_0 KEY = 29 //   48
	KEY_1 KEY = 18 //   49
	KEY_2 KEY = 19 //   50
	KEY_3 KEY = 20 //   51
	KEY_4 KEY = 21 //   52
	KEY_5 KEY = 23 //   53
	KEY_6 KEY = 22 //   54
	KEY_7 KEY = 26 //   55
	KEY_8 KEY = 28 //   56
	KEY_9 KEY = 25 //   57
	// VKA THRU VKZ ARE THE SAME AS ASCII 'A' THRU 'Z' (0X41 - 0X5A)
	KEY_A           KEY = 0    //   65
	KEY_B           KEY = 11   //   66
	KEY_C           KEY = 8    //   67
	KEY_D           KEY = 2    //   68
	KEY_E           KEY = 14   //   69
	KEY_F           KEY = 3    //   70
	KEY_G           KEY = 5    //   71
	KEY_H           KEY = 4    //   72
	KEY_I           KEY = 34   //   73
	KEY_J           KEY = 38   //   74
	KEY_K           KEY = 40   //   75
	KEY_L           KEY = 37   //   76
	KEY_M           KEY = 46   //   77
	KEY_N           KEY = 45   //   78
	KEY_O           KEY = 31   //   79
	KEY_P           KEY = 35   //   80
	KEY_Q           KEY = 12   //   81
	KEY_R           KEY = 15   //   82
	KEY_S           KEY = 0x01 //   83
	KEY_T           KEY = 17   //   84
	KEY_U           KEY = 32   //   85
	KEY_V           KEY = 9    //   86
	KEY_W           KEY = 13   //   87
	KEY_X           KEY = 7    //   88
	KEY_Y           KEY = 16   //   89
	KEY_Z           KEY = 6    //   90
	KEY_LWIN        KEY = 59   //   91
	KEY_RWIN        KEY = 62   //   92
	KEY_NUMPAD0     KEY = 82   //   96
	KEY_NUMPAD1     KEY = 83   //   97
	KEY_NUMPAD2     KEY = 84   //   98
	KEY_NUMPAD3     KEY = 85   //   99
	KEY_NUMPAD4     KEY = 86   //   100
	KEY_NUMPAD5     KEY = 87   //   101
	KEY_NUMPAD6     KEY = 88   //   102
	KEY_NUMPAD7     KEY = 89   //   103
	KEY_NUMPAD8     KEY = 91   //   104
	KEY_NUMPAD9     KEY = 92   //   105
	KEY_MULTIPLY    KEY = 67   //   106
	KEY_ADD         KEY = 69   //   107
	KEY_NUMPADENTER KEY = 76
	KEY_SEPARATOR   KEY = 108 //   108
	KEY_SUBTRACT    KEY = 78  //   109
	KEY_DECIMAL     KEY = 65  //   110
	KEY_DIVIDEKEY   KEY = 75  //   111
	KEY_F1          KEY = 122 //   112
	KEY_F2          KEY = 120 //   113
	KEY_F3          KEY = 99  //   114
	KEY_F4          KEY = 118 //   115
	KEY_F5          KEY = 96  //   116
	KEY_F6          KEY = 97  //   117
	KEY_F7          KEY = 98  //   118
	KEY_F8          KEY = 100 //   119
	KEY_F9          KEY = 101 //   120
	KEY_F10         KEY = 109 //   121
	KEY_F11         KEY = 103 //   122
	KEY_F12         KEY = 111 //   123
	KEY_F13         KEY = -1  //   124
	KEY_F14         KEY = -1  //   125
	KEY_F15         KEY = -1  //   126
	KEY_F16         KEY = -1  //   127
	KEY_F17         KEY = -1  //   128
	KEY_F18         KEY = -1  //   129
	KEY_F19         KEY = -1  //   130
	KEY_F20         KEY = -1  //   131
	KEY_F21         KEY = -1  //   132
	KEY_F22         KEY = -1  //   133
	KEY_F23         KEY = -1  //   134
	KEY_F24         KEY = -1  //   135

	KEY_NUMLOCK  KEY = 71  //   144
	KEY_SCROLL   KEY = 107 //   145
	KEY_LSHIFT   KEY = 56  //   160
	KEY_RSHIFT   KEY = 60  //   161
	KEY_LCONTROL KEY = 54  //   162
	KEY_RCONTROL KEY = 55  //   163
	KEY_LALT     KEY = 58  //   164
	KEY_RALT     KEY = 61  //   165

	KEY_VOLUMEMUTE KEY = 0x4A //   173
	KEY_VOLUMEDOWN KEY = 0x49 //   174
	KEY_VOLUMEUP   KEY = 0x48 //   175

	KEY_SEMICOLONKEY KEY = 41  //   186
	KEY_EQUAL        KEY = 24  //   187
	KEY_COMMA        KEY = 43  //   188
	KEY_MINUS        KEY = 27  //   189
	KEY_PERIOD       KEY = 47  //   190
	KEY_SLASH        KEY = 44  //   191
	KEY_TILDE        KEY = 50  //   192
	KEY_LEFTBRACKET  KEY = 33  //   219
	KEY_BACKSLASHKEY KEY = 42  //   220
	KEY_RIGHTBRACKET KEY = 30  //   221
	KEY_QUOTE        KEY = 39  //   222
	KEY_NONE         KEY = 255 //   255
)

type Key

type Key string

func (Key) Assemble

func (this Key) Assemble(args ...interface{}) string

type LongPacketCreator

type LongPacketCreator func(data []byte, idx int) ([]byte, error)

create long packet args: data:binary to create idx: long packet index return: 1.long packet

type LongPacketPicker

type LongPacketPicker func(data []byte) (bool, int, []byte)

pick long packet from binary args: data:binary return: 1.is long packet 2.long packet index, if index = 0 is the last long packet 3.if is long packet return real packet binary else return nil

type MOUSE_BUTTON added in v0.0.6

type MOUSE_BUTTON protocol.Enum
const (
	MOUSE_BUTTON_LEFT MOUSE_BUTTON = iota + 1
	MOUSE_BUTTON_RIGHT
	MOUSE_BUTTON_MIDDLE
	MOUSE_BUTTON_EXTRA1
	MOUSE_BUTTON_EXTRA2
)

type Server

type Server interface {
	Start(addr string) error                     //start server
	Stop()                                       //stop server
	Broadcast(sessionIds []util.ID, data []byte) // broadcast data to all connected sessions
	GetActiveConnNum() int                       // get current count of connections
}

Server the interface for wsserver and tcpserver

type Service

type Service struct {
	Id        protocol.BINARY_TAG //service Id
	ProcessId util.ProcessId      //server Id
	ActorId   util.ID             //actor Id
	Type      ServiceType         //rpc pub/sub stateless
}

Service the registry for service details

type ServiceType

type ServiceType int
const (
	//rpc have a actorId<util.Id> to route message
	//SERVICE_RPC ServiceType = iota + 1
	//subscribe have a actorId<util.Id> to pub message
	SERVICE_SUB ServiceType = iota + 1
	//stateless  do not have local state
	SERVICE_STATELESS
	SERVICE_SERVER
)

Jump to

Keyboard shortcuts

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