示例#1
0
    public void SendPacket(PacketDistributed pPacket)
    {
        if (pPacket == null)
        {
            return;
        }

        if (m_connectStatus != ConnectStatus.CONNECTED)
        {
            if (m_connectStatus == ConnectStatus.DISCONNECTED)
            {
                // 再次询问断线重连情况
                ConnectLost();
            }
            return;
        }

        if (m_Socket.IsValid)
        {
            if (!pPacket.IsInitialized())
            {
                throw InvalidProtocolBufferException.ErrorMsg("Request data have not set");
            }

            string name = pPacket.GetType().Name;
            if (name != "CG_PING" && name != "CG_CONNECTED_HEARTBEAT" && name != "CG_MOVE")
            {
                LogModule.WarningLog(name);
            }

            int nValidbyteSize = pPacket.SerializedSize();
            if (nValidbyteSize <= 0)
            {
                return;
            }

            //Array.Clear(m_SendbyteData, 0, (int)SocketOutputStream.DEFAULT_SOCKET_OUTPUT_BUFFER_SIZE); 不用全部清理,用多少清多少吧
            int nClearCount = nValidbyteSize + 128;
            if (nClearCount > (int)SocketOutputStream.DEFAULT_SOCKET_OUTPUT_BUFFER_SIZE)
            {
                nClearCount = (int)SocketOutputStream.DEFAULT_SOCKET_OUTPUT_BUFFER_SIZE;
            }
            Array.Clear(m_SendbyteData, 0, nClearCount);
            CodedOutputStream output = CodedOutputStream.CreateInstance(m_SendbyteData, 0, nValidbyteSize);
            pPacket.WriteTo(output);
            output.CheckNoSpaceLeft();

            Int32 nlen      = nValidbyteSize + NetWorkLogic.PACKET_HEADER_SIZE;
            Int32 netnlen   = System.Net.IPAddress.HostToNetworkOrder(nlen);
            Int16 messageid = System.Net.IPAddress.HostToNetworkOrder((Int16)pPacket.GetPacketID());

            Array.Clear(m_LenbyteData, 0, sizeof(Int32));
            Array.Clear(m_PacketIDbyteData, 0, sizeof(Int16));

            m_LenbyteData[0] = (byte)(netnlen);  //小端顺序放
            m_LenbyteData[1] = (byte)(netnlen >> 8);
            m_LenbyteData[2] = (byte)(netnlen >> 16);
            m_LenbyteData[3] = (byte)(netnlen >> 24);


            m_PacketIDbyteData[0] = (byte)(messageid);
            m_PacketIDbyteData[1] = (byte)(messageid >> 8);

            uint nSizeBefore = m_SocketOutputStream.Length();
            m_SocketOutputStream.Write(m_LenbyteData, sizeof(Int32));
            m_SocketOutputStream.Write(m_PacketIDbyteData, sizeof(Int16));
            if (IsCryptoPacket((UInt16)pPacket.GetPacketID()))
            {
                XorCrypto.XorEncrypt(m_SendbyteData, (uint)nValidbyteSize);
            }
            m_SocketOutputStream.Write(m_SendbyteData, (uint)nValidbyteSize);
            uint nSizeAfter = m_SocketOutputStream.Length();

            //发包统计
            if (nSizeAfter > nSizeBefore)
            {
                if (NetWorkLogic.s_nSendCount < 0)
                {
                    NetWorkLogic.s_nSendCount = 0;
                }
                NetWorkLogic.s_nSendCount += (int)(nSizeAfter - nSizeBefore);
            }
        }
        else
        {
            ConnectLost();
        }
    }
示例#2
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;
        }
    }