예제 #1
0
        /// <summary>
        /// Decode process.
        /// </summary>
        /// <returns></returns>
        public byte[] Decode()
        {
            byte[] res = new byte[_message.Length];

            foreach (var item in ReadByChunk().Select((chunk, i) => new { i, chunk }))
            {
                Array.Copy(_cipher.Decode(_message, _subKeys), 0, res, item.i, item.chunk.Length);
            }

            return(res);
        }
예제 #2
0
        /// <summary>
        /// Decode process.
        /// </summary>
        /// <returns></returns>
        public byte[] Decode()
        {
            var res = new byte[_message.Length];

            var i = -1;

            foreach (var item in ReadByChunk())
            {
                Array.Copy(_cipher.Decode(item, _subKeys), 0, res, i + 1, item.Length);

                i += item.Length;
            }

            return(res);
        }
예제 #3
0
        /// <summary>
        /// Called when the client has received data.
        /// </summary>
        /// <param name="numBytes">number of bytes received in _pBuf</param>
        protected void OnReceive(byte[] buffer, int numBytes)
        {
            lock (this)
            {
                //End Offset of buffer
                int bufferSize = m_pBufOffset + numBytes;

                //Size < minimum
                if (bufferSize < PacketIn.HDR_SIZE)
                {
                    m_pBufOffset = bufferSize; // undo buffer read
                    return;
                }

                //Reset the offset
                m_pBufOffset = 0;

                //Current offset into the buffer
                int curOffset = 0;

                do
                {
                    byte[] hdr = m_decoding.Peek(buffer, curOffset);

                    int packetLength = Marshal.ConvertToInt32(hdr, 0);
                    int dataLeft     = bufferSize - curOffset;

                    if (dataLeft < packetLength)
                    {
                        Buffer.BlockCopy(buffer, curOffset, buffer, 0, dataLeft);
                        m_pBufOffset = dataLeft;
                        break;
                    }

                    int packetEnd = curOffset + packetLength;


                    byte[] d = m_decoding.Decode(buffer, curOffset, packetLength);

                    //Console.WriteLine( Marshal.ToHexDump(
                    //string.Format("{0} <=== <{3}> Packet 0x{1:X2} ({2}) length: {4}", m_config.PacketInTxt, Marshal.ConvertToInt16(d, 4),
                    //Marshal.ConvertToInt16(d, 4), TcpEndpoint, Marshal.ConvertToInt32(d, 0),
                    //d.Length),
                    //d));


                    long start = Environment.TickCount;
                    try
                    {
                        PacketIn packet = m_networkHandler.ProcessPacket(this, d, 0, packetLength);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("ProcessPacket error: {0}", e.ToString());
                    }
                    long timeUsed = Environment.TickCount - start;

                    curOffset += packetLength;
                } while (bufferSize - 1 > curOffset);

                if (bufferSize - 1 == curOffset)
                {
                    buffer[0]    = buffer[curOffset];
                    m_pBufOffset = 1;
                }
            }
        }