public CPacket CreatePacket(byte Packet, byte[] arr_bArguments)
        {
            try
            {
                //Split Buffer to 128-byte long Blocks
                List <byte[]> lstBlockBuffers = CUtilities.SplitBytes(arr_bArguments, 512);

                List <CPacketBlock> lstBlocks = new List <CPacketBlock>();

                //Create Blocks
                foreach (Byte[] arr_bBuffer in lstBlockBuffers.ToArray())
                {
                    CPacketBlock Block = new CPacketBlock();
                    Block.Block_ID     = lstBlocks.Count + 1;
                    Block.Block_Buffer = arr_bBuffer;

                    lstBlocks.Add(Block);
                }

                //Create Packet Header
                CPacketHeader Header = CPacketHeader.CreateHeader(Packet, lstBlocks.Count);

                //Create Packet
                CPacket SendPacket = new CPacket(Header);
                SendPacket.Data_Blocks = lstBlocks;

                return(SendPacket);
            }
            catch { }
            return(null);
        }
        CPacketHeader GetHeaderFromBuffer(Byte[] arr_bBuffer)
        {
            try
            {
                string strBuffer = UnicodeEncoding.Unicode.GetString(arr_bBuffer, 2, arr_bBuffer.Length - 2);

                if (String.IsNullOrEmpty(strBuffer))
                {
                    return(null);
                }

                string[] arr_strData = strBuffer.Split('|');

                if (arr_strData.Length != Header_Length)
                {
                    return(null);
                }

                CPacketHeader PacketHeader = new CPacketHeader();
                PacketHeader.Packet_Ident = arr_strData[0];
                PacketHeader.Packet       = arr_bBuffer[1];

                if (!int.TryParse(arr_strData[1], out PacketHeader.Block_Amount))
                {
                    return(null);
                }

                return(PacketHeader);
            }
            catch { }
            return(null);
        }
        void ProcessHeader(Byte[] arr_bBuffer)
        {
            CPacketHeader Header = this.GetHeaderFromBuffer(arr_bBuffer);

            if (Header == null)
            {
                return;
            }

            CPacket Packet = new CPacket(Header);

            for (int i = 0; i < Packet.PacketHeader.Block_Amount; i++)
            {
                CPacketBlock Block = new CPacketBlock();
                Block.Block_ID = Packet.Data_Blocks.Count + 1;

                Packet.Data_Blocks.Add(Block);
            }

            if (!Packet.IsFinished())
            {
                lock (m_lstQueueRecvPacket)
                {
                    m_lstQueueRecvPacket.Add(Packet);
                }
            }
            else
            {
                if (this.OnPacketReceived != null)
                {
                    this.OnPacketReceived.Invoke(Packet.GetPacketFromHeader(), null);
                }
            }
        }
Example #4
0
        public CPacket(CPacketHeader Header)
        {
            this.PacketHeader = Header;

            if (Header.Block_Amount > 0)
            {
                Data_Blocks = new List <CPacketBlock>();
            }
        }
Example #5
0
 public static CPacketHeader CreateHeader(Byte Packet, Int32 iBlockAmount)
 {
     try
     {
         CPacketHeader Header = new CPacketHeader();
         Header.Packet_Ident = CUtilities.RandomString(8);
         Header.Packet       = Packet;
         Header.Block_Amount = iBlockAmount;
         return(Header);
     }
     catch { }
     return(null);
 }