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 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 Register_lua_db_DBInit(L *lua.LState) int { db.Init() return 0 }