func Register_lua_rpc_RpcClient_newClass(L *lua.LState) int { addr := L.CheckString(1) rpcConn, err := net.Dial("tcp", addr) for { if err == nil { break } logger.Error("rpcConn Connect Error : %v", err.Error()) time.Sleep(time.Second * 3) rpcConn, err = net.Dial("tcp", addr) } ud := L.NewUserData() ud.Value = rpc.NewClient(rpcConn) L.SetMetatable(ud, L.GetTypeMetatable(luaRpcClientTypeName)) 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 }