func Register_lua_server_Server_newClass(L *lua.LState) int {
	svc := server.NewServer()
	svc.SetLuaState(L)
	ud := L.NewUserData()
	ud.Value = svc
	L.SetMetatable(ud, L.GetTypeMetatable(luaServerTypeName))
	L.Push(ud)
	return 1
}
Ejemplo n.º 2
0
func StartServices(cfg *config.SvrConfig, id *uint64) *FightServer {
	fightServer = &FightServer{}

	fightServer = &FightServer{
		rpcServer: server.NewServer(),
		battles:   make(map[string]*Battle),
	}
	fightServer.rpcServer.Register(fightServer)

	fightServer.rpcServer.RegCallBackOnConn(
		func(conn server.RpcConn) {
			fightServer.onConn(conn)
		},
	)

	listener, err := net.Listen("tcp", cfg.FsHost[*id])
	if err != nil {
		logger.Fatal("net.Listen: %s", err.Error())
	}

	fightServer.id = *id

	go func() {
		for {
			//For Client/////////////////////////////
			time.Sleep(time.Millisecond * 5)
			conn, err := listener.Accept()
			if err != nil {
				logger.Error("fightserver StartServices %s", err.Error())
				break
			}
			go func() {
				rpcConn := server.NewTCPSocketConn(fightServer.rpcServer, conn, 1000, 0, 1)
				rpcConn.SetResultServer("Connector")

				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 ")
					}
				}()

				fightServer.rpcServer.ServeConn(rpcConn)
			}()
		}
	}()

	return fightServer
}
Ejemplo n.º 3
0
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
}