Example #1
0
        /// <summary>
        /// 初始化跨服客户端回调对象
        /// </summary>
        /// <param name="callback"></param>
        /// <param name="age"></param>
        /// <param name="version"></param>
        /// <returns>clientId</returns>
        public int InitializeClient(IKuaFuClient callback, KuaFuClientContext clientInfo)
        {
            int  clientId  = -1;
            bool firstInit = false;

            try
            {
                if (clientInfo.GameType == (int)GameTypes.HuanYingSiYuan && clientInfo.ServerId != 0)
                {
                    //如果是第一次初始化,需要清空原始的
                    if (clientInfo.ClientId == 0)
                    {
                        firstInit = true;
                    }

                    Lazy <HuanYingSiYuanAgent> lazy = new Lazy <HuanYingSiYuanAgent>(() =>
                    {
                        clientInfo.ClientId = Persistence.GetUniqueClientId(clientInfo.ServerId);
                        return(new HuanYingSiYuanAgent(clientInfo, callback));
                    });

                    HuanYingSiYuanAgent huanYingSiYuanAgent = ServerId2KuaFuClientAgent.GetOrAdd(clientInfo.ServerId, (x) => { return(lazy.Value); });
                    if (clientInfo.ClientId != huanYingSiYuanAgent.ClientInfo.ClientId)
                    {
                        if (clientInfo.ClientId > 0)
                        {
                            if (huanYingSiYuanAgent.IsClientAlive())
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("InitializeClient服务器ID重复,禁止连接.ServerId:{0},ClientId:{1}", clientInfo.ServerId, clientInfo.ClientId));
                                return(StdErrorCode.Error_Duplicate_Key_ServerId);
                            }
                        }

                        clientId = huanYingSiYuanAgent.ClientInfo.ClientId;
                    }
                    else
                    {
                        clientId = clientInfo.ClientId;
                    }

                    if (firstInit)
                    {
                        huanYingSiYuanAgent.Reset();
                        List <int> list = huanYingSiYuanAgent.GetAliveGameFuBenList();
                        if (null != list && list.Count > 0)
                        {
                            foreach (var id in list)
                            {
                                HuanYingSiYuanFuBenData fuBenData;
                                if (HuanYingSiYuanFuBenDataDict.TryGetValue(id, out fuBenData))
                                {
                                    RemoveGameFuBen(fuBenData, huanYingSiYuanAgent);
                                }
                                else
                                {
                                    huanYingSiYuanAgent.RemoveGameFuBen(id);
                                }
                            }
                        }
                    }
                }
                else
                {
                    LogManager.WriteLog(LogTypes.Error, string.Format("InitializeClient时GameType错误,禁止连接.ServerId:{0},GameType:{1}", clientInfo.ServerId, clientInfo.GameType));
                    return(StdErrorCode.Error_Invalid_GameType);
                }
            }
            catch (System.Exception ex)
            {
                LogManager.WriteExceptionUseCache(string.Format("InitializeClient服务器ID重复,禁止连接.ServerId:{0},ClientId:{1}", clientInfo.ServerId, clientInfo.ClientId));
                return(StdErrorCode.Error_Server_Internal_Error);
            }

            return(clientId);
        }