Skip to content

Guide Document

Cyinx edited this page Feb 16, 2019 · 5 revisions

获取einx

   go get github.com/Cyinx/einx
   在einx中使用mysql
   go get github.com/go-sql-driver/mysql
   在einx中使用gopher-lua
   go get github.com/yuin/gopher-lua

einx框架示例服务器地址

    https://github.com/Cyinx/game_server_einx

使用einx创建一个简单的服务器

package main

import (
	"github.com/Cyinx/einx"
	"github.com/Cyinx/einx/slog"
)
func main() {
	slog.SetLogPath("log/game_server/")
	slog.LogInfo("game_server", "start server...")
	slog.LogInfo("game_server", "hello world...")
	einx.Run()
	einx.Close()
}

创建一个监听tcp端口的服务器

package clientmgr

import (
	"github.com/Cyinx/einx"
	"github.com/Cyinx/einx/slog"
	"msg_def" //this is a package for serialization
)

type Agent = einx.Agent
type AgentID = einx.AgentID
type NetLinker = einx.NetLinker
type EventType = einx.EventType
type Component = einx.Component
type ModuleRouter = einx.ModuleRouter
type ComponentID = einx.ComponentID
type Context = einx.Context
type ProtoTypeID = uint32

var logic = einx.GetModule("logic")
var logic_router = logic.(ModuleRouter)

type ClientMgr struct {
	client_map map[AgentID]*Client
	tcp_link   Component
}

var Instance = &ClientMgr{
	client_map: make(map[AgentID]*Client),
}

func GetClient(agent_id uint64) *Client {
	client, _ := Instance.client_map[AgentID(agent_id)]
	return client
}

func (this *ClientMgr) OnLinkerConneted(id AgentID, agent Agent) {
	this.client_map[id] = &Client{linker: agent.(NetLinker)}
}

func (this *ClientMgr) OnLinkerClosed(id AgentID, agent Agent) {
	delete(this.client_map, id)
}

func (this *ClientMgr) OnComponentError(ctx Context, err error) {

}

func (this *ClientMgr) OnComponentCreate(ctx Context, id ComponentID) {
	component := ctx.GetComponent()
	this.tcp_link = component
	component.Start()
	slog.LogInfo("gate_client", "Tcp sever start success")
}

func (this *ClientMgr) ServeHandler(agent Agent, id ProtoTypeID, b []byte) {
	msg := msg_def.UnmarshalMsg(id, b) //deserialize the msg and send it to the module you want.
	if msg != nil {
		logic_router.RouterMsg(agent, id, msg)
	}

}

func (this *ClientMgr) ServeRpc(agent Agent, id ProtoTypeID, b []byte) {
	msg := msg_def.UnmarshalRpc(id, b) //deserialize the rpc msg and send it to the module you want.
	if msg != nil {
		logic_router.RouterMsg(agent, id, msg)
	}
}

创建一个worker module用于处理逻辑

package main

import (
	"clientmgr" //clientmgr is the package in step 3.
	"github.com/Cyinx/einx"
	"github.com/Cyinx/einx/slog"
)
var logic = einx.GetModule("logic")
func main() {
	slog.SetLogPath("log/game_server/")
	logic.AddTcpServer(":2345", clientmgr.Instance)
	slog.LogInfo("game_server", "start server...")
	einx.Run()
	einx.Close()
}

注册Rpc或者消息handler到worker

import (
	"msg_def"
)
var logic = einx.GetModule("logic")
func InitDBHandler() {
	logic.RegisterRpcHandler("testRpc", testRpc)
	logic.RegisterHandler(msg_def.VersionCheckMsgID, CheckVersion)
}

func testRpc(ctx Context, args []interface{}) {
    rpcData := args[0].([]byte)
}

func CheckVersion(ctx Context, args interface{}) {
     msg := args.(*msg_def.VersionCheckMsg)
}

注册定时器.

import (
	"msg_def"
)
var logic = einx.GetModule("logic")
var timerID uint64 = 0
func InitDBHandler() {
	timerID = logic.AddTimer(40,testTimer,1,"test")
       //如果需要提前清除timer
       logic.RemoveTimer(timerID)
}

func testTimer(ctx Context, args []interface{}) {
    dataInt := args[0].(int)
    dataString := args[1].(string)
}