public static SimplePacketBlock 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.SimplePacket, "Invalid packet type");

            long positionInStream = baseBlock.PositionInStream;

            using (Stream stream = new MemoryStream(baseBlock.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    int packetLength = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);

                    byte[] data = binaryReader.ReadBytes(packetLength);
                    if (data.Length < packetLength)
                    {
                        throw new EndOfStreamException("Unable to read beyond the end of the stream");
                    }
                    int remainderLength = packetLength % BaseBlock.AlignmentBoundary;
                    if (remainderLength > 0)
                    {
                        int paddingLength = BaseBlock.AlignmentBoundary - remainderLength;
                        binaryReader.ReadBytes(paddingLength);
                    }
                    SimplePacketBlock simplePacketBlock = new SimplePacketBlock(data, positionInStream);
                    return(simplePacketBlock);
                }
            }
        }
 public static void AbstractBlockFactory_ConvertTo_SimplePacketBlock_Test()
 {
     byte[] byteblock = { 3, 0, 0, 0, 139, 0, 0, 0, 123, 0, 0, 0, 104, 83, 17, 243, 59, 0, 0, 0, 151, 143, 0, 243, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 208, 241, 255, 191, 127, 0, 0, 0, 208, 79, 17, 243, 59, 0, 0, 0, 96, 5, 0, 243, 59, 0, 0, 0, 252, 6, 0, 243, 59, 0, 0, 0, 96, 2, 0, 243, 59, 0, 0, 0, 88, 6, 64, 0, 0, 0, 0, 0, 104, 83, 17, 243, 59, 0, 0, 0, 104, 83, 17, 243, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 139, 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);
             SimplePacketBlock packetBlock = block as SimplePacketBlock;
             Assert.IsNotNull(packetBlock);
             Assert.AreEqual(packetBlock.PacketLength, 123);
         }
     }
 }
Example #3
0
        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 = EnhancedPacketBlock.Parse(baseblock, ActionOnException);
                    break;

                default:
                    break;
                }
                return(block);
            }
            catch (Exception exc)
            {
                ActionOnException(exc);
                return(null);
            }
        }