//--------------------------------------------------------------------------------------------------------------------------
        //供外部通过QueueAction调用的方法,实际执行线程是DataCacheThread。
        //--------------------------------------------------------------------------------------------------------------------------
        internal void LoadAccount(string accountId)
        {
            Msg_LD_Load msg = new Msg_LD_Load();

            msg.MsgId = (int)DataEnum.TableAccount;
            msg.PrimaryKeys.Add(accountId);
            Msg_LD_SingleLoadRequest reqAccount = new Msg_LD_SingleLoadRequest();

            reqAccount.MsgId    = (int)DataEnum.TableAccount;
            reqAccount.LoadType = Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadSingle;
            reqAccount.Keys.Add(accountId);
            msg.LoadRequests.Add(reqAccount);
            RequestLoad(msg, (ret) => {
                KeyString primaryKey = KeyString.Wrap(ret.PrimaryKeys);
                if (Msg_DL_LoadResult.ErrorNoEnum.Success == ret.ErrorNo)
                {
                    LogSys.Log(LOG_TYPE.INFO, ConsoleColor.Green, "DataCache Load Success: Msg:{0}, Key:{1}", "Account", primaryKey);
                }
                else if (Msg_DL_LoadResult.ErrorNoEnum.NotFound == ret.ErrorNo)
                {
                    LogSys.Log(LOG_TYPE.INFO, ConsoleColor.Yellow, "DataCache Load NotFound: Msg:{0}, Key:{1}", "Account", primaryKey);
                }
                else
                {
                    LogSys.Log(LOG_TYPE.ERROR, ConsoleColor.Red, "DataCache Load Failed: Msg:{0}, Key:{1}, ERROR:{2}", "Account", primaryKey, ret.ErrorInfo);
                }
                UserProcessScheduler dataProcessScheduler = UserServer.Instance.UserProcessScheduler;
                dataProcessScheduler.DefaultUserThread.QueueAction(dataProcessScheduler.LoadAccountCallback, accountId, ret);
            });
        }
        internal void GMLoadUser(string gmAccount, ulong userGuid, LobbyGmMessageDefine jsonMsgId, int nodeHandle)
        {
            string      key = userGuid.ToString();
            Msg_LD_Load msg = new Msg_LD_Load();

            msg.MsgId = (int)DataEnum.TableUserInfo;
            msg.PrimaryKeys.Add(key);
            Msg_LD_SingleLoadRequest reqUser = new Msg_LD_SingleLoadRequest();

            reqUser.MsgId    = (int)DataEnum.TableUserInfo;
            reqUser.LoadType = Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadSingle;
            reqUser.Keys.Add(key);
            msg.LoadRequests.Add(reqUser);
            RequestLoad(msg, (ret) => {
                JsonMessage resultMsg = new JsonMessage(jsonMsgId, gmAccount);
                GameFrameworkMessage.Msg_LC_GmQueryUser protoData = new GameFrameworkMessage.Msg_LC_GmQueryUser();
                protoData.m_Result = GameFrameworkMessage.GmResultEnum.Failed;
                protoData.m_Info   = null;

                KeyString primaryKey = KeyString.Wrap(ret.PrimaryKeys);
                if (Msg_DL_LoadResult.ErrorNoEnum.Success == ret.ErrorNo)
                {
                    LogSys.Log(LOG_TYPE.INFO, "DataCache Load Success: Msg:{0}, Key:{1}", "GmUser", primaryKey);
                    TableUserInfo dataUser = null;
                    foreach (var result in ret.Results)
                    {
                        object _msg;
                        if (DbDataSerializer.Decode(result.Data, DataEnum2Type.Query(result.MsgId), out _msg))
                        {
                            DataEnum msgEnum = (DataEnum)result.MsgId;
                            switch (msgEnum)
                            {
                            case DataEnum.TableUserInfo:
                                dataUser = _msg as TableUserInfo;
                                break;

                            default:
                                LogSys.Log(LOG_TYPE.ERROR, ConsoleColor.Red, "Decode user data ERROR. Wrong message id. UserGuid:{0}, WrongId:{1}", userGuid, msgEnum);
                                break;
                            }
                        }
                    }
                    protoData.m_Result = GameFrameworkMessage.GmResultEnum.Success;
                    protoData.m_Info   = CreateGmUserInfo(dataUser);
                }
                else if (Msg_DL_LoadResult.ErrorNoEnum.NotFound == ret.ErrorNo)
                {
                    LogSys.Log(LOG_TYPE.INFO, ConsoleColor.Yellow, "DataCache Load NotFound: Msg:{0}, Key:{1}", "GmUser", primaryKey);
                }
                else
                {
                    LogSys.Log(LOG_TYPE.ERROR, "DataCache Load Failed: Msg:{0}, Key:{1}, ERROR:{2}", "GmUser", primaryKey, ret.ErrorInfo);
                }
                resultMsg.m_ProtoData = protoData;
                JsonGmMessageDispatcher.SendNodeMessage(nodeHandle, resultMsg);
            });
        }
        internal void LoadUser(ulong userGuid, string accountId, string nickname)
        {
            string      key = userGuid.ToString();
            Msg_LD_Load msg = new Msg_LD_Load();

            msg.MsgId = (int)DataEnum.TableUserInfo;
            msg.PrimaryKeys.Add(key);
            Msg_LD_SingleLoadRequest reqUser = new Msg_LD_SingleLoadRequest();

            reqUser.MsgId    = (int)DataEnum.TableUserInfo;
            reqUser.LoadType = Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadSingle;
            reqUser.Keys.Add(key);
            msg.LoadRequests.Add(reqUser);
            Msg_LD_SingleLoadRequest reqMember = new Msg_LD_SingleLoadRequest();

            reqMember.MsgId    = (int)DataEnum.TableMemberInfo;
            reqMember.LoadType = Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadMulti;
            reqMember.Keys.Add(key);
            msg.LoadRequests.Add(reqMember);
            Msg_LD_SingleLoadRequest reqItem = new Msg_LD_SingleLoadRequest();

            reqItem.MsgId    = (int)DataEnum.TableItemInfo;
            reqItem.LoadType = Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadMulti;
            reqItem.Keys.Add(key);
            msg.LoadRequests.Add(reqItem);
            Msg_LD_SingleLoadRequest reqFriend = new Msg_LD_SingleLoadRequest();

            reqFriend.MsgId    = (int)DataEnum.TableFriendInfo;
            reqFriend.LoadType = Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadMulti;
            reqFriend.Keys.Add(key);
            msg.LoadRequests.Add(reqFriend);
            RequestLoad(msg, (ret) => {
                KeyString primaryKey = KeyString.Wrap(ret.PrimaryKeys);
                if (Msg_DL_LoadResult.ErrorNoEnum.Success == ret.ErrorNo)
                {
                    LogSys.Log(LOG_TYPE.INFO, "DataCache Load Success: Msg:{0}, Key:{1}", "UserInfo", primaryKey);
                }
                else if (Msg_DL_LoadResult.ErrorNoEnum.NotFound == ret.ErrorNo)
                {
                    LogSys.Log(LOG_TYPE.INFO, ConsoleColor.Yellow, "DataCache Load NotFound: Msg:{0}, Key:{1}", "UserInfo", primaryKey);
                }
                else
                {
                    LogSys.Log(LOG_TYPE.ERROR, "DataCache Load Failed: Msg:{0}, Key:{1}, ERROR:{2}", "UserInfo", primaryKey, ret.ErrorInfo);
                }
                UserProcessScheduler dataProcessScheduler = UserServer.Instance.UserProcessScheduler;
                dataProcessScheduler.DefaultUserThread.QueueAction(dataProcessScheduler.LoadUserCallback, ret, accountId, nickname);
            });
        }
    internal static void Load(Msg_LD_Load msg, MyAction <Msg_DL_LoadResult> callback)
    {
        Msg_DL_LoadResult ret = new Msg_DL_LoadResult();

        ret.MsgId = msg.MsgId;
        ret.PrimaryKeys.AddRange(msg.PrimaryKeys);
        ret.SerialNo = msg.SerialNo;;
        ret.ErrorNo  = Msg_DL_LoadResult.ErrorNoEnum.Success;
        if (DataCacheConfig.IsPersistent)
        {
            try {
                for (int i = 0; i < msg.LoadRequests.Count; ++i)
                {
                    Msg_LD_SingleLoadRequest req = msg.LoadRequests[i];
                    switch (req.LoadType)
                    {
                    case Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadAll: {
                        int start = req.Start;
                        int count = req.Count;
                        List <GeneralRecordData> datas = DataDML.LoadAll(req.MsgId, start, count);
                        foreach (GeneralRecordData data in datas)
                        {
                            Msg_DL_SingleRowResult result = new Msg_DL_SingleRowResult();
                            result.MsgId = req.MsgId;
                            result.PrimaryKeys.AddRange(data.PrimaryKeys);
                            result.ForeignKeys.AddRange(data.ForeignKeys);
                            result.DataVersion = data.DataVersion;
                            result.Data        = data.Data;
                            ret.Results.Add(result);
                        }
                    }
                    break;

                    case Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadSingle: {
                        GeneralRecordData      data   = DataDML.LoadSingle(req.MsgId, req.Keys);
                        Msg_DL_SingleRowResult result = new Msg_DL_SingleRowResult();
                        result.MsgId = req.MsgId;

                        if (data != null)
                        {
                            result.PrimaryKeys.AddRange(data.PrimaryKeys);
                            result.ForeignKeys.AddRange(data.ForeignKeys);
                            result.DataVersion = data.DataVersion;
                            result.Data        = data.Data;
                        }
                        else
                        {
                            result.PrimaryKeys.AddRange(msg.PrimaryKeys);
                            result.ForeignKeys.Clear();
                            result.DataVersion = -1;
                            result.Data        = null;
                            ret.ErrorNo        = Msg_DL_LoadResult.ErrorNoEnum.NotFound;
                        }
                        ret.Results.Add(result);
                    }
                    break;

                    case Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadMulti: {
                        List <GeneralRecordData> dataList = DataDML.LoadMulti(req.MsgId, req.Keys);
                        foreach (GeneralRecordData data in dataList)
                        {
                            Msg_DL_SingleRowResult result = new Msg_DL_SingleRowResult();
                            result.MsgId = req.MsgId;
                            result.PrimaryKeys.AddRange(data.PrimaryKeys);
                            result.ForeignKeys.AddRange(data.ForeignKeys);
                            result.DataVersion = data.DataVersion;
                            result.Data        = data.Data;
                            ret.Results.Add(result);
                        }
                    }
                    break;
                    }
                }
            } catch (Exception ex) {
                ret.ErrorNo   = Msg_DL_LoadResult.ErrorNoEnum.PostError;
                ret.ErrorInfo = ex.Message;
                LogSys.Log(LOG_TYPE.ERROR, ConsoleColor.Red, "Load data from mysql ERROR. MsgId:{0}, Key:{1}\nErrorMessage:{2}\nStackTrace:{3}",
                           msg.MsgId, msg.PrimaryKeys, ex.Message, ex.StackTrace);
            } finally {
                DataCacheSystem.Instance.QueueAction(callback, ret);
            }
        }
        else
        {
            ret.ErrorNo   = Msg_DL_LoadResult.ErrorNoEnum.NotFound;
            ret.ErrorInfo = string.Empty;
            DataCacheSystem.Instance.QueueAction(callback, ret);
        }
    }
        //
        internal void GMLoadAccount(string gmAccount, string accountId, int nodeHandle)
        {
            Msg_LD_Load msg = new Msg_LD_Load();

            msg.MsgId = (int)DataEnum.TableAccount;
            msg.PrimaryKeys.Add(accountId);
            Msg_LD_SingleLoadRequest reqAccount = new Msg_LD_SingleLoadRequest();

            reqAccount.MsgId    = (int)DataEnum.TableAccount;
            reqAccount.LoadType = Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadSingle;
            reqAccount.Keys.Add(accountId);
            msg.LoadRequests.Add(reqAccount);
            RequestLoad(msg, (ret) => {
                JsonMessage resultMsg = new JsonMessage(LobbyGmMessageDefine.Msg_CL_GmQueryAccount, gmAccount);
                GameFrameworkMessage.Msg_LC_GmQueryAccount protoData = new GameFrameworkMessage.Msg_LC_GmQueryAccount();
                protoData.m_Result       = GameFrameworkMessage.GmResultEnum.Failed;
                protoData.m_QueryAccount = accountId;
                protoData.m_AccountState = GameFrameworkMessage.GmStateEnum.Offline;
                resultMsg.m_ProtoData    = protoData;

                KeyString primaryKey = KeyString.Wrap(ret.PrimaryKeys);
                if (Msg_DL_LoadResult.ErrorNoEnum.Success == ret.ErrorNo)
                {
                    LogSys.Log(LOG_TYPE.INFO, ConsoleColor.Green, "DataCache Load Success: Msg:{0}, Key:{1}", "GmAccount", primaryKey);
                    TableAccount dataAccount = null;
                    foreach (var result in ret.Results)
                    {
                        object _msg;
                        if (DbDataSerializer.Decode(result.Data, DataEnum2Type.Query(result.MsgId), out _msg))
                        {
                            DataEnum msgEnum = (DataEnum)result.MsgId;
                            switch (msgEnum)
                            {
                            case DataEnum.TableAccount:
                                dataAccount = _msg as TableAccount;
                                break;

                            default:
                                LogSys.Log(LOG_TYPE.ERROR, ConsoleColor.Red, "Decode account data ERROR. Wrong message id. Account:{0}, WrongId:{1}", result.PrimaryKeys[0], msgEnum);
                                break;
                            }
                        }
                    }
                    protoData.m_Result       = GameFrameworkMessage.GmResultEnum.Success;
                    protoData.m_QueryAccount = dataAccount.AccountId;
                    protoData.m_AccountState = GameFrameworkMessage.GmStateEnum.Online;
                    if (dataAccount.IsBanned)
                    {
                        protoData.m_AccountState = GameFrameworkMessage.GmStateEnum.Banned;
                    }
                }
                else if (Msg_DL_LoadResult.ErrorNoEnum.NotFound == ret.ErrorNo)
                {
                    LogSys.Log(LOG_TYPE.INFO, ConsoleColor.Yellow, "DataCache Load NotFound: Msg:{0}, Key:{1}", "GmAccount", primaryKey);
                }
                else
                {
                    LogSys.Log(LOG_TYPE.ERROR, ConsoleColor.Red, "DataCache Load Failed: Msg:{0}, Key:{1}, ERROR:{2}", "GmAccount", primaryKey, ret.ErrorInfo);
                }
                resultMsg.m_ProtoData = protoData;
                JsonGmMessageDispatcher.SendNodeMessage(nodeHandle, resultMsg);
            });
        }
