Ejemplo n.º 1
0
        private void GetTemporaryStorage(
            ObjectReader reader, SerializationKinds kind, out ITemporaryStreamStorage storage, out long length, CancellationToken cancellationToken)
        {
            if (kind == SerializationKinds.Bits)
            {
                storage = _storageService.CreateTemporaryStreamStorage(cancellationToken);
                using (var stream = SerializableBytes.CreateWritableStream())
                {
                    CopyByteArrayToStream(reader, stream, cancellationToken);

                    length = stream.Length;

                    stream.Position = 0;
                    storage.WriteStream(stream, cancellationToken);
                }

                return;
            }

            if (kind == SerializationKinds.MemoryMapFile)
            {
                var service2 = _storageService as ITemporaryStorageService2;
                Contract.ThrowIfNull(service2);

                var name = reader.ReadString();
                var size = reader.ReadInt64();

                storage = service2.AttachTemporaryStreamStorage(name, size, cancellationToken);
                length  = size;

                return;
            }

            throw ExceptionUtilities.UnexpectedValue(kind);
        }
Ejemplo n.º 2
0
        private ValueTuple <Metadata, ImmutableArray <ITemporaryStreamStorage> > ReadMetadataFrom(
            ObjectReader reader, SerializationKinds kind, CancellationToken cancellationToken)
        {
            var metadataKind = (MetadataImageKind)reader.ReadInt32();

            if (metadataKind == MetadataImageKind.Assembly)
            {
                using (var pooledMetadata = Creator.CreateList <ModuleMetadata>())
                    using (var pooledStorage = Creator.CreateList <ITemporaryStreamStorage>())
                    {
                        var count = reader.ReadInt32();
                        for (var i = 0; i < count; i++)
                        {
                            metadataKind = (MetadataImageKind)reader.ReadInt32();
                            Contract.ThrowIfFalse(metadataKind == MetadataImageKind.Module);

                            var tuple = ReadModuleMetadataFrom(reader, kind, cancellationToken);

                            pooledMetadata.Object.Add(tuple.Item1);
                            pooledStorage.Object.Add(tuple.Item2);
                        }

                        return(ValueTuple.Create <Metadata, ImmutableArray <ITemporaryStreamStorage> >(AssemblyMetadata.Create(pooledMetadata.Object), pooledStorage.Object.ToImmutableArrayOrEmpty()));
                    }
            }

            Contract.ThrowIfFalse(metadataKind == MetadataImageKind.Module);

            var moduleInfo = ReadModuleMetadataFrom(reader, kind, cancellationToken);

            return(ValueTuple.Create <Metadata, ImmutableArray <ITemporaryStreamStorage> >(moduleInfo.Item1, ImmutableArray.Create(moduleInfo.Item2)));
        }
Ejemplo n.º 3
0
        private ValueTuple <ModuleMetadata, ITemporaryStreamStorage> ReadModuleMetadataFrom(
            ObjectReader reader, SerializationKinds kind, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            ITemporaryStreamStorage storage;
            long length;

            GetTemporaryStorage(reader, kind, out storage, out length, cancellationToken);

            var storageStream = storage.ReadStream(cancellationToken);

            Contract.ThrowIfFalse(length == storageStream.Length);

            ModuleMetadata metadata;
            object         lifeTimeObject;

            GetMetadata(storageStream, length, out metadata, out lifeTimeObject);

            // make sure we keep storageStream alive while Metadata is alive
            // we use conditional weak table since we can't control metadata liftetime
            s_lifetimeMap.Add(metadata, lifeTimeObject);

            return(ValueTuple.Create(metadata, storage));
        }
Ejemplo n.º 4
0
        private (Metadata metadata, ImmutableArray <ITemporaryStreamStorage> storages)? TryReadMetadataFrom(
            ObjectReader reader, SerializationKinds kind, CancellationToken cancellationToken)
        {
            var imageKind = reader.ReadInt32();

            if (imageKind == MetadataFailed)
            {
                // error case
                return(null);
            }

            var metadataKind = (MetadataImageKind)imageKind;

            if (_storageService == null)
            {
                if (metadataKind == MetadataImageKind.Assembly)
                {
                    using var pooledMetadata = Creator.CreateList <ModuleMetadata>();

                    var count = reader.ReadInt32();
                    for (var i = 0; i < count; i++)
                    {
                        metadataKind = (MetadataImageKind)reader.ReadInt32();
                        Contract.ThrowIfFalse(metadataKind == MetadataImageKind.Module);

                        pooledMetadata.Object.Add(ReadModuleMetadataFrom(reader, kind));
                    }

                    return(AssemblyMetadata.Create(pooledMetadata.Object), storages : default);
Ejemplo n.º 5
0
        protected static void WritePortableExecutableReferenceHeaderTo(
            PortableExecutableReference reference, SerializationKinds kind, ObjectWriter writer, CancellationToken cancellationToken)
        {
            writer.WriteString(nameof(PortableExecutableReference));
            writer.WriteInt32((int)kind);

            WritePortableExecutableReferencePropertiesTo(reference, writer, cancellationToken);
        }
        private (Metadata metadata, ImmutableArray <ITemporaryStreamStorage> storages)? TryReadMetadataFrom(
            ObjectReader reader, SerializationKinds kind, CancellationToken cancellationToken)
        {
            var imageKind = reader.ReadInt32();

            if (imageKind == MetadataFailed)
            {
                // error case
                return(null);
            }

            var metadataKind = (MetadataImageKind)imageKind;

            if (metadataKind == MetadataImageKind.Assembly)
            {
                using (var pooledMetadata = Creator.CreateList <ModuleMetadata>())
                    using (var pooledStorage = Creator.CreateList <ITemporaryStreamStorage>())
                    {
                        var count = reader.ReadInt32();
                        for (var i = 0; i < count; i++)
                        {
                            metadataKind = (MetadataImageKind)reader.ReadInt32();
                            Contract.ThrowIfFalse(metadataKind == MetadataImageKind.Module);

                            var tuple = ReadModuleMetadataFrom(reader, kind, cancellationToken);

                            pooledMetadata.Object.Add(tuple.metadata);
                            pooledStorage.Object.Add(tuple.storage);
                        }

                        return(AssemblyMetadata.Create(pooledMetadata.Object), pooledStorage.Object.ToImmutableArrayOrEmpty());
                    }
            }

            Contract.ThrowIfFalse(metadataKind == MetadataImageKind.Module);

            var moduleInfo = ReadModuleMetadataFrom(reader, kind, cancellationToken);

            return(moduleInfo.metadata, ImmutableArray.Create(moduleInfo.storage));
        }
        private void GetTemporaryStorage(
            ObjectReader reader, SerializationKinds kind, out ITemporaryStreamStorage storage, out long length, CancellationToken cancellationToken)
        {
            if (kind == SerializationKinds.Bits)
            {
                storage = _storageService.CreateTemporaryStreamStorage(cancellationToken);
                using (var stream = SerializableBytes.CreateWritableStream())
                {
                    CopyByteArrayToStream(reader, stream, cancellationToken);

                    length = stream.Length;

                    stream.Position = 0;
                    storage.WriteStream(stream, cancellationToken);
                }

                return;
            }

            if (kind == SerializationKinds.MemoryMapFile)
            {
                var service2 = _storageService as ITemporaryStorageService2;
                Contract.ThrowIfNull(service2);

                var name = reader.ReadString();
                var size = reader.ReadInt64();

                storage = service2.AttachTemporaryStreamStorage(name, size, cancellationToken);
                length = size;

                return;
            }

            throw ExceptionUtilities.UnexpectedValue(kind);
        }
        private ValueTuple<ModuleMetadata, ITemporaryStreamStorage> ReadModuleMetadataFrom(
            ObjectReader reader, SerializationKinds kind, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            ITemporaryStreamStorage storage;
            long length;
            GetTemporaryStorage(reader, kind, out storage, out length, cancellationToken);

            var storageStream = storage.ReadStream(cancellationToken);
            Contract.ThrowIfFalse(length == storageStream.Length);

            ModuleMetadata metadata;
            object lifeTimeObject;

            GetMetadata(storageStream, length, out metadata, out lifeTimeObject);

            // make sure we keep storageStream alive while Metadata is alive
            // we use conditional weak table since we can't control metadata liftetime
            s_lifetimeMap.Add(metadata, lifeTimeObject);

            return ValueTuple.Create(metadata, storage);
        }
        private ValueTuple<Metadata, ImmutableArray<ITemporaryStreamStorage>> ReadMetadataFrom(
            ObjectReader reader, SerializationKinds kind, CancellationToken cancellationToken)
        {
            var metadataKind = (MetadataImageKind)reader.ReadInt32();
            if (metadataKind == MetadataImageKind.Assembly)
            {
                using (var pooledMetadata = Creator.CreateList<ModuleMetadata>())
                using (var pooledStorage = Creator.CreateList<ITemporaryStreamStorage>())
                {
                    var count = reader.ReadInt32();
                    for (var i = 0; i < count; i++)
                    {
                        metadataKind = (MetadataImageKind)reader.ReadInt32();
                        Contract.ThrowIfFalse(metadataKind == MetadataImageKind.Module);

                        var tuple = ReadModuleMetadataFrom(reader, kind, cancellationToken);

                        pooledMetadata.Object.Add(tuple.Item1);
                        pooledStorage.Object.Add(tuple.Item2);
                    }

                    return ValueTuple.Create<Metadata, ImmutableArray<ITemporaryStreamStorage>>(AssemblyMetadata.Create(pooledMetadata.Object), pooledStorage.Object.ToImmutableArrayOrEmpty());
                }
            }

            Contract.ThrowIfFalse(metadataKind == MetadataImageKind.Module);

            var moduleInfo = ReadModuleMetadataFrom(reader, kind, cancellationToken);
            return ValueTuple.Create<Metadata, ImmutableArray<ITemporaryStreamStorage>>(moduleInfo.Item1, ImmutableArray.Create(moduleInfo.Item2));
        }
        protected void WritePortableExecutableReferenceHeaderTo(
            PortableExecutableReference reference, SerializationKinds kind, ObjectWriter writer, CancellationToken cancellationToken)
        {
            writer.WriteString(nameof(PortableExecutableReference));
            writer.WriteInt32((int)kind);

            WriteTo(reference.Properties, writer, cancellationToken);
            writer.WriteString(reference.FilePath);
        }