Esempio n. 1
0
func (p *PlayerList) AddPlayer(session int64) *BasePlayer {

	if _, dup := p.players[session]; dup {
		return nil
	}

	pl := &BasePlayer{}
	pl.Mailbox = rpc.NewMailBox(1, session, App.AppId)
	pl.Session = session
	p.players[session] = pl
	return pl
}
Esempio n. 2
0
func (a *Account) SavePlayer(mailbox rpc.Mailbox, msg *rpc.Message) (errcode int32, reply *rpc.Message) {
	r := server.NewMessageReader(msg)
	var data share.UpdateUser
	if server.Check(r.ReadObject(&data)) {
		return 0, nil
	}
	if data.SaveData.Data == nil {
		log.LogError("save data is nil")
		return 0, nil
	}

	sqlconn := db.sql
	base := rpc.NewMailBox(0, 0, mailbox.App)
	infos := make([]share.ObjectInfo, 0, 128)
	if err := UpdateItem(sqlconn, data.SaveData.Data.DBId, data.SaveData.Data); err != nil {
		log.LogError(err)
		server.Check(server.MailTo(&mailbox, &base, "DbBridge.SavePlayerBak", err.Error()))
		return 0, nil
	}

	if data.Type == share.SAVETYPE_OFFLINE {
		_, err := db.sql.Exec("UPDATE `role_info` SET `status`=?, `serverid`=?, `scene`=?, `scene_x`=?, `scene_y`=?, `scene_z`=?, `scene_dir`=?, `roleinfo`=? WHERE `account`=? and `rolename`=?",
			0,
			"",
			data.Scene, data.X, data.Y, data.Z, data.Dir,
			data.SaveData.RoleInfo,
			data.Account,
			data.Name,
		)
		if err != nil {
			log.LogError(err)
			server.Check(server.MailTo(&mailbox, &base, "DbBridge.SavePlayerBak", err.Error()))
			return 0, nil
		}

		server.Check(server.MailTo(&mailbox, &base, "DbBridge.SavePlayerBak", "ok"))
		return 0, nil
	}

	bakinfo := share.UpdateUserBak{}
	bakinfo.Infos = infos
	server.Check(server.MailTo(&mailbox, &base, "DbBridge.UpdateUserInfo", bakinfo))
	return 0, nil
}
Esempio n. 3
0
func (a *Account) SavePlayer(mailbox rpc.Mailbox, data share.UpdateUser) error {
	return a.process("SavePlayer", func() error {
		base := rpc.NewMailBox(0, 0, mailbox.App)

		//save
		if err := SaveToDb(db.DB, 0, &data.SaveData); err != nil {
			log.LogError(err)
			return server.MailTo(&mailbox, &base, "DbBridge.SavePlayerBak", err.Error())
		}

		if data.Type == share.SAVETYPE_OFFLINE {
			if err := a.ClearPlayerStatus(mailbox, share.ClearUser{Account: data.Account, Name: data.Name}); err != nil {
				log.LogError(err)
				return server.MailTo(&mailbox, &base, "DbBridge.SavePlayerBak", err.Error())
			}
		}
		log.LogMessage("player:", data.Name, " save succeed")
		return server.MailTo(&mailbox, &base, "DbBridge.SavePlayerBak", "ok")
	})
}
Esempio n. 4
0
func (c *WSClientHandler) Handle(ws *websocket.Conn) {
	if core.Closing {
		ws.Close()
		return
	}
	rwc := NewWSConn(ws)
	id := core.clientList.Add(rwc, ws.RemoteAddr().String())
	mailbox := rpc.NewMailBox(1, id, core.AppId)
	core.Emitter.Push(NEWUSERCONN, map[string]interface{}{"id": id}, true)
	cl := core.clientList.FindNode(id)
	cl.MailBox = mailbox
	cl.Run()
	codec := &ClientCodec{}
	codec.rwc = rwc
	codec.cachebuf = make([]byte, SENDBUFLEN)
	codec.node = cl
	log.LogInfo("new client:", mailbox, ",", ws.RemoteAddr())
	core.rpcServer.ServeCodec(codec, rpc.MAX_BUF_LEN)
	core.Emitter.Push(LOSTUSERCONN, map[string]interface{}{"id": cl.Session}, true)
	log.LogMessage("client handle quit")
}
Esempio n. 5
0
func (svr *Server) Start(master string, localip string, outerip string, typ string, argstr string) bool {
	svr.startTime = time.Now()
	defer func() {
		if e := recover(); e != nil {
			log.LogFatalf(e)
		}
	}()

	args, err := simplejson.NewJson([]byte(argstr))
	if err != nil {
		panic(err)
	}
	svr.Type = typ
	svr.StartArgs = args
	if name, ok := args.CheckGet("name"); ok {
		v, err := name.String()
		if err != nil {
			panic(err)
		}
		svr.Name = v
	} else {
		panic("app id not defined")
	}
	svr.MailBox = rpc.NewMailBox(0, 0, svr.AppId)

	//now := time.Now()
	//log.WriteToFile(fmt.Sprintf("log/%s_%d_%d_%d_%d_%d_%d.log", svr.Name, now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second()))

	log.LogMessage("id:", svr.AppId, ", name:", svr.Name)

	svr.Host = localip
	if svr.Host == "" {
		if host, ok := args.CheckGet("host"); ok {
			v, err := host.String()
			if err != nil {
				panic(err)
			}
			svr.Host = v
		} else {
			panic(svr.Name + " host not defined")
		}
	}

	if port, ok := args.CheckGet("port"); ok {
		v, err := port.Int()
		if err != nil {
			panic(err)
		}
		svr.Port = v
	} else {
		panic(svr.Name + "port not defined")
	}

	svr.ClientHost = outerip
	if svr.ClientHost == "" {
		if clienthost, ok := args.CheckGet("clienthost"); ok {
			v, err := clienthost.String()
			if err != nil {
				panic(err)
			}
			svr.ClientHost = v
		}
	}

	if clientport, ok := args.CheckGet("clientport"); ok {
		v, err := clientport.Int()
		if err != nil {
			panic(err)
		}
		svr.ClientPort = v
	}

	if fronted, ok := args.CheckGet("fronted"); ok {
		v, err := fronted.Bool()
		if err != nil {
			panic(err)
		}

		svr.Fronted = v
	}

	svr.Sockettype = "native"
	if Sockettype, ok := args.CheckGet("sockettype"); ok {
		v, err := Sockettype.String()
		if err != nil {
			panic(err)
		}

		svr.Sockettype = v
	}

	//rpc端口
	r, err := net.Listen("tcp", fmt.Sprintf("%s:%d", svr.Host, svr.Port))
	if err != nil {
		panic(err)
	}
	svr.rpcListener = r
	if svr.Port == 0 {
		svr.Port = r.Addr().(*net.TCPAddr).Port
	}

	if svr.Fronted && !svr.apper.RawSock() {
		log.TraceInfo(svr.Name, "init link")
		listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", svr.ClientHost, svr.ClientPort))
		if err != nil {
			panic(err)
		}

		svr.clientListener = listener
		if svr.ClientPort == 0 {
			svr.ClientPort = listener.Addr().(*net.TCPAddr).Port
		}

		switch svr.Sockettype {
		case "websocket":
			svr.WaitGroup.Wrap(func() { util.WSServer(svr.clientListener, &WSClientHandler{}) })
		default:
			svr.WaitGroup.Wrap(func() { util.TCPServer(svr.clientListener, &ClientHandler{}) })
		}

		log.TraceInfo(svr.Name, "start link complete")
	}

	master_peer := &master_peer{}
	peer := &peer{addr: master, h: master_peer}
	if err := peer.Connect(); err != nil {
		panic(err)
	}

	svr.noder = peer

	signalChan := make(chan os.Signal, 1)
	go func() {
		for {
			if _, ok := <-signalChan; !ok {
				return
			}
		}

	}()

	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

	if debug, ok := args.CheckGet("debug"); ok {
		v, err := debug.Bool()
		if err == nil {
			svr.Debug = v
		}
	}

	if pfport, ok := args.CheckGet("pprof"); ok {
		v, err := pfport.Int()
		if err == nil {
			svr.PProfPort = v
		}
	}

	if assets, ok := args.CheckGet("assets"); ok {
		v, err := assets.String()
		if err == nil {
			svr.AssetPath = v
		}
	}

	if loglevel, ok := args.CheckGet("loglevel"); ok {
		v, err := loglevel.Int()
		if err == nil {
			log.SetLogLevel("stdout", v)
		}
	}

	svr.globalset = "GlobalSet"
	if gs, ok := args.CheckGet("globalset"); ok {
		v, err := gs.String()
		if err == nil {
			svr.globalset = v
		}
	}

	svr.globaldataserver = false
	if enable, ok := args.CheckGet("globaldataserver"); ok {
		v, err := enable.Bool()
		if err == nil {
			svr.globaldataserver = v
		}
	}

	svr.enableglobaldata = false
	if enable, ok := args.CheckGet("enableglobaldata"); ok {
		v, err := enable.Bool()
		if err == nil {
			svr.enableglobaldata = v
		}
	}

	svr.maxglobalentry = 10
	if max, ok := args.CheckGet("maxglobalentry"); ok {
		v, err := max.Int()
		if err == nil {
			svr.maxglobalentry = v
		}
	}

	log.LogMessage("global data status is ", svr.enableglobaldata)

	if svr.globaldataserver {
		svr.globalHelper.SetServer()
		log.LogMessage("promote to global data server")
	}

	serial := (uint64(time.Now().Unix()%0x80000000) << 32) | (uint64(svr.AppId) << 24)
	svr.setSerial(serial)
	log.LogMessage("start serial:", fmt.Sprintf("%X", serial))
	if !svr.apper.OnPrepare() {
		return false
	}
	svr.CurrentInBase(svr.apper.IsBase())
	if svr.AssetPath != "" {
		helper.LoadAllConfig(svr.AssetPath)
	}

	//内部rpc注册
	svr.rpcCh = make(chan *rpc.RpcCall, RPCBUFFER)
	svr.rpcServer = createRpc(svr.rpcCh)
	svr.WaitGroup.Wrap(func() { rpc.CreateService(svr.rpcServer, svr.rpcListener) })

	svr.rpcProto = codec
	if svr.rpcProto == nil {
		panic("proto not set")
	}
	log.LogMessage("client proto:", svr.rpcProto.GetCodecInfo())
	svr.Ready()

	return true
}