Exemplo n.º 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();
        }
    }