func CreateServices(authcfg config.AuthConfig) *AuthServer { //初始化db logger.Info("Init DB") db.Init() //初始化cache var cacheCfg config.CacheConfig if err := config.ReadConfig("etc/maincache.json", &cacheCfg); err != nil { logger.Fatal("load config failed, error is: %v", err) } logger.Info("Init Cache %v", cacheCfg) pAuthServices = &AuthServer{ maincache: db.NewCachePool(cacheCfg), rpcServer: rpc.NewServer(), exit: make(chan bool), } pAuthServices.rpcServer.Register(pAuthServices) pAuthServices.rpcServer.ListenAndServe(authcfg.AuthHost, nil) return pAuthServices }
func Register_lua_db_CachePool_newClass(L *lua.LState) int { cfg := L.CheckString(2) var cacheCfg config.CacheConfig if err := config.ReadConfig(cfg, &cacheCfg); err != nil { logger.Fatal("load config failed, error is: %v", err) } logger.Info("Init Cache %v", cacheCfg) cache := db.NewCachePool(cacheCfg) ud := L.NewUserData() ud.Value = cache L.SetMetatable(ud, L.GetTypeMetatable(luaCachePoolTypeName)) L.Push(ud) return 1 }
func CreateConnectorServerForClient(cfg config.SvrConfig) *Connector { db.Init() var authCfg config.AuthConfig if err := config.ReadConfig("etc/authserver.json", &authCfg); err != nil { logger.Fatal("load config failed, error is: %v", err) } authConn, err := net.Dial("tcp", authCfg.AuthHost) if err != nil { logger.Fatal("connect logserver failed %s", err.Error()) } var gsCfg config.LoginConfig if err = config.ReadConfig("etc/loginserver.json", &gsCfg); err != nil { logger.Fatal("load config failed, error is: %v", err) } gsConn, err := net.Dial("tcp", gsCfg.LoginHost) if err != nil { logger.Fatal("%s", err.Error()) } pConnector = &Connector{ m: make(map[uint32]serverInfo), authserver: rpc.NewClient(authConn), loginserver: rpc.NewClient(gsConn), rpcServer: server.NewServer(), players: make(map[uint64]*Player), playersbyid: make(map[string]*Player), } //初始化cache logger.Info("Init Cache %v", authCfg.MainCacheProfile) pConnector.maincache = db.NewCachePool(authCfg.MainCacheProfile) pConnector.rpcServer.ApplyProtocol(protobuf.CS_Protocol_value) pConnector.rpcServer.Register(pConnector) pConnector.rpcServer.RegCallBackOnConn( func(conn server.RpcConn) { pConnector.onConn(conn) }, ) pConnector.rpcServer.RegCallBackOnDisConn( func(conn server.RpcConn) { pConnector.onDisConn(conn) }, ) pConnector.rpcServer.RegCallBackOnCallBefore( func(conn server.RpcConn) { conn.Lock() }, ) pConnector.rpcServer.RegCallBackOnCallAfter( func(conn server.RpcConn) { conn.Unlock() }, ) //开始对fightserver的RPC服务 pConnector.FsMgr.Init(pConnector.rpcServer, cfg) listener, err := net.Listen("tcp", cfg.TcpHost) if err != nil { logger.Fatal("net.Listen: %s", err.Error()) } pConnector.id = cfg.ServerID pConnector.listenTcpIp = cfg.TcpHost pConnector.listenHttpIp = cfg.HttpHost pConnector.sendPlayerCountToGateServer() go func() { for { //For Client///////////////////////////// time.Sleep(time.Millisecond * 5) conn, err := listener.Accept() if err != nil { logger.Error("cns StartServices %s", err.Error()) break } go func() { rpcConn := server.NewTCPSocketConn(pConnector.rpcServer, conn, 128, 45, 1) defer func() { if r := recover(); r != nil { logger.Error("player rpc runtime error begin:", r) debug.PrintStack() rpcConn.Close() logger.Error("player rpc runtime error end ") } }() pConnector.rpcServer.ServeConn(rpcConn) }() } }() http.HandleFunc("/", wsServeConnHandler) http.ListenAndServe(cfg.HttpHost, nil) return pConnector }
func NewDBServer(cfg config.DBConfig) (server *DBServer) { server = &DBServer{ dbGroups: map[string]dbGroup{}, cacheGroups: map[string]cacheGroup{}, tables: map[string]*table{}, exit: make(chan bool), } //http.Handle("/debug/state", debugHTTP{server}) //初始化所有的db for key, pools := range cfg.DBProfiles { logger.Info("Init DB Profile %s", key) server.dbGroups = make(map[string]dbGroup) server.dbVirNodes = make(map[string]map[uint32]uint32) server.dbNodes = make(map[string][]uint32) temDbs := make(map[uint32]uint32) temGroups := make(dbGroup) temDbInt := []uint32{} for _, poolCfg := range pools { logger.Info("Init DB %v", poolCfg) leng := makeHash(poolCfg.NodeName) temGroups[leng] = db.NewDBPool(poolCfg) if poolCfg.Vnode <= 0 { poolCfg.Vnode = 1 } var i uint8 for i = 0; i < poolCfg.Vnode; i++ { keys := makeHash(fmt.Sprintf("%s#%d", poolCfg.NodeName, i)) temDbs[keys] = leng temDbInt = append(temDbInt, keys) } } server.dbGroups[key] = temGroups bubbleSort(temDbInt) //排序节点 server.dbVirNodes[key] = temDbs server.dbNodes[key] = temDbInt } //初始化所有的cache for key, pools := range cfg.CacheProfiles { logger.Info("Init Cache Profile %s", key) server.cacheGroups = make(map[string]cacheGroup) server.cacheVirNodes = make(map[string]map[uint32]uint32) server.cacheNodes = make(map[string][]uint32) temDbs := make(map[uint32]uint32) temGroups := make(cacheGroup) temDbInt := []uint32{} for _, poolCfg := range pools { logger.Info("Init Cache %v", poolCfg) leng := makeHash(poolCfg.NodeName) temGroups[leng] = db.NewCachePool(poolCfg) if poolCfg.Vnode <= 0 { poolCfg.Vnode = 1 } //leng := len(server.cacheGroups[key]) - 1 var i uint8 for i = 0; i < poolCfg.Vnode; i++ { keys := makeHash(fmt.Sprintf("%s#%d", poolCfg.NodeName, i)) temDbs[keys] = leng temDbInt = append(temDbInt, keys) } } server.cacheGroups[key] = temGroups bubbleSort(temDbInt) //排序节点 server.cacheVirNodes[key] = temDbs server.cacheNodes[key] = temDbInt } //初始化table for key, table := range cfg.Tables { logger.Info("Init Table: %s %v", key, table) server.tables[key] = NewTable(key, table, server) } return server }