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 Serialize(BitWriter bw)
 {
     bw.Write((UInt32)Type);
     if (Type != UniqueIdType.Unknown)
     {
         SerializeId(bw);
     }
 }
 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 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 ?? "");
         }
     }
 }
 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 Serialize(BitWriter bw)
 {
     bw.Write((byte)Type);
     SerializeId(bw);
 }
 protected void SerializeId(BitWriter bw)
 {
     bw.Write(Id);
     bw.Write(PlayerNumber);
 }