public void simple_packet_block()
        {
            var simplePacketBlock = new SimplePacketBlock
            {
                Bytes = new Byte[] {
                    0x00, 0x00, 0x00, 0x03,
                    0x00, 0x00, 0x00, 0x14,
                    0x00, 0x00, 0x00, 0x80,
                    0x12, 0x34, 0x56, 0x78,
                    0x00, 0x00, 0x00, 0x14
                }
            };

            simplePacketBlock.Should().BeAssignableTo <IPacket>();
            simplePacketBlock.IsPacket.Should().Be(true);
            simplePacketBlock.OriginalLength.Should().Be(0x80);
            simplePacketBlock.Payload.ToArray().Should().Equal(0x12, 0x34, 0x56, 0x78);
        }
Example #2
0
        public void ReadPackets(System.Threading.CancellationToken cancellationToken)
        {
            AbstractBlock block;
            long          prevPosition = 0;

            while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length && !cancellationToken.IsCancellationRequested)
            {
                try
                {
                    lock (syncRoot)
                    {
                        prevPosition = binaryReader.BaseStream.Position;
                        block        = AbstractBlockFactory.ReadNextBlock(binaryReader, this.ReverseByteOrder, OnException);
                    }

                    if (block == null)
                    {
                        throw new Exception(string.Format("[ReadPackets] AbstractBlockFactory cannot read packet on position {0}", prevPosition));
                    }

                    switch (block.BlockType)
                    {
                    case BaseBlock.Types.EnhancedPacket:
                    {
                        EnchantedPacketBlock enchantedBlock = block as EnchantedPacketBlock;
                        if (enchantedBlock == null)
                        {
                            throw new Exception(string.Format("[ReadPackets] system cannot cast block to EnchantedPacketBlock. Block start on position: {0}.", prevPosition));
                        }
                        else
                        {
                            OnReadPacket(enchantedBlock);
                        }
                    }
                    break;

                    case BaseBlock.Types.Packet:
                    {
                        PacketBlock packetBlock = block as PacketBlock;
                        if (packetBlock == null)
                        {
                            throw new Exception(string.Format("[ReadPackets] system cannot cast block to PacketBlock. Block start on position: {0}.", prevPosition));
                        }
                        else
                        {
                            OnReadPacket(packetBlock);
                        }
                    }
                    break;

                    case BaseBlock.Types.SimplePacket:
                    {
                        SimplePacketBlock simpleBlock = block as SimplePacketBlock;
                        if (simpleBlock == null)
                        {
                            throw new Exception(string.Format("[ReadPackets] system cannot cast block to SimplePacketBlock. Block start on position: {0}.", prevPosition));
                        }
                        else
                        {
                            OnReadPacket(simpleBlock);
                        }
                    }
                    break;

                    default:
                        break;
                    }
                }
                catch (Exception exc)
                {
                    OnException(exc);
                    lock (syncRoot)
                    {
                        if (prevPosition == binaryReader.BaseStream.Position)
                        {
                            break;
                        }
                    }
                    continue;
                }
            }
        }