Пример #1
0
        public static PacketOption Parse(BinaryReader binaryReader, bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            CustomContract.Requires <ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");

            PacketOption option = new PacketOption();
            List <KeyValuePair <ushort, byte[]> > optionsList = EkstractOptions(binaryReader, reverseByteOrder, ActionOnException);

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

                        case (ushort)PacketOptionCode.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("[PacketOption.Parse] PacketFlagCode contains invalid length. Received: {0} bytes, expected: {1}.", item.Value.Length, 4));
                            }
                            break;

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

                        case (ushort)PacketOptionCode.EndOfOptionsCode:
                        default:
                            break;
                        }
                    }
                    catch (Exception exc)
                    {
                        if (ActionOnException != null)
                        {
                            ActionOnException(exc);
                        }
                    }
                }
            }
            return(option);
        }
Пример #2
0
        public static void PacketOption_ConvertToByte_Test(bool reorder)
        {
            PacketOption preOption = new PacketOption();
            PacketOption postOption;

            preOption.Comment = "Test Comment";
            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 = PacketOption.Parse(binaryReader, reorder, null);
                }
            }

            Assert.IsNotNull(postOption);
            Assert.AreEqual(preOption.Comment, postOption.Comment);
            Assert.AreEqual(preOption.Hash.Algorithm, postOption.Hash.Algorithm);
            Assert.AreEqual(preOption.Hash.Value, postOption.Hash.Value);
            Assert.AreEqual(preOption.PacketFlag.Flag, postOption.PacketFlag.Flag);
        }