示例#1
0
        /// <summary>
        /// 在多个线程内部循环调用
        /// </summary>
        public void ProcessCmdPackets(Queue <CmdPacket> ls)
        {
            //dictInPackets.Values值的个数可能会变化,每次处理先保存好最大个数
            //同时给出20个冗余数量
            int maxCount = dictInPackets.Values.Count + 20;

            for (int n = 0; n < maxCount; n++)
            {
                TCPInPacket tcpInPacket = GetNextTcpInPacket(n);

                //这意味着列表数据包变少了
                if (null == tcpInPacket)
                {
                    break;
                }

                ls.Clear();

                if (tcpInPacket.PopCmdPackets(ls)) //判断是否取到了指令列表
                {
                    try
                    {
                        while (ls.Count > 0)
                        {
                            CmdPacket cmdPacket = ls.Dequeue();
                            //接收到了完整的命令包
                            TCPOutPacket         tcpOutPacket = null;
                            TCPProcessCmdResults result       = TCPProcessCmdResults.RESULT_FAILED;

                            result = TCPCmdHandler.ProcessCmd(this, tcpInPacket.CurrentSocket, tcpClientPool, tcpRandKey, tcpOutPacketPool, cmdPacket.CmdID, cmdPacket.Data, cmdPacket.Data.Length, out tcpOutPacket);

                            if (result == TCPProcessCmdResults.RESULT_DATA && null != tcpOutPacket)
                            {
                                //向登陆客户端返回数据
                                socketListener.SendData(tcpInPacket.CurrentSocket, tcpOutPacket);
                            }
                            else if (result == TCPProcessCmdResults.RESULT_FAILED)//解析失败, 直接关闭连接
                            {
                                if (cmdPacket.CmdID != (int)TCPGameServerCmds.CMD_LOG_OUT)
                                {
                                    LogManager.WriteLog(LogTypes.Error, string.Format("解析并执行命令失败: {0},{1}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket)));
                                }
                                SysConOut.WriteLine("========================ProcessCmdPackets CloseSocket================");
                                // Console.WriteLine("========================ProcessCmdPackets CloseSocket================");
                                //这儿需要关闭链接--->这样关闭对吗?
                                socketListener.CloseSocket(tcpInPacket.CurrentSocket);

                                break;
                            }
                        }
                    }
                    finally
                    {
                        //处理结束之后设置相关标志位
                        tcpInPacket.OnThreadDealingComplete();
                    }
                }
            }
        }
示例#2
0
        public void ProcessCmdPackets(Queue <CmdPacket> ls)
        {
            int maxCount = this.dictInPackets.Values.Count + 20;

            for (int i = 0; i < maxCount; i++)
            {
                TCPInPacket tcpInPacket = this.GetNextTcpInPacket(i);
                if (null == tcpInPacket)
                {
                    break;
                }
                ls.Clear();
                if (tcpInPacket.PopCmdPackets(ls))
                {
                    try
                    {
                        while (ls.Count > 0)
                        {
                            CmdPacket            cmdPacket    = ls.Dequeue();
                            TCPOutPacket         tcpOutPacket = null;
                            TCPProcessCmdResults result       = TCPCmdHandler.ProcessCmd(this, tcpInPacket.CurrentSocket, this.tcpClientPool, this.tcpRandKey, this.tcpOutPacketPool, cmdPacket.CmdID, cmdPacket.Data, cmdPacket.Data.Length, out tcpOutPacket);
                            if (result == TCPProcessCmdResults.RESULT_DATA && null != tcpOutPacket)
                            {
                                this.socketListener.SendData(tcpInPacket.CurrentSocket, tcpOutPacket, true);
                            }
                            else
                            {
                                if (result == TCPProcessCmdResults.RESULT_FAILED)
                                {
                                    if (cmdPacket.CmdID != 22)
                                    {
                                        LogManager.WriteLog(LogTypes.Error, string.Format("解析并执行命令失败: {0},{1}, 关闭连接", (TCPGameServerCmds)tcpInPacket.PacketCmdID, Global.GetSocketRemoteEndPoint(tcpInPacket.CurrentSocket, false)), null, true);
                                    }
                                    this.socketListener.CloseSocket(tcpInPacket.CurrentSocket, "");
                                    break;
                                }
                                if (result == TCPProcessCmdResults.RESULT_DATA_CLOSE && null != tcpOutPacket)
                                {
                                    this.socketListener.SendData(tcpInPacket.CurrentSocket, tcpOutPacket, true);
                                    tcpInPacket.CurrentSocket.DelayClose = 250;
                                }
                            }
                        }
                    }
                    finally
                    {
                        tcpInPacket.OnThreadDealingComplete();
                    }
                }
            }
        }