public static void EnhancedPacketOption_ConvertToByte_Test(bool reorder)
        {
            EnhancedPacketOption preOption = new EnhancedPacketOption();
            EnhancedPacketOption postOption;

            preOption.Comment   = "Test Comment";
            preOption.DropCount = 25;
            byte[] md5Hash = { 3, 87, 248, 225, 163, 56, 121, 102, 219, 226, 164, 68, 165, 51, 9, 177, 59 };
            preOption.Hash       = new HashBlock(md5Hash);
            preOption.PacketFlag = new PacketBlockFlags(0xFF000000);
            byte[] preOptionByte = preOption.ConvertToByte(reorder, null);
            using (MemoryStream stream = new MemoryStream(preOptionByte))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    postOption = EnhancedPacketOption.Parse(binaryReader, reorder, null);
                }
            }

            Assert.IsNotNull(postOption);
            Assert.AreEqual(preOption.Comment, postOption.Comment);
            Assert.AreEqual(preOption.DropCount, postOption.DropCount);
            Assert.AreEqual(preOption.Hash.Algorithm, postOption.Hash.Algorithm);
            Assert.AreEqual(preOption.Hash.Value, postOption.Hash.Value);
            Assert.AreEqual(preOption.PacketFlag.Flag, postOption.PacketFlag.Flag);
        }
Exemple #2
0
        public static EnhancedPacketOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            CustomContract.Requires <ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");
            EnhancedPacketOption option = new EnhancedPacketOption();
            List <KeyValuePair <ushort, byte[]> > optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException);

            if (optionsList.Any())
            {
                foreach (var item in optionsList)
                {
                    try
                    {
                        switch (item.Key)
                        {
                        case (ushort)EnhancedPacketOptionCode.CommentCode:
                            option.Comment = UTF8Encoding.UTF8.GetString(item.Value);
                            break;

                        case (ushort)EnhancedPacketOptionCode.PacketFlagCode:
                            if (item.Value.Length == 4)
                            {
                                uint packetFlag = (BitConverter.ToUInt32(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                                option.PacketFlag = new PacketBlockFlags(packetFlag);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[EnhancedPacketOptio.Parse] PacketFlagCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 4));
                            }
                            break;

                        case (ushort)EnhancedPacketOptionCode.HashCode:
                            option.Hash = new HashBlock(item.Value);
                            break;

                        case (ushort)EnhancedPacketOptionCode.DropCountCode:
                            if (item.Value.Length == 8)
                            {
                                option.DropCount = (BitConverter.ToInt64(item.Value, 0)).ReverseByteOrder(reverseByteOrder);
                            }
                            else
                            {
                                throw new ArgumentException(string.Format("[EnhancedPacketOptio.Parse] HashCode contains invalid length. Received: {0} bytes, expected: {1}", item.Value.Length, 8));
                            }
                            break;

                        case (ushort)EnhancedPacketOptionCode.EndOfOptionsCode:
                        default:
                            break;
                        }
                    }
                    catch (Exception exc)
                    {
                        if (ActionOnException != null)
                        {
                            ActionOnException(exc);
                        }
                    }
                }
            }
            return(option);
        }