func NewServer() (*Server, error) { // connect dao server dao: daocli, err := rpc.Dial("tcp", cfg.DaoAddr()) if err != nil { log.Warningf("dao server connect fail(err=%+v), try again...", err.Error()) time.Sleep(1 * time.Second) goto dao } ctrl := make(chan string, 10) game: cli, err := fw.NewClient(cfg.GameAddr(), &gameCliModel{}, ctrl) if err != nil { log.Error(err.Error()) log.Warningf("game server connect fail(err=%+v), try again...", err.Error()) time.Sleep(1 * time.Second) goto game } go cli.Loop() // daemon routine (to be done), with "ctrl" // ...... //////////////////// // new server serverInst = &Server{ dao: daocli, gameCli: cli, gameCtrl: ctrl, connIdAcc: 0, agents: make(map[int]*fw.Agent, 1000), } return serverInst, nil }
func NewModel() *Model { if modelInst != nil { panic("game:Model:NewModel can be invoked only once") } modelInst := new(Model) // connect dao server dao: daocli, err := rpc.Dial("tcp", cfg.DaoAddr()) if err != nil { log.Warningf("dao server connect fail(err=%+v), try again...", err.Error()) time.Sleep(1 * time.Second) goto dao } modelInst.dao = daocli // data init modelInst.gameIdAcc = 0 modelInst.games = map[int]*cow.Game{} modelInst.playerGames = map[int]*cow.Game{} modelInst.freeGames = [][]*cow.Game{} // 1000 games each type // for i, _ := range (modelInst.freeGames) { // modelInst.freeGames[i] = make([]*cow.Game, 0, 1000) // } for i := 0; i < int(RoomTypeCount); i++ { modelInst.freeGames[i] = make([]*cow.Game, 0, 1000) } return modelInst }
func NewClient() (c *Client, err error) { var cli *rpc.Client if cli, err = rpc.Dial("tcp", cfg.DaoAddr()); err != nil { return } c = &Client{*cli} return }