Example #1
0
func (self *table) get(key string) (ret []byte, err error) {
	if len(key) > keylen {
		return nil, fmt.Errorf("key (%s) len must <= 64", key)
	}

	defer self.tableStats.Record("get", time.Now())
	hid := makeHash(key)

	if self.caches != nil {
		cidx := self.getCacheNode(hid)
		cache := cidx.Get()
		defer cache.Recycle()

		ret, err = redis.Bytes(cache.Conn.Do("GET", self.name+"_"+key))
		//fmt.Print("GET ", self.name+"_"+key, "\n")
		if err != nil {

			if err != redis.ErrNil {
				logger.Fatal("get error: %s (%s, %v)", err.Error(), key, ret)
			} else {
				err = nil
			}
		}

		if ret != nil {
			return
		}
	}

	if self.dbs != nil {
		didx := self.getDbNode(hid)
		db := didx.Get()
		defer db.Recycle()

		var rows *sql.Rows
		rows, err = db.Query("SELECT body from "+self.name+" where id = CAST(? as BINARY(64)) LIMIT 1;", key)

		if err != nil {
			logger.Fatal("get error: %s (%s, %v)", err.Error(), key, rows)
		}

		defer rows.Close()

		for rows.Next() {
			err = rows.Scan(&ret)
			if err != nil {
				logger.Fatal("get scan error %s (%s)", err.Error(), key)
			}
			return
		}
	}

	return nil, nil
}
Example #2
0
func (self *AuthServer) LA_CheckAccount(req *protobuf.LA_CheckAccount, ret *protobuf.AL_CheckAccountResult) error {

	uid := common.GenUUID(req.GetAccount())

	if len(req.GetUid()) > 0 {
		if req.GetUid() != uid { //客户端伪造uid
			(*ret).SetResult(protobuf.AL_CheckAccountResult_AUTH_FAILED)
			return nil
		}
	}

	account := &protobuf.AccountInfo{}
	result, err := db.Query("AccountInfo", uid, account)

	if err != nil {
		(*ret).SetResult(protobuf.AL_CheckAccountResult_SERVERERROR)
		return nil
	}

	if result == false { //用户注册

		account.SetUid(uid)
		account.SetAccount(req.GetAccount())
		account.SetPassword(common.GenPassword(req.GetAccount(), req.GetPassword()))
		account.SetLanguage(req.GetLanguage())
		account.SetOption(req.GetOption())
		account.SetSessionKey(common.GenSessionKey())
		account.SetUdid(req.GetUdid())
		account.SetCreateTime(uint32(time.Now().Unix()))

		db.Write("AccountInfo", uid, account)
		logger.Info("Auth AccountInfo create")

	} else { //用户登陆
		if !common.CheckPassword(account.GetPassword(), req.GetAccount(), req.GetPassword()) {
			(*ret).SetResult(protobuf.AL_CheckAccountResult_AUTH_FAILED)
			return nil
		}
		account.SetSessionKey(common.GenSessionKey()) //保存进缓存
		db.Write("AccountInfo", uid, account)
		logger.Info("Auth Account find")
	}

	self.maincache.Do("SET", "SessionKey_"+uid, []byte(account.GetSessionKey()))

	(*ret).SetResult(protobuf.AL_CheckAccountResult_OK)
	(*ret).SetSessionKey(account.GetSessionKey())
	(*ret).SetUid(account.GetUid())

	logger.Info("ComeInto AuthServer.Login %v, %v", req, ret)

	return nil
}
Example #3
0
func (self *table) del(key string) (err error) {
	if len(key) > keylen {
		return fmt.Errorf("key (%s) len must <= 64", key)
	}

	defer self.tableStats.Record("del", time.Now())
	hid := makeHash(key)

	if self.caches != nil {
		cidx := self.getCacheNode(hid)
		cache := cidx.Get()
		defer cache.Recycle()

		_, err = cache.Conn.Do("DEL", self.name+"_"+key)

		if err != nil {
			if err != redis.ErrNil {
				logger.Fatal("del error: %s (%s)", err.Error(), key)
			} else {
				err = nil
			}
		}
	}

	if self.dbs != nil {
		didx := self.getDbNode(hid)
		db := didx.Get()
		defer db.Recycle()

		_, err = db.Query("DELETE from "+self.name+" where id = CAST(? as BINARY(64));", key)

		if err != nil {
			logger.Fatal("delete error: %s (%s)", err.Error(), key)
		}
	}

	return
}
Example #4
0
func (self *Connector) CS_CheckSession(conn server.RpcConn, login protobuf.CS_CheckSession) (err error) {

	rep := protobuf.SC_CheckSessionResult{}
	uid := login.GetUid()
	var rst []byte

	rst, err = redis.Bytes(self.maincache.Do("GET", "SessionKey_"+uid))
	rep.SetResult(protobuf.SC_CheckSessionResult_AUTH_FAILED)
	rep.SetServerTime(uint32(time.Now().Unix()))
	if rst != nil || err == nil {
		if login.GetSessionKey() == string(rst) {
			rep.SetResult(protobuf.SC_CheckSessionResult_OK)
		}
	}

	logger.Debug("SC_CheckSessionResult %v", rep)

	rep.SetResult(rep.GetResult())

	if rep.GetResult() == protobuf.SC_CheckSessionResult_OK {
		WriteResult(conn, &rep)
		if p, ok := self.playersbyid[login.GetUid()]; ok {
			if err := p.conn.Close(); err == nil {
				logger.Info("kick the online player")
			}
		}

		var base protobuf.PlayerBaseInfo
		logger.Info("query db : %v", login.GetUid())
		result, err := db.Query("playerbase", login.GetUid(), &base)
		if result == false {
			base = protobuf.PlayerBaseInfo{}
			base.SetUid(login.GetUid())

			stat := &protobuf.StatusInfo{}
			stat.SetName("test_" + uid)
			stat.SetLevel(1)

			base.SetStat(stat)
			db.Write("playerbase", login.GetUid(), &base)
			logger.Info("playerbase create %v", login.GetUid())
		} else {
			if err != nil {
				logger.Info("err query db : %v", err)
				return err
			}
			logger.Info("playerbase find")
		}

		p := &Player{PlayerBaseInfo: &base, conn: conn}

		p.SetUid(uid)

		//进入服务器全局表

		self.addPlayer(conn.GetId(), p)

	} else {
		WriteResult(conn, &rep)

		go func() {
			time.Sleep(time.Millisecond * 1000)
			defer func() {
				conn.Close()
			}()
		}()
	}

	return nil
}