Example #1
0
        public void WriteRawFloat_CodedOutputStream()
        {
            var cos = new CodedOutputStream(outputBuffer);

            foreach (var value in floatValues)
            {
                cos.WriteFloat(value);
            }
            cos.Flush();
            cos.CheckNoSpaceLeft();
        }
Example #2
0
        public void WriteRawDouble_CodedOutputStream()
        {
            var cos = new CodedOutputStream(outputBuffer);

            foreach (var value in doubleValues)
            {
                cos.WriteDouble(value);
            }
            cos.Flush();
            cos.CheckNoSpaceLeft();
        }
Example #3
0
        public void WriteBytes_CodedOutputStream(int encodedSize)
        {
            var values = byteStringValues[encodedSize];
            var cos    = new CodedOutputStream(outputBuffer);

            foreach (var value in values)
            {
                cos.WriteBytes(value);
            }
            cos.Flush();
            cos.CheckNoSpaceLeft();
        }
Example #4
0
        public void WriteFixed64_CodedOutputStream()
        {
            const int encodedSize = sizeof(ulong);
            var       cos         = new CodedOutputStream(outputBuffer);

            for (int i = 0; i < BytesToWrite / encodedSize; i++)
            {
                cos.WriteFixed64(123456789);
            }
            cos.Flush();
            cos.CheckNoSpaceLeft();
        }
Example #5
0
        public void WriteRawVarint64_CodedOutputStream(int encodedSize)
        {
            var values = varint64Values[encodedSize];
            var cos    = new CodedOutputStream(outputBuffer);

            for (int i = 0; i < values.Length; i++)
            {
                cos.WriteRawVarint64(values[i]);
            }
            cos.Flush();
            cos.CheckNoSpaceLeft();
        }
        public void WriteRawVarint64_CodedOutputStream(int encodedSize)
        {
            var values = varint64Values[encodedSize];
            var cos    = new CodedOutputStream(outputBuffer);

            foreach (var value in values)
            {
                cos.WriteRawVarint64(value);
            }
            cos.Flush();
            cos.CheckNoSpaceLeft();
        }
Example #7
0
    public byte[] ToByteArray()
    {
        //Check must init
        if (!IsInitialized())
        {
            throw InvalidProtocolBufferException.ErrorMsg("Request data have not set");
        }
        byte[]            data   = new byte[SerializedSize()];
        CodedOutputStream output = CodedOutputStream.CreateInstance(data);

        WriteTo(output);
        output.CheckNoSpaceLeft();
        return(data);
    }
Example #8
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();
        }
    }