public CPacket CreatePacket(Byte Packet, Byte[] arr_bArguments) { try { //Split Buffer to 128-byte long Blocks List <Byte[]> lstBlockBuffers = CUtils.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; string strBuffer = UnicodeEncoding.Unicode.GetString(arr_bBuffer); 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); }
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); } } }
void ProcessBlock(Byte[] arr_bBuffer) { String strPacketIdent; Int32 iBlockID; Byte[] arr_bBlockBuffer; if (!this.GetBlockDataFromBuffer(arr_bBuffer, out strPacketIdent, out iBlockID, out arr_bBlockBuffer)) { return; } lock (m_lstQueueRecvPacket) { //Loop Through all Packets for (int iPacketIndex = 0; iPacketIndex < m_lstQueueRecvPacket.Count; iPacketIndex++) { CPacket Packet = m_lstQueueRecvPacket[iPacketIndex]; if (Packet.PacketHeader.Packet_Ident != strPacketIdent) { continue; } lock (Packet.Data_Blocks) { //Loop through all Blocks for (int iBlockIndex = 0; iBlockIndex < Packet.Data_Blocks.Count; iBlockIndex++) { CPacketBlock Block = Packet.Data_Blocks[iBlockIndex]; if (Block.IsFinished || Block.Block_ID != iBlockID) { continue; } //Replace Block with Received Buffer Block.Block_Buffer = arr_bBlockBuffer; //Block is done Block.IsFinished = true; //Replace Packet Block Packet.Data_Blocks[iBlockIndex] = Block; //Replace Packet m_lstQueueRecvPacket[iPacketIndex] = Packet; } } if (Packet.IsFinished()) { if (this.OnPacketReceived != null) { this.OnPacketReceived.Invoke(Packet.GetPacketFromHeader(), Packet.GetPacketBufferFromBlocks()); } m_lstQueueRecvPacket.Remove(Packet); } } } }