Beispiel #1
0
        public static TCPCmdWrapper Get(TCPManager tcpMgr,
                                        TMSKSocket socket,
                                        TCPClientPool tcpClientPool,
                                        TCPRandKey tcpRandKey,
                                        TCPOutPacketPool pool,
                                        int nID,
                                        byte[] data,
                                        int count)
        {
            TCPCmdWrapper wrapper = null;

            lock (CachedWrapperList)
            {
                if (CachedWrapperList.Count > 0)
                {
                    wrapper               = CachedWrapperList.Dequeue();
                    wrapper.tcpMgr        = tcpMgr;
                    wrapper.socket        = socket;
                    wrapper.tcpClientPool = tcpClientPool;
                    wrapper.tcpRandKey    = tcpRandKey;
                    wrapper.pool          = pool;
                    wrapper.nID           = nID;
                    wrapper.data          = data;
                    wrapper.count         = count;
                }
            }
            if (null == wrapper)
            {
                wrapper = new TCPCmdWrapper(tcpMgr, socket, tcpClientPool, tcpRandKey, pool, nID, data, count);
            }
            return(wrapper);
        }
Beispiel #2
0
 public void addTCPCmdWrapper(TCPCmdWrapper wrapper, out int posCmdNum)
 {
     posCmdNum = 0;
     lock (this._cmdWrapperQueue)
     {
         this._cmdWrapperQueue.Enqueue(wrapper);
     }
     if (611 == wrapper.NID)
     {
         this.cmdNum++;
         if (TimeUtil.NOW() - this.beginTime >= 10000L)
         {
             if (this.cmdNum >= TCPSession.MaxPosCmdNumPer5Seconds)
             {
                 posCmdNum      = this.cmdNum;
                 this.cmdNum    = 0;
                 this.beginTime = TimeUtil.NOW();
             }
             else
             {
                 this.cmdNum    = 0;
                 this.beginTime = TimeUtil.NOW();
             }
         }
     }
 }
Beispiel #3
0
        //public long ParsePositionTicks(TCPCmdWrapper wrapper)
        //{
        //    SpritePositionData cmdData = null;

        //    try
        //    {
        //        cmdData = DataHelper.BytesToObject<SpritePositionData>(wrapper.Data, 0, wrapper.Count);
        //    }
        //    catch (Exception) //解析错误
        //    {
        //        return 0;
        //    }

        //    //解析用户名称和用户密码
        //    //string[] fields = cmdData.Split(':');
        //    //if (fields.Length != 5)
        //    if (null == cmdData)
        //    {
        //        return 0;
        //    }

        //    long currentPosTicks = cmdData.currentPosTicks;
        //    return currentPosTicks;
        //}

        public void addTCPCmdWrapper(TCPCmdWrapper wrapper, out int posCmdNum)
        {
            posCmdNum = 0;

            lock (_cmdWrapperQueue)
            {
                _cmdWrapperQueue.Enqueue(wrapper);
            }

            if ((int)TCPGameServerCmds.CMD_SPR_CHECK /*TCPGameServerCmds.CMD_SPR_POSITION*/ == wrapper.NID)
            {
//                 long ticks = ParsePositionTicks(wrapper);
//                 if (ticks <= 0)
//                 {
                cmdNum++;

                if ((TimeUtil.NOW() - beginTime) >= TimeUtil.SECOND * 10)
                {
                    if (cmdNum >= MaxPosCmdNumPer5Seconds)
                    {
                        posCmdNum = cmdNum;
                        cmdNum    = 0;
                        beginTime = TimeUtil.NOW();
                    }
                    else
                    {
                        cmdNum    = 0;
                        beginTime = TimeUtil.NOW();
                    }
                }
//                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// 命令包接收完毕后的回调事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private bool TCPCmdPacketEvent(object sender, int doType)
        {
            TCPInPacket tcpInPacket = sender as TCPInPacket;

            if (0 == doType) //正常的登录指令包
            {
                int thisTimeCheckTicks = 0;
                int checkErrorCode     = 0;

                if (0xffff == tcpInPacket.PacketCmdID)
                {
                    String cmdData = new UTF8Encoding().GetString(tcpInPacket.GetPacketBytes(), 0, tcpInPacket.PacketDataSize);
                    if (cmdData == "serveripinfo")
                    {
                        String  strinfo     = string.Format("{0}_{1}", ServerPort, Global.GetLocalAddressIPs());
                        byte [] arrSendData = Encoding.UTF8.GetBytes(strinfo);
                        if (null != arrSendData)
                        {
                            tcpInPacket.CurrentSocket.Send(arrSendData, arrSendData.Length, SocketFlags.None);
                        }
                    }
                    return(false);
                }
                //这里实际上是有一个指令流的拷贝,占用的很零碎的内存,这个是否考虑使用内存池????
                byte[] bytesData = CheckClientDataValid(tcpInPacket.PacketCmdID, tcpInPacket.GetPacketBytes(), tcpInPacket.PacketDataSize, tcpInPacket.LastCheckTicks, out thisTimeCheckTicks, out checkErrorCode);

                if (null != bytesData)
                {
                    tcpInPacket.LastCheckTicks = thisTimeCheckTicks; //记忆此次的心跳数据
//#if false
                    if (UseWorkerPool)
                    {
                        //杰隆的异步处理指令入口代码
                        TCPCmdWrapper wrapper = TCPCmdWrapper.Get(this, tcpInPacket.CurrentSocket, tcpClientPool, tcpRandKey, tcpOutPacketPool, tcpInPacket.PacketCmdID, bytesData, tcpInPacket.PacketDataSize - 1 - 4);

                        TCPSession session = null;
                        if (GameManager.FlagOptimizeLock3)
                        {
                            if (null != tcpInPacket.CurrentSocket)
                            {
                                session = tcpInPacket.CurrentSocket.session;
                            }
                            if (null == session)
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("未与客户端建立会话: {0},{1}, 错误码:{2}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), checkErrorCode));
                                return(false);
                            }
                        }
                        else
                        {
                            lock (tcpSessions)
                            {
                                if (!tcpSessions.TryGetValue(tcpInPacket.CurrentSocket, out session))
                                {
                                    LogManager.WriteLog(LogTypes.Error, string.Format("未与客户端建立会话: {0},{1}, 错误码:{2}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), checkErrorCode));
                                    return(false);
                                }
                            }
                        }

                        int posCmdNum = 0;
                        session.addTCPCmdWrapper(wrapper, out posCmdNum);
                        if (posCmdNum > 0)
                        {
                            int        banSpeedUpMinutes = GameManager.PlatConfigMgr.GetGameConfigItemInt(PlatConfigNames.BanSpeedUpMinutes2, 10); //加速禁止登陆的时间
                            GameClient client            = GameManager.ClientMgr.FindClient(tcpInPacket.CurrentSocket);
                            if (null != client)
                            {
                                GameManager.ClientMgr.NotifyImportantMsg(this.MySocketListener, tcpOutPacketPool, client, StringUtil.substitute(Global.GetLang("本游戏禁止使用加速软件,{0}分钟内将禁止登陆!"), banSpeedUpMinutes), GameInfoTypeIndexes.Error, ShowGameInfoTypes.HintAndBox);
                                BanManager.BanRoleName(Global.FormatRoleName(client, client.ClientData.RoleName), banSpeedUpMinutes);
                            }

                            LogManager.WriteLog(LogTypes.Error, string.Format("通过POSITION指令判断客户端加速: {0}, 指令个数:{1}, 断开连接", Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), posCmdNum));
                            return(false);
                        }

                        taskExecutor.execute(new ProcessSessionTask(session));
                    }
//#else
                    else
                    {
                        TCPSession session = null;
                        if (GameManager.FlagOptimizeLock3)
                        {
                            if (null != tcpInPacket.CurrentSocket)
                            {
                                session = tcpInPacket.CurrentSocket.session;
                            }
                            if (null == session)
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("未与客户端建立会话: {0},{1}, 错误码:{2}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), checkErrorCode));
                                return(false);
                            }
                        }
                        else
                        {
                            lock (tcpSessions)
                            {
                                if (!tcpSessions.TryGetValue(tcpInPacket.CurrentSocket, out session))
                                {
                                    LogManager.WriteLog(LogTypes.Error, string.Format("未与客户端建立会话: {0},{1}, 错误码:{2}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), checkErrorCode));
                                    return(false);
                                }
                            }
                        }

                        int posCmdNum = 0;
                        session.CheckCmdNum(tcpInPacket.PacketCmdID, out posCmdNum);
                        if (posCmdNum > 0)
                        {
                            int        banSpeedUpMinutes = GameManager.PlatConfigMgr.GetGameConfigItemInt(PlatConfigNames.BanSpeedUpMinutes2, 10); //加速禁止登陆的时间
                            GameClient client            = GameManager.ClientMgr.FindClient(tcpInPacket.CurrentSocket);
                            if (null != client)
                            {
                                if (client.CheckCheatData.ProcessBooster)
                                {
                                    GameManager.ClientMgr.NotifyImportantMsg(this.MySocketListener, tcpOutPacketPool, client, StringUtil.substitute(Global.GetLang("本游戏禁止使用加速软件,{0}分钟内将禁止登陆!"), banSpeedUpMinutes), GameInfoTypeIndexes.Error, ShowGameInfoTypes.HintAndBox);
                                    BanManager.BanRoleName(Global.FormatRoleName(client, client.ClientData.RoleName), banSpeedUpMinutes);

                                    LogManager.WriteLog(LogTypes.Error, string.Format("通过POSITION指令判断客户端加速: {0}, 指令个数:{1}, 断开连接", Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), posCmdNum));
                                    return(false);
                                }
                                else if (client.CheckCheatData.ProcessBoosterTicks == 0)
                                {
                                    client.CheckCheatData.ProcessBoosterTicks = TimeUtil.NOW();
                                }
                            }
                        }

                        TCPOutPacket         tcpOutPacket = null;
                        TCPProcessCmdResults result       = TCPProcessCmdResults.RESULT_FAILED;
                        long processBeginTime             = TimeUtil.NowEx();
                        if (tcpInPacket.PacketCmdID > 30000)
                        {
                            result = CCTCPCmdHandler.CCProcessCmd(this, tcpInPacket.CurrentSocket, tcpClientPool, tcpRandKey, tcpOutPacketPool, tcpInPacket.PacketCmdID, bytesData, tcpInPacket.PacketDataSize - 1 - 4, out tcpOutPacket);
                        }
                        else
                        {
                            result = TCPCmdHandler.ProcessCmd(this, tcpInPacket.CurrentSocket, tcpClientPool, tcpRandKey, tcpOutPacketPool, tcpInPacket.PacketCmdID, bytesData, tcpInPacket.PacketDataSize - 1 - 4, out tcpOutPacket);
                        }

                        long processTime = (TimeUtil.NowEx() - processBeginTime);
                        if (GameManager.StatisticsMode > 0 || processTime > 50 || result == TCPProcessCmdResults.RESULT_FAILED)
                        {
                            RecordCmdDetail(tcpInPacket.PacketCmdID, processTime, tcpInPacket.PacketDataSize, result);
                        }

                        if (result == TCPProcessCmdResults.RESULT_DATA && null != tcpOutPacket)
                        {
                            // Console.WriteLine("===>>> tcpOutPacket==== " + tcpOutPacket.PacketCmdID.ToString());
                            //向登陆客户端返回数据
                            socketListener.SendData(tcpInPacket.CurrentSocket, tcpOutPacket);
                        }
                        else if (result == TCPProcessCmdResults.RESULT_FAILED)//解析失败, 直接关闭连接
                        {
                            if (tcpInPacket.PacketCmdID != (int)TCPGameServerCmds.CMD_LOG_OUT)
                            {
                                SysConOut.WriteLine(string.Format("解析并执行命令失败: {0},{1}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket)));
                                LogManager.WriteLog(LogTypes.Error, string.Format("解析并执行命令失败: {0},{1}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket)));
                            }
                            return(false);
                        }
                    }
//#endif
                }
                else
                {
                    var    _s  = tcpInPacket.CurrentSocket;
                    string uid = _s != null?GameManager.OnlineUserSession.FindUserID(_s) : "socket is nil";

                    SysConOut.WriteLine(string.Format("校验客户端发送的指令数据完整性失败: {0},{1}, 错误码:{2}, uid={3}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), checkErrorCode, uid));

                    LogManager.WriteLog(LogTypes.Error, string.Format("校验客户端发送的指令数据完整性失败: {0},{1}, 错误码:{2}, uid={3}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), checkErrorCode, uid));
                    return(false);
                }
            }
            else if (1 == doType) //正常的登录指令包//策略验证请求
            {
                //直接发送策略数据
                DirectSendPolicyFileData(tcpInPacket);
            }
            else
            {
                SysConOut.WriteLine(string.Format("解析并执行命令时类型未知: {0},{1}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket)));
                LogManager.WriteLog(LogTypes.Error, string.Format("解析并执行命令时类型未知: {0},{1}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket)));
                //socketListener.CloseSocket(tcpInPacket.CurrentSocket);
                return(false);
            }

            return(true);
        }
Beispiel #5
0
        /// <summary>
        /// 命令包接收完毕后的回调事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private bool TCPCmdPacketEvent(object sender, int doType)
        {
            TCPInPacket tcpInPacket = sender as TCPInPacket;

            if (0 == doType) //正常的登录指令包
            {
                int thisTimeCheckTicks = 0;
                int checkErrorCode     = 0;

                //这里实际上是有一个指令流的拷贝,占用的很零碎的内存,这个是否考虑使用内存池????
                byte[] bytesData = CheckClientDataValid(tcpInPacket.PacketCmdID, tcpInPacket.GetPacketBytes(), tcpInPacket.PacketDataSize, tcpInPacket.LastCheckTicks, out thisTimeCheckTicks, out checkErrorCode);

                if (null != bytesData)
                {
                    tcpInPacket.LastCheckTicks = thisTimeCheckTicks; //记忆此次的心跳数据
//#if false
                    if (UseWorkerPool)
                    {
                        //杰隆的异步处理指令入口代码
                        TCPCmdWrapper wrapper = TCPCmdWrapper.Get(this, tcpInPacket.CurrentSocket, tcpClientPool, tcpRandKey, tcpOutPacketPool, tcpInPacket.PacketCmdID, bytesData, tcpInPacket.PacketDataSize - 1 - 4);

                        TCPSession session = null;
                        if (GameManager.FlagOptimizeLock3)
                        {
                            if (null != tcpInPacket.CurrentSocket)
                            {
                                session = tcpInPacket.CurrentSocket.session;
                            }
                            if (null == session)
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("未与客户端建立会话: {0},{1}, 错误码:{2}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), checkErrorCode));
                                return(false);
                            }
                        }
                        else
                        {
                            lock (tcpSessions)
                            {
                                if (!tcpSessions.TryGetValue(tcpInPacket.CurrentSocket, out session))
                                {
                                    LogManager.WriteLog(LogTypes.Error, string.Format("未与客户端建立会话: {0},{1}, 错误码:{2}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), checkErrorCode));
                                    return(false);
                                }
                            }
                        }

                        int posCmdNum = 0;
                        session.addTCPCmdWrapper(wrapper, out posCmdNum);
                        if (posCmdNum > 0)
                        {
                            int banSpeedUpMinutes = GameManager.GameConfigMgr.GetGameConfigItemInt("ban-speed-up-minutes2", 10); //加速禁止登陆的时间

                            GameClient client = GameManager.ClientMgr.FindClient(tcpInPacket.CurrentSocket);
                            if (null != client)
                            {
                                GameManager.ClientMgr.NotifyImportantMsg(this.MySocketListener, tcpOutPacketPool, client, StringUtil.substitute(Global.GetLang("本游戏禁止使用加速软件,{0}分钟内将禁止登陆!"), banSpeedUpMinutes), GameInfoTypeIndexes.Error, ShowGameInfoTypes.HintAndBox);
                                BanManager.BanRoleName(Global.FormatRoleName(client, client.ClientData.RoleName), banSpeedUpMinutes);
                            }

                            LogManager.WriteLog(LogTypes.Error, string.Format("通过POSITION指令判断客户端加速: {0}, 指令个数:{1}, 断开连接", Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), posCmdNum));
                            return(false);
                        }

                        taskExecutor.execute(new ProcessSessionTask(session));
                    }
//#else
                    else
                    {
                        TCPSession session = null;
                        if (GameManager.FlagOptimizeLock3)
                        {
                            if (null != tcpInPacket.CurrentSocket)
                            {
                                session = tcpInPacket.CurrentSocket.session;
                            }
                            if (null == session)
                            {
                                LogManager.WriteLog(LogTypes.Error, string.Format("未与客户端建立会话: {0},{1}, 错误码:{2}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), checkErrorCode));
                                return(false);
                            }
                        }
                        else
                        {
                            lock (tcpSessions)
                            {
                                if (!tcpSessions.TryGetValue(tcpInPacket.CurrentSocket, out session))
                                {
                                    LogManager.WriteLog(LogTypes.Error, string.Format("未与客户端建立会话: {0},{1}, 错误码:{2}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), checkErrorCode));
                                    return(false);
                                }
                            }
                        }

                        int posCmdNum = 0;
                        session.CheckCmdNum(tcpInPacket.PacketCmdID, out posCmdNum);
                        if (posCmdNum > 0)
                        {
                            int banSpeedUpMinutes = GameManager.GameConfigMgr.GetGameConfigItemInt("ban-speed-up-minutes2", 10); //加速禁止登陆的时间

                            GameClient client = GameManager.ClientMgr.FindClient(tcpInPacket.CurrentSocket);
                            if (null != client)
                            {
                                GameManager.ClientMgr.NotifyImportantMsg(this.MySocketListener, tcpOutPacketPool, client, StringUtil.substitute(Global.GetLang("本游戏禁止使用加速软件,{0}分钟内将禁止登陆!"), banSpeedUpMinutes), GameInfoTypeIndexes.Error, ShowGameInfoTypes.HintAndBox);
                                BanManager.BanRoleName(Global.FormatRoleName(client, client.ClientData.RoleName), banSpeedUpMinutes);
                            }

                            LogManager.WriteLog(LogTypes.Error, string.Format("通过POSITION指令判断客户端加速: {0}, 指令个数:{1}, 断开连接", Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), posCmdNum));
                            return(false);
                        }

                        TCPOutPacket         tcpOutPacket     = null;
                        long                 processBeginTime = TimeUtil.NowEx();
                        TCPProcessCmdResults result           = TCPCmdHandler.ProcessCmd(this, tcpInPacket.CurrentSocket, tcpClientPool, tcpRandKey, tcpOutPacketPool, tcpInPacket.PacketCmdID, bytesData, tcpInPacket.PacketDataSize - 1 - 4, out tcpOutPacket);

                        long processTime = (TimeUtil.NowEx() - processBeginTime);
                        if (GameManager.StatisticsMode > 0 || processTime > 50 || result == TCPProcessCmdResults.RESULT_FAILED)
                        {
                            lock (ProcessSessionTask.cmdMoniter) //锁定命令监控对象
                            {
                                int cmdID = tcpInPacket.PacketCmdID;
                                PorcessCmdMoniter moniter = null;
                                if (!ProcessSessionTask.cmdMoniter.TryGetValue(cmdID, out moniter))
                                {
                                    moniter = new PorcessCmdMoniter(cmdID, processTime);
                                    ProcessSessionTask.cmdMoniter.Add(cmdID, moniter);
                                }

                                moniter.onProcessNoWait(processTime, result);
                            }
                        }

                        if (result == TCPProcessCmdResults.RESULT_DATA && null != tcpOutPacket)
                        {
                            //向登陆客户端返回数据
                            socketListener.SendData(tcpInPacket.CurrentSocket, tcpOutPacket);
                        }
                        else if (result == TCPProcessCmdResults.RESULT_FAILED)//解析失败, 直接关闭连接
                        {
                            LogManager.WriteLog(LogTypes.Error, string.Format("解析并执行命令失败: {0},{1}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket)));
                            return(false);
                        }
                    }
//#endif
                }
                else
                {
                    LogManager.WriteLog(LogTypes.Error, string.Format("校验客户端发送的指令数据完整性失败: {0},{1}, 错误码:{2}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket), checkErrorCode));
                    return(false);
                }
            }
            else if (1 == doType) //正常的登录指令包//策略验证请求
            {
                //直接发送策略数据
                DirectSendPolicyFileData(tcpInPacket);
            }
            else
            {
                LogManager.WriteLog(LogTypes.Error, string.Format("解析并执行命令时类型未知: {0},{1}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket)));
                //socketListener.CloseSocket(tcpInPacket.CurrentSocket);
                return(false);
            }

            return(true);
        }