public static EnchantedPacketBlock Parse(BaseBlock baseBlock, Action <Exception> ActionOnException)
        {
            CustomContract.Requires <ArgumentNullException>(baseBlock != null, "BaseBlock cannot be null");
            CustomContract.Requires <ArgumentNullException>(baseBlock.Body != null, "BaseBlock.Body cannot be null");
            CustomContract.Requires <ArgumentException>(baseBlock.BlockType == BaseBlock.Types.EnhancedPacket, "Invalid packet type");

            long positionInStream = baseBlock.PositionInStream;

            using (Stream stream = new MemoryStream(baseBlock.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    int             interfaceID     = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    byte[]          timestamp       = binaryReader.ReadBytes(8);
                    TimestampHelper timestampHelper = new TimestampHelper(timestamp, baseBlock.ReverseByteOrder);
                    int             capturedLength  = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    int             packetLength    = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    byte []         data            = binaryReader.ReadBytes(capturedLength);
                    if (data.Length < capturedLength)
                    {
                        throw new EndOfStreamException("Unable to read beyond the end of the stream");
                    }
                    int remainderLength = (int)capturedLength % BaseBlock.AlignmentBoundary;
                    if (remainderLength > 0)
                    {
                        int paddingLength = BaseBlock.AlignmentBoundary - remainderLength;
                        binaryReader.ReadBytes(paddingLength);
                    }
                    EnchantedPacketOption option         = EnchantedPacketOption.Parse(binaryReader, baseBlock.ReverseByteOrder, ActionOnException);
                    EnchantedPacketBlock  enchantedBlock = new EnchantedPacketBlock(interfaceID, timestampHelper, packetLength, data, option, positionInStream);
                    return(enchantedBlock);
                }
            }
        }
        public static EnchantedPacketBlock CreateEnchantedPacketFromIPacket(IPacket packet, Action <Exception> ActionOnException)
        {
            CustomContract.Requires <ArgumentNullException>(packet != null, "packet cannot be null");
            CustomContract.Requires <ArgumentNullException>(packet.Data != null, "packet.Data cannot be null");
            TimestampHelper timestampHelper = new TimestampHelper(packet.Seconds, packet.Microseconds);

            EnchantedPacketBlock enchantedBlock = new EnchantedPacketBlock(0, timestampHelper, packet.Data.Length, packet.Data, new EnchantedPacketOption(), 0);

            return(enchantedBlock);
        }
        public static AbstractBlock ReadNextBlock(BinaryReader binaryReader, bool bytesReorder, Action <Exception> ActionOnException)
        {
            CustomContract.Requires <ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");
            try
            {
                BaseBlock     baseblock = new BaseBlock(binaryReader, bytesReorder);
                AbstractBlock block     = null;;
                switch (baseblock.BlockType)
                {
                case BaseBlock.Types.SectionHeader:
                    block = SectionHeaderBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.InterfaceDescription:
                    block = InterfaceDescriptionBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.Packet:
                    block = PacketBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.SimplePacket:
                    block = SimplePacketBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.NameResolution:
                    block = NameResolutionBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.InterfaceStatistics:
                    block = InterfaceStatisticsBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.EnhancedPacket:
                    block = EnchantedPacketBlock.Parse(baseblock, ActionOnException);
                    break;

                default:
                    break;
                }
                return(block);
            }
            catch (Exception exc)
            {
                ActionOnException(exc);
                return(null);
            }
        }
Beispiel #4
0
 public static void AbstractBlockFactory_ConvertTo_EnchantedPacketBlock_Test()
 {
     byte[] byteblock = { 6, 0, 0, 0, 132, 0, 0, 0, 0, 0, 0, 0, 12, 191, 4, 0, 118, 176, 176, 8, 98, 0, 0, 0, 98, 0, 0, 0, 0, 0, 94, 0, 1, 177, 0, 33, 40, 5, 41, 186, 8, 0, 69, 0, 0, 84, 48, 167, 0, 0, 255, 1, 3, 72, 192, 168, 177, 160, 10, 64, 11, 49, 8, 0, 10, 251, 67, 168, 0, 0, 79, 161, 27, 41, 0, 2, 83, 141, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 0, 0, 132, 0, 0, 0 };
     using (MemoryStream stream = new MemoryStream(byteblock))
     {
         using (BinaryReader binaryReader = new BinaryReader(stream))
         {
             AbstractBlock block = AbstractBlockFactory.ReadNextBlock(binaryReader, false, null);
             Assert.IsNotNull(block);
             EnchantedPacketBlock packetBlock = block as EnchantedPacketBlock;
             Assert.IsNotNull(packetBlock);
             Assert.AreEqual(packetBlock.InterfaceID, 0);
             Assert.AreEqual(packetBlock.Seconds, 1335958313);
             Assert.AreEqual(packetBlock.Microseconds, 152630);
             Assert.IsTrue(true);
         }
     }
 }