Example #1
0
        public static ArraySegment <byte> Encryption(NetPackage mPackage)
        {
            int command = mPackage.command;

            byte[] msg = mPackage.buffer;

            int buffer_Length = mPackage.buffer.Length;
            int sum_Length    = 4 + buffer_Length;

            mSendBuffer [0] = (byte)command;
            mSendBuffer [1] = (byte)(command >> 8);
            mSendBuffer [2] = (byte)(command >> 16);
            mSendBuffer [3] = (byte)(command >> 24);

            Array.Copy(msg, 0, mSendBuffer, 4, buffer_Length);
            byte[] data = mAES.Encryption(mSendBuffer, 0, sum_Length);
            Array.Copy(data, 0, mSendBuffer, 8, data.Length);

            Array.Copy(mCheck, mSendBuffer, 4);
            buffer_Length   = data.Length;
            mSendBuffer [4] = (byte)buffer_Length;
            mSendBuffer [5] = (byte)(buffer_Length >> 8);
            mSendBuffer [6] = (byte)(buffer_Length >> 16);
            mSendBuffer [7] = (byte)(buffer_Length >> 24);

            return(new ArraySegment <byte> (mSendBuffer, 0, data.Length + 8));
        }
Example #2
0
 public SocketPeer_SocketAsyncEventArgs()
 {
     mPackage              = new NetPackage();
     mDicWaitSendBuffer    = new ListBuffer <byte>();
     mBindReceieNetPackage = new DataBind <NetPackage> (new NetPackage());
     mBindReceieNetPackage.addDataBind(NetSystem.mEventSystem.DeSerialize);
     mParseStreamList = new CircularBuffer <byte> (ServerConfig.nMaxBufferSize * 2);
 }
Example #3
0
        public SocketPeer_Select()
        {
            mWaitSendBuffer = new QueueArraySegment <byte> (64, ServerConfig.nMaxBufferSize);
            mNetPackage     = new NetPackage();

            mParseStreamList       = new CircularBuffer <byte> (2 * ServerConfig.nMaxBufferSize);
            mBindReceiveNetPackage = new DataBind <NetPackage> (new NetPackage());

            mBindReceiveNetPackage.addDataBind(NetSystem_Select.mEventSystem.DeSerialize);
        }
Example #4
0
        public static bool DeEncryption(CircularBuffer <byte> data, NetPackage mPackage)
        {
            if (data.Length <= 8)
            {
                return(false);
            }

            for (int i = 0; i < 4; i++)
            {
                if (data [i] != mCheck [i])
                {
                    return(false);
                }
            }

            int nBodyLength1 = data [4] | data [5] << 8 | data [6] << 16 | data [7] << 24;

            if (nBodyLength1 <= 0 || nBodyLength1 + 8 > data.Length)
            {
                return(false);
            }

            if (nBodyLength1 > mReceiveBuffer.Length)
            {
                mReceiveBuffer = new byte[nBodyLength1];
            }

            data.CopyTo(8, mReceiveBuffer, 0, nBodyLength1);
            data.ClearBuffer(nBodyLength1 + 8);

            byte[] msg = mAES.Decryption(mReceiveBuffer, 0, nBodyLength1);
            if (msg.Length < 4)
            {
                DebugSystem.LogError("解包失败");
                return(false);
            }

            int command      = msg [0] | msg [1] << 8 | msg [2] << 16 | msg [3] << 24;
            int nBodyLength2 = msg.Length - 4;

            byte[] buffer = new byte[nBodyLength2];
            Array.Copy(msg, 4, buffer, 0, nBodyLength2);

            mPackage.command = command;
            mPackage.buffer  = buffer;
            return(true);
        }