protected override BitStream InternalValueToBitStream() { ulong bits; dynamic value = GetNumber(InternalValue); if (value > 0 && (ulong)value > MaxValue) { string msg = string.Format("Error, {0} value '{1}' is greater than the maximum {2}-bit {3} number.", debugName, value, lengthAsBits, Signed ? "signed" : "unsigned"); var inner = new OverflowException(msg); throw new SoftException(inner); } if (value < 0 && (long)value < MinValue) { string msg = string.Format("Error, {0} value '{1}' is less than the minimum {2}-bit {3} number.", debugName, value, lengthAsBits, Signed ? "signed" : "unsigned"); var inner = new OverflowException(msg); throw new SoftException(inner); } if (LittleEndian) { bits = LittleBitWriter.GetBits(value, (int)lengthAsBits); } else { bits = BigBitWriter.GetBits(value, (int)lengthAsBits); } var bs = new BitStream(); bs.WriteBits(bits, (int)lengthAsBits); return(bs); }
private dynamic FromBitstream(BitStream bs) { ulong bits = bs.ReadBits((int)lengthAsBits); if (Signed) { if (LittleEndian) { return(LittleBitWriter.GetInt64(bits, (int)lengthAsBits)); } else { return(BigBitWriter.GetInt64(bits, (int)lengthAsBits)); } } else { if (LittleEndian) { return(LittleBitWriter.GetUInt64(bits, (int)lengthAsBits)); } else { return(BigBitWriter.GetUInt64(bits, (int)lengthAsBits)); } } }
public void BitConverter() { /* * Unsigned, BE, 12bit "A B C" -> 0x0ABC -> 2748 * Signed , BE, 12bit "A B C" -> 0xFABC -> -1348 * Unsigned, LE, 12bit "B C A" -> 0x0ABC -> 2748 * Signed , LE, 12bit "B C A" -> 0xFABC -> -1348 */ byte[] val = null; val = BitWriter <BigEndian> .GetBytes(0xABC, 0); Assert.AreEqual(new byte[] { }, val); Assert.AreEqual(0, BigBitWriter.GetUInt64(val, 0)); Assert.AreEqual(0, BigBitWriter.GetInt64(val, 0)); val = BitWriter <BigEndian> .GetBytes(0xABC, 12); Assert.AreEqual(new byte[] { 0xab, 0xc0 }, val); Assert.AreEqual(2748, BigBitWriter.GetUInt64(val, 12)); Assert.AreEqual(-1348, BigBitWriter.GetInt64(val, 12)); val = BitWriter <LittleEndian> .GetBytes(0xABC, 0); Assert.AreEqual(new byte[] { }, val); Assert.AreEqual(0, LittleBitWriter.GetUInt64(val, 0)); Assert.AreEqual(0, LittleBitWriter.GetInt64(val, 0)); val = BitWriter <LittleEndian> .GetBytes(0xABC, 12); Assert.AreEqual(new byte[] { 0xbc, 0xa0 }, val); Assert.AreEqual(2748, LittleBitWriter.GetUInt64(val, 12)); Assert.AreEqual(-1348, LittleBitWriter.GetInt64(val, 12)); ulong bits = 0; bits = BitWriter <BigEndian> .GetBits(0xABC, 12); Assert.AreEqual(0xABC, bits); Assert.AreEqual(2748, BigBitWriter.GetUInt64(bits, 12)); Assert.AreEqual(-1348, BigBitWriter.GetInt64(bits, 12)); bits = BitWriter <LittleEndian> .GetBits(0xABC, 12); Assert.AreEqual(0xBCA, bits); Assert.AreEqual(2748, LittleBitWriter.GetUInt64(bits, 12)); Assert.AreEqual(-1348, LittleBitWriter.GetInt64(bits, 12)); }
protected override Variant GenerateInternalValue() { BitStream bits = new BitStream(); bits.BigEndian(); int shift; if (_isLittleEndian && lengthAsBits < 64) { // Expand to 64 bits and skip remaining bits at the beginning shift = 64 - (int)lengthAsBits; bits.WriteBits(0, 64); } else { // Expand to 'size' bits shift = 0; bits.WriteBits(0, (int)lengthAsBits); } foreach (DataElement child in this) { if (child is Flag) { bits.SeekBits(((Flag)child).position + shift, System.IO.SeekOrigin.Begin); bits.Write(child.Value, child); } else { throw new ApplicationException("Flag has child thats not a flag!"); } } if (!_isLittleEndian) { return(new Variant(bits)); } bits.SeekBits(0, System.IO.SeekOrigin.Begin); ulong val = bits.ReadUInt64(); ulong final = LittleBitWriter.GetBits(val, (int)lengthAsBits); BitStream bs = new BitStream(); bs.WriteBits(final, (int)lengthAsBits); return(new Variant(bs)); }
protected override void OnSetProperty(string property, Variant value) { if (property == "MTU") { uint mtu = 0; if (value.GetVariantType() == Variant.VariantType.BitStream) { var bs = (BitStream)value; bs.SeekBits(0, SeekOrigin.Begin); int len = (int)Math.Min(bs.LengthBits, 32); ulong bits = bs.ReadBits(len); mtu = LittleBitWriter.GetUInt32(bits, len); } else if (value.GetVariantType() == Variant.VariantType.ByteString) { byte[] buf = (byte[])value; int len = Math.Min(buf.Length * 8, 32); mtu = LittleBitWriter.GetUInt32(buf, len); } else { throw new SoftException("Can't set MTU, 'value' is an unsupported type."); } if (MaxMTU >= mtu && mtu >= MinMTU) { try { using (var sock = OpenSocket(mtu)) { Logger.Debug("Changed MTU of {0} to {1}.", Interface, mtu); } } catch (Exception ex) { string err = "Failed to change MTU of '{0}' to {1}. {2}".Fmt(Interface, mtu, ex.Message); Logger.Error(err); var se = new SoftException(err, ex); throw new SoftException(se); } } } }
protected override void OnSetProperty(string property, Variant value) { if (property == "MTU") { uint mtu = 0; if (value.GetVariantType() == Variant.VariantType.BitStream) { var bs = (BitStream)value; bs.SeekBits(0, SeekOrigin.Begin); int len = (int)Math.Min(bs.LengthBits, 32); ulong bits = bs.ReadBits(len); mtu = LittleBitWriter.GetUInt32(bits, len); } else if (value.GetVariantType() == Variant.VariantType.ByteString) { byte[] buf = (byte[])value; int len = Math.Min(buf.Length * 8, 32); mtu = LittleBitWriter.GetUInt32(buf, len); } else { throw new SoftException("Can't set MTU, 'value' is an unsupported type."); } if (MaxMTU >= mtu && mtu >= MinMTU) { using (var cfg = NetworkAdapter.CreateInstance(_iface)) { try { cfg.MTU = mtu; } catch (Exception ex) { string msg = ex.Message; if (ex is TypeInitializationException || ex is TargetInvocationException) { msg = ex.InnerException.Message; } string err = "Failed to change MTU of '{0}' to {1}. {2}".Fmt(_iface, mtu, msg); Logger.Error(err); var se = new SoftException(err, ex); throw new SoftException(se); } _mtu = cfg.MTU; if (!_mtu.HasValue || _mtu.Value != mtu) { string err = "Failed to change MTU of '{0}' to {1}. The change did not take effect.".Fmt(_iface, mtu); Logger.Error(err); throw new SoftException(err); } else { Logger.Debug("Changed MTU of '{0}' to {1}.", _iface, mtu); } } } else { Logger.Debug("Not setting MTU of '{0}', value is out of range.", _iface); } } }