示例#1
0
        public static StorageFrameDecoded ReadFrame(Stream source)
        {
            using (var binary = new BitReader(source))
            {
                var version      = binary.ReadInt64();
                var name         = binary.ReadString();
                var len          = binary.Read7BitInt();
                var bytes        = binary.ReadBytes(len);
                var sha1Expected = binary.ReadBytes(20);

                var decoded = new StorageFrameDecoded(bytes, name, version);
                if (decoded.IsEmpty && sha1Expected.All(b => b == 0))
                {
                    // this looks like end of the stream.
                    return(decoded);
                }

                //SHA1. TODO: compute hash nicely
                var sha1Actual = EncodeFrame(name, bytes, version).Hash;
                if (!sha1Expected.SequenceEqual(sha1Actual))
                {
                    throw new StorageFrameException("SHA mismatch in data frame");
                }

                return(decoded);
            }
        }
        public static StorageFrameDecoded ReadFrame(Stream source)
        {
            using (var binary = new BitReader(source))
            {
                var version = binary.ReadInt64();
                var name = binary.ReadString();
                var len = binary.Read7BitInt();
                var bytes = binary.ReadBytes(len);
                var sha1Expected = binary.ReadBytes(20);

                var decoded = new StorageFrameDecoded(bytes, name, version);
                if (decoded.IsEmpty && sha1Expected.All(b => b == 0))
                {
                    // this looks like end of the stream.
                    return decoded;
                }

                //SHA1. TODO: compute hash nicely
                var sha1Actual = EncodeFrame(name, bytes, version).Hash;
                if (!sha1Expected.SequenceEqual(sha1Actual))
                    throw new StorageFrameException("SHA mismatch in data frame");
                
                return decoded;
            }
        }
示例#3
0
 public static bool TryReadFrame(Stream source, out StorageFrameDecoded result)
 {
     result = default(StorageFrameDecoded);
     try
     {
         result = ReadFrame(source);
         return(!result.IsEmpty);
     }
     catch (EndOfStreamException)
     {
         // we are done
         return(false);
     }
     catch (Exception ex)
     {
         NetcoLogger.GetLogger(typeof(StorageFramesEvil)).Error(ex, "ReadFrame encountered error");
         // Auto-clean?
         return(false);
     }
 }
示例#4
0
 public static bool TryReadFrame(Stream source, out StorageFrameDecoded result)
 {
     result = default(StorageFrameDecoded);
     try
     {
         result = ReadFrame(source);
         return(!result.IsEmpty);
     }
     catch (EndOfStreamException)
     {
         // we are done
         return(false);
     }
     catch (Exception ex)
     {
         Trace.WriteLine(ex);
         // Auto-clean?
         return(false);
     }
 }
 public static bool TryReadFrame(Stream source, out StorageFrameDecoded result)
 {
     result = default(StorageFrameDecoded);
     try
     {
         result = ReadFrame(source);
         return !result.IsEmpty;
     }
     catch (EndOfStreamException)
     {
         // we are done
         return false;
     }
     catch (Exception ex)
     {
         Trace.WriteLine(ex);
         // Auto-clean?
         return false;
     }
 }