public void WriteMessage(object message, Type type, Stream stream)
 {
     Formatter formatter;
     if (!_formattersByType.TryGetValue(type, out formatter))
     {
         var s =
             string.Format(
                 "Can't find serializer for unknown object type '{0}'. Have you passed all known types to the constructor?",
                 message.GetType());
         throw new InvalidOperationException(s);
     }
     using (var bin = new BitWriter(stream))
     {
         bin.Write(formatter.ContractName);
         byte[] buffer;
         using (var inner = new MemoryStream())
         {
             // Some formatter implementations close the stream after writing.
             // kudos to Slav for reminding that
             formatter.SerializeDelegate(message, inner);
             buffer = inner.ToArray();
         }
         bin.Write7BitInt(buffer.Length);
         bin.Write(buffer);
     }
 }
        public void TestRandomFixedFloatRoundTripSerialization()
        {
            var r = new Random();

            for (int n = 0; n < 1000; ++n)
            {

                var f = (float)((r.NextDouble() * 2) - 1);

                for (var i = 0; i < 2; ++i)
                {
                    var bw = new BitWriter(32);
                    bw.WriteFixedCompressedFloat(f, 1, 16);
                    var br = new BitReader(bw.GetBits(0, bw.Length).ToArray());
                    var f2 = br.ReadFixedCompressedFloat(1, 16);

                    if (i == 0)
                    {
                        // We're generating floats that are probably going to lose precision when serialized.
                        // So the first time around just check to see if we're close.
                        Assert.IsTrue(Math.Abs(f - f2) < 0.0001);
                    }
                    else
                    {
                        Assert.AreEqual(f, f2);
                    }
                    Assert.AreEqual(bw.Length, br.Position);
                    f = f2;
                }
            }
        }
Ejemplo n.º 3
0
 public void Serialize(BitWriter bw)
 {
     bw.Write((UInt32)Type);
     if (Type != UniqueIdType.Unknown)
     {
         SerializeId(bw);
     }
 }
Ejemplo n.º 4
0
 public void WriteAttributes(ICollection<MessageAttribute> attributes, Stream stream)
 {
     using (var writer = new BitWriter(stream))
     {
         writer.Write7BitInt(attributes.Count);
         foreach (var attribute in attributes)
         {
             writer.Write(attribute.Key ?? "");
             writer.Write(attribute.Value ?? "");
         }
     }
 }
Ejemplo n.º 5
0
    public byte[] Encode(byte[] input)
    {
        var buffer = new byte[0xffff];
        var bufferIdx = 0;
        using (var ms = new MemoryStream())
        {
            using (var bw = new BitWriter(ms))
            {

                int i = 0;
                string w = "";
                while (i < input.Length)
                {

                    bufferIdx = 0;
                    buffer[bufferIdx++] = input[i++];

                    int cur = -1, prev = -1;
                    while ((cur = lookup.FindEntry(buffer, 0, bufferIdx)) != -1 && i < input.Length)
                    {
                        buffer[bufferIdx++] = input[i++];
                        prev = cur;
                    }

                    if (cur == -1)
                    {
                        //Debug.WriteLine("E: " + prev);
                        bw.WriteBits((uint)prev, (uint)lookup.CodeLen);
                        lookup.AppendChar(buffer[bufferIdx - 1], prev);
                        i--;
                    }
                    else
                    {
                        //Debug.WriteLine("E: " + cur);
                        bw.WriteBits((uint)cur, (uint)lookup.CodeLen);
                    }

                    lookup.EnsureCodeLen2();
                    if (lookup.Count > 0xFFF)
                    {
                        lookup.Reset();
                    }
                }
            }
            return ms.ToArray();
        }
    }
Ejemplo n.º 6
0
        public FileAppendOnlyStore(string name)
        {
            var path = name ?? "";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            _checkStream = new FileStream(Path.Combine(path, "stream.chk"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
            if (_checkStream.Length != 8)
                _checkStream.SetLength(8);
            _checkBits = new BitWriter(_checkStream);

            var b = new byte[8];
            _checkStream.Read(b, 0, 8);

            var offset = BitConverter.ToInt64(b, 0);

            _dataStream = new FileStream(Path.Combine(path, "stream.dat"), FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
            _dataStream.Seek(offset, SeekOrigin.Begin);
            _dataBits = new BitWriter(_dataStream);
        }
Ejemplo n.º 7
0
 public static StorageFrameEncoded EncodeFrame(string key, byte[] buffer, long stamp)
 {
     using (var sha1 = new SHA1Managed())
     {
         // version, ksz, vsz, key, value, sha1
         byte[] data;
         using (var memory = new MemoryStream())
         {
             using (var crypto = new CryptoStream(memory, sha1, CryptoStreamMode.Write))
             using (var binary = new BitWriter(crypto))
             {
                 binary.Write(stamp);
                 binary.Write(key);
                 binary.Write7BitInt(buffer.Length);
                 binary.Write(buffer);
             }
             data = memory.ToArray();
         }
         return new StorageFrameEncoded(data, sha1.Hash);
     }
 }
 public void WriteCompactInt(int value, Stream stream)
 {
     using (var binary = new BitWriter(stream))
     {
         binary.Write7BitInt(value);
     }
 }
        public void TestVectorRoundTripSerializationFromBinary(string binary)
        {
            var br = new BitReader(binary);
            var v = Vector3D.Deserialize(br);

            var bw = new BitWriter(32);
            v.Serialize(bw);
            var writtenBits = bw.GetBits(0, bw.Length).ToBinaryString();

            Assert.AreEqual(binary, writtenBits);
        }
        public void TestVectorRoundTripSerialization(float x, float y, float z)
        {
            var v = new Vector3D(x, y, z);
            for (var i = 0; i < 2; ++i)
            {
                var bw = new BitWriter(32);
                v.Serialize(bw);
                var br = new BitReader(bw.GetBits(0, bw.Length).ToArray());
                var v2 = Vector3D.Deserialize(br);

                if (i == 0)
                {
                    // We're generating floats even though these are serialized as ints
                    // So the first time around just check to see if we're at the nearest int
                    Assert.IsTrue(Math.Abs(v.X - v2.X) <= 0.5);
                    Assert.IsTrue(Math.Abs(v.Y - v2.Y) <= 0.5);
                    Assert.IsTrue(Math.Abs(v.Z - v2.Z) <= 0.5);
                }
                else
                {
                    Assert.AreEqual(v.X, v2.X);
                    Assert.AreEqual(v.Y, v2.Y);
                    Assert.AreEqual(v.Z, v2.Z);
                }
                Assert.AreEqual(bw.Length, br.Position);
                v = v2;
            }
        }
 public void TestUIntMaxRoundTripSerialization(UInt32 value, UInt32 max)
 {
     for (var i = 0; i < 2; ++i)
     {
         var bw = new BitWriter(32);
         bw.Write(value, max);
         var br = new BitReader(bw.GetBits(0, bw.Length).ToArray());
         var val2 = br.ReadUInt32Max((int)max);
         Assert.AreEqual(value, val2);
         Assert.AreEqual(bw.Length, br.Position);
         value = val2;
     }
 }
 public void TestUIntFixedRoundTripSerialization(UInt32 value, int numBits)
 {
     for (var i = 0; i < 2; ++i)
     {
         var bw = new BitWriter(32);
         bw.WriteFixedBitCount(value, numBits);
         var br = new BitReader(bw.GetBits(0, bw.Length).ToArray());
         var val2 = br.ReadUInt32FromBits(numBits);
         Assert.AreEqual(value, val2);
         Assert.AreEqual(bw.Length, br.Position);
         value = val2;
     }
 }
Ejemplo n.º 13
0
 public void Serialize(BitWriter bw)
 {
     bw.Write((byte)Type);
     SerializeId(bw);
 }
Ejemplo n.º 14
0
 protected void SerializeId(BitWriter bw)
 {
     bw.Write(Id);
     bw.Write(PlayerNumber);
 }