示例#1
0
    //User buffer to push data
    void ProcessPacket()
    {
        if (m_SocketInputStream == null)
        {
            return;
        }

        int nProcessPacketCount = m_nEachFrame_ProcessPacket_Count;

        Int32   packetSize;
        Int16   messageid;
        Ipacket pPacket = null;

        while (m_bCanProcessPacket && (nProcessPacketCount--) > 0)
        {
            Array.Clear(m_HeadbyteData, 0, PACKET_HEADER_SIZE);
            if (!m_SocketInputStream.Peek(m_HeadbyteData, PACKET_HEADER_SIZE))
            {
                break;
            }

            packetSize = BitConverter.ToInt32(m_HeadbyteData, 0);
            packetSize = System.Net.IPAddress.NetworkToHostOrder(packetSize);

            messageid = BitConverter.ToInt16(m_HeadbyteData, sizeof(UInt32));
            messageid = System.Net.IPAddress.NetworkToHostOrder(messageid);

            if (packetSize <= 0 || messageid <= 0)
            {
                LogModule.ErrorLog("ProcessPacket packetSize: " + packetSize + "  messageid : " + messageid + " HeadDate: " + LogModule.ByteToString(m_HeadbyteData, 0, PACKET_HEADER_SIZE));
            }

            if (m_SocketInputStream.Length() < packetSize)
            {
                break;
            }

            try
            {
                if (m_MaxRevOnePacketbyteCount < packetSize)
                {
                    m_MaxRevOnePacketbyte      = new byte[packetSize];
                    m_MaxRevOnePacketbyteCount = packetSize;
                }
                Array.Clear(m_MaxRevOnePacketbyte, 0, m_MaxRevOnePacketbyteCount);

                bool bRet = m_SocketInputStream.Skip(PACKET_HEADER_SIZE);
                if (bRet == false)
                {
                    string errorLog = string.Format("Can not Create Packet MessageID({0},packetSize{1})", messageid, packetSize);
                    throw PacketException.PacketReadError(errorLog);
                }
                m_SocketInputStream.Read(m_MaxRevOnePacketbyte, (uint)(packetSize - PACKET_HEADER_SIZE));
                if (IsCryptoPacket((UInt16)messageid))
                {
                    XorCrypto.XorDecrypt(m_MaxRevOnePacketbyte, (uint)(packetSize - PACKET_HEADER_SIZE));
                }

                pPacket = m_PacketFactoryManager.GetPacketHandler((MessageID)messageid);
                if (pPacket == null)
                {
                    string errorLog = string.Format("Can not Create Packet MessageID({0},buff{1})", messageid, LogModule.ByteToString(m_MaxRevOnePacketbyte, 0, m_MaxRevOnePacketbyteCount));
                    throw PacketException.PacketCreateError(errorLog);
                }

                PacketDistributed realPacket = PacketDistributed.CreatePacket((MessageID)messageid);

                if (realPacket == null)
                {
                    string errorLog = string.Format("Can not Create Inner Packet Data MessageID({0},buff{1})", messageid, LogModule.ByteToString(m_MaxRevOnePacketbyte, 0, m_MaxRevOnePacketbyteCount));
                    throw PacketException.PacketCreateError(errorLog);
                }
                PacketDistributed instancePacket = realPacket.ParseFrom(m_MaxRevOnePacketbyte, packetSize - PACKET_HEADER_SIZE);
                if (instancePacket == null)
                {
                    string errorLog = string.Format("Can not Merged Inner Packet Data MessageID({0},buff{1})", messageid, LogModule.ByteToString(m_MaxRevOnePacketbyte, 0, m_MaxRevOnePacketbyteCount));
                    throw PacketException.PacketCreateError(errorLog);
                }

                //Profiler.BeginSample((MessageID)messageid+"");
                uint result = pPacket.Execute(instancePacket);
                if (pPacket.GetType().Name != "GC_RET_PINGHandler" &&
                    pPacket.GetType().Name != "GC_CONNECTED_HEARTBEATHandler" &&
                    pPacket.GetType().Name != "GC_MOVEHandler")
                {
                    LogModule.WarningLog("Packet :" + pPacket.GetType().Name);
                }
                //Profiler.EndSample();

                if ((PACKET_EXE)result != PACKET_EXE.PACKET_EXE_NOTREMOVE)
                {
                    m_PacketFactoryManager.RemovePacket(pPacket);
                }
                else if ((PACKET_EXE)result == PACKET_EXE.PACKET_EXE_ERROR)
                {
                    string errorLog = string.Format("Execute Packet error!!! MessageID({0},buff{1})", messageid, LogModule.ByteToString(m_MaxRevOnePacketbyte, 0, m_MaxRevOnePacketbyteCount));
                    throw PacketException.PacketExecuteError(errorLog);
                }
            }
            catch (PacketException ex)
            {
                LogModule.ErrorLog(ex.ToString());
            }
            catch (System.Exception ex)
            {
                LogModule.ErrorLog(ex.ToString());
            }
        }

        if (nProcessPacketCount >= 0)
        {
            m_nEachFrame_ProcessPacket_Count = EACHFRAME_PROCESSPACKET_COUNT;
        }
        else
        {
            m_nEachFrame_ProcessPacket_Count += 4;
        }
    }