Exemplo n.º 6
0
    //==========================通过QueueAction调用的方法===========================================
    //注意!回调函数目前在缓存线程与db线程都可能调用,回调函数的实现需要是线程安全的(目前一般都是发消息,满足此条件)。
    internal void Load(Msg_LD_Load msg, PBChannel channel, int handle)
    {
        //首先在缓存中查找数据,若未找到,则到DB中查找
        bool isLoadCache      = true;
        Msg_DL_LoadResult ret = new Msg_DL_LoadResult();

        ret.MsgId = msg.MsgId;
        ret.PrimaryKeys.AddRange(msg.PrimaryKeys);
        ret.SerialNo = msg.SerialNo;
        ret.ErrorNo  = 0;
        for (int i = 0; i < msg.LoadRequests.Count; ++i)
        {
            Msg_LD_SingleLoadRequest req = msg.LoadRequests[i];
            KeyString loadKey            = KeyString.Wrap(req.Keys);
            switch (req.LoadType)
            {
            case Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadAll: {
                isLoadCache = false;
            }
            break;

            case Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadSingle: {
                InnerCacheItem item = m_InnerCacheSystem.Find(req.MsgId, loadKey);
                if (item != null)
                {
                    Msg_DL_SingleRowResult result = new Msg_DL_SingleRowResult();
                    result.MsgId = req.MsgId;
                    result.PrimaryKeys.AddRange(req.Keys);
                    result.DataVersion = 0;                 //TODO: 这个DataVersion有用吗?
                    result.Data        = item.DataMessage;
                    ret.Results.Add(result);
                }
                else
                {
                    isLoadCache = false;
                }
            }
            break;

            case Msg_LD_SingleLoadRequest.LoadTypeEnum.LoadMulti: {
                List <InnerCacheItem> itemList = m_InnerCacheSystem.FindByForeignKey(req.MsgId, loadKey);
                foreach (var item in itemList)
                {
                    Msg_DL_SingleRowResult result = new Msg_DL_SingleRowResult();
                    result.MsgId = req.MsgId;
                    result.PrimaryKeys.AddRange(req.Keys);
                    result.DataVersion = 0;                 //TODO: 这个DataVersion有用吗?
                    result.Data        = item.DataMessage;
                    ret.Results.Add(result);
                }
            }
            break;
            }
        }
        if (isLoadCache)
        {
            channel.Send(ret);
            LogSys.Log(LOG_TYPE.INFO, "Load data from cache. MsgId:{0}, Key:{1}", msg.MsgId, KeyString.Wrap(msg.PrimaryKeys).ToString());
        }
        else
        {
            //查找DB交给DBLoad线程操作
            DbThreadManager.Instance.LoadActionQueue.QueueAction(DataLoadImplement.Load, msg, (MyAction <Msg_DL_LoadResult>)((Msg_DL_LoadResult result) => {
                if (result.ErrorNo == Msg_DL_LoadResult.ErrorNoEnum.Success)
                {
                    foreach (Msg_DL_SingleRowResult row in result.Results)
                    {
                        m_InnerCacheSystem.AddOrUpdate(row.MsgId, KeyString.Wrap(row.PrimaryKeys), KeyString.Wrap(row.ForeignKeys), row.Data);
                    }
                }
                channel.Send(result);
                LogSys.Log(LOG_TYPE.INFO, "Load data from database. MsgId:{0}, Key:{1}", msg.MsgId, KeyString.Wrap(msg.PrimaryKeys).ToString());
            }));
        }
    }