private IEnumerable<CompletionPort> CreateParentLocator(AsyncMachine<ParentLocator> machine)
        {
            var locator = new ParentLocator();

            BeginReadPlatformCode(attributeHelper.GetAttribute(() => locator.PlatformCode), machine.CompletionCallback, null);
            yield return CompletionPort.SingleOperation;
            locator.PlatformCode = EndReadPlatformCode(machine.CompletionResult);

            BeginReadPlatformDataSpace(attributeHelper.GetAttribute(() => locator.PlatformDataSpace), machine.CompletionCallback, null);
            yield return CompletionPort.SingleOperation;
            locator.PlatformDataSpace = EndReadPlatformDataSpace(machine.CompletionResult);

            BeginReadPlatformDataLength(attributeHelper.GetAttribute(() => locator.PlatformDataLength), machine.CompletionCallback, null);
            yield return CompletionPort.SingleOperation;
            locator.PlatformDataLength = EndReadPlatformDataLength(machine.CompletionResult);

            BeginReadReserved(attributeHelper.GetAttribute(() => locator.Reserved), machine.CompletionCallback, null);
            yield return CompletionPort.SingleOperation;
            locator.Reserved = EndReadReserved(machine.CompletionResult);

            BeginReadPlatformDataOffset(attributeHelper.GetAttribute(() => locator.PlatformDataOffset), machine.CompletionCallback, null);
            yield return CompletionPort.SingleOperation;
            locator.PlatformDataOffset = EndReadPlatformDataOffset(machine.CompletionResult);

            BeginReadFileLocator(locator, machine.CompletionCallback, null);
            yield return CompletionPort.SingleOperation;
            locator.PlatformSpecificFileLocator = EndReadFileLocator(machine.CompletionResult);

            machine.ParameterValue = locator;
        }
        private static IEnumerable<CompletionPort> CreateFixedVhdFileAtAsync(AsyncMachine machine, Stream destination, long virtualSize)
        {
            var footer = VhdFooterFactory.CreateFixedDiskFooter(virtualSize);
            var serializer = new VhdFooterSerializer(footer);
            var buffer = serializer.ToByteArray();
            destination.SetLength(virtualSize + VhdConstants.VHD_FOOTER_SIZE);
            destination.Seek(-VhdConstants.VHD_FOOTER_SIZE, SeekOrigin.End);

            destination.BeginWrite(buffer, 0, buffer.Length, machine.CompletionCallback, null);
            yield return CompletionPort.SingleOperation;
            destination.EndWrite(machine.CompletionResult);
            destination.Flush();
        }
        private IEnumerable<CompletionPort> CreateAsync(AsyncMachine<BlockAllocationTable> machine)
        {
            dataReader.SetPosition(header.TableOffset);

            var bat = new uint[header.MaxTableEntries];
            for (int block = 0; block < header.MaxTableEntries; block++)
            {
                dataReader.BeginReadUInt32(machine.CompletionCallback, null);
                yield return CompletionPort.SingleOperation;
                bat[block] = dataReader.EndReadUInt32(machine.CompletionResult);
            }
            machine.ParameterValue = new BlockAllocationTable(header.MaxTableEntries, header.BlockSize, bat);
        }
Esempio n. 4
0
        public static IEnumerable<CompletionPort> ReadBytesAsync(AsyncMachine<byte[]> machine, Stream stream, int length)
        {
            var attributeHelper = new AttributeHelper<VhdHeader>();
            var buffer = new byte[length];

            int readCount = 0;
            int remaining = length;
            while (remaining > 0)
            {
                stream.BeginRead(buffer, readCount, remaining, machine.CompletionCallback, null);
                yield return CompletionPort.SingleOperation;
                var currentRead = stream.EndRead(machine.CompletionResult);
                if (currentRead == 0)
                {
                    break;
                }
                readCount += currentRead;
                remaining -= currentRead;
            }
            machine.ParameterValue = buffer;
            yield break;
        }
Esempio n. 5
0
        IEnumerable<CompletionPort> FillBuffer(AsyncMachine machine, int numBytes)
        {
            if (m_buffer != null && (numBytes < 0 || numBytes > m_buffer.Length))
            {
                throw new ArgumentOutOfRangeException("numBytes", String.Format("Expected (0-16) however found: {0}", numBytes));
            }
            int bytesRead = 0;
            int n = 0;

            // Need to find a good threshold for calling ReadByte() repeatedly 
            // vs. calling Read(byte[], int, int) for both buffered & unbuffered 
            // streams.
            if (numBytes == 1)
            {
                this.reader.BaseStream.BeginRead(m_buffer, 0, numBytes, machine.CompletionCallback, null);
                yield return CompletionPort.SingleOperation;
                n = this.reader.BaseStream.EndRead(machine.CompletionResult);
                if (n == -1)
                {
                    throw new EndOfStreamException();
                }
                m_buffer[0] = (byte)n;
            }

            do
            {
                this.reader.BaseStream.BeginRead(m_buffer, bytesRead, numBytes - bytesRead, machine.CompletionCallback, null);
                yield return CompletionPort.SingleOperation;
                n = this.reader.BaseStream.EndRead(machine.CompletionResult);

                if (n == 0)
                {
                    throw new EndOfStreamException();
                }
                bytesRead += n;
            } while (bytesRead < numBytes);
        }
Esempio n. 6
0
 public static void EndCreateFixedVhdFile(IAsyncResult result)
 {
     AsyncMachine.EndAsyncMachine(result);
 }
 public VhdFile EndCreate(IAsyncResult result)
 {
     return(AsyncMachine <VhdFile> .EndAsyncMachine(result));
 }
 public IAsyncResult BeginCreate(AsyncCallback callback, object state)
 {
     return(AsyncMachine <BlockAllocationTable> .BeginAsyncMachine(CreateAsync, callback, state));
 }
Esempio n. 9
0
 public static IAsyncResult BeginCreateFixedVhdFile(Stream destination, long size, AsyncCallback callback, object state)
 {
     return(AsyncMachine.BeginAsyncMachine(CreateFixedVhdFileAtAsync, destination, size, callback, state));
 }
Esempio n. 10
0
 public IAsyncResult BeginReadString(int count, AsyncCallback callback, object state)
 {
     return(AsyncMachine <string> .BeginAsyncMachine(ReadStringAsync, this.reader.BaseStream.Position, count, callback, state));
 }
Esempio n. 11
0
 private IEnumerable<CompletionPort> ReadStringAsync(AsyncMachine<string> machine, long offset, int count)
 {
     BeginReadBytes(offset, count, machine.CompletionCallback, null);
     yield return CompletionPort.SingleOperation;
     byte[] values = EndReadBytes(machine.CompletionResult);
     machine.ParameterValue = Encoding.ASCII.GetString(values);
 }
Esempio n. 12
0
 private IAsyncResult BeginReadParentLocators(VhdPropertyAttribute attribute, AsyncCallback callback, object state)
 {
     return(AsyncMachine <IList <ParentLocator> > .BeginAsyncMachine(CreateParentLocators, attribute, callback, state));
 }
Esempio n. 13
0
 public static IAsyncResult BeginReadBytes(Stream stream, long offset, int length, SeekOrigin origin, AsyncCallback callback, object state)
 {
     stream.Seek(-offset, SeekOrigin.End);
     return(AsyncMachine <byte[]> .BeginAsyncMachine(ReadBytesAsync, stream, length, callback, state));
 }
Esempio n. 14
0
 private IAsyncResult BeginReadDiskGeometry(VhdPropertyAttribute attribute, AsyncCallback callback, object state)
 {
     return(AsyncMachine <DiskGeometry> .BeginAsyncMachine(ReadDiskGeometryAsync, attribute, callback, state));
 }
Esempio n. 15
0
 private DiskGeometry EndReadDiskGeometry(IAsyncResult result)
 {
     return(AsyncMachine <DiskGeometry> .EndAsyncMachine(result));
 }
Esempio n. 16
0
        private IEnumerable <CompletionPort> CreateFooterAsync(AsyncMachine <VhdFooter> machine)
        {
            ValidateVhdSize();
            var attributeHelper = new AttributeHelper <VhdFooter>();
            var footer          = new VhdFooter();

            BeginReadVhdCookie(attributeHelper.GetAttribute(() => footer.Cookie), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.Cookie = TryCatch <VhdCookie>(EndReadVhdCookie, machine.CompletionResult);

            BeginReadFeatures(attributeHelper.GetAttribute(() => footer.Features), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.Features = TryCatch <VhdFeature>(EndReadFeatures, machine.CompletionResult);

            BeginReadVhdFileFormatVersion(attributeHelper.GetAttribute(() => footer.FileFormatVersion), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.FileFormatVersion = TryCatch <VhdFileFormatVersion>(EndReadVhdFileFormatVersion, machine.CompletionResult);

            BeginReadHeaderOffset(attributeHelper.GetAttribute(() => footer.HeaderOffset), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.HeaderOffset = TryCatch <long>(EndReadHeaderOffset, machine.CompletionResult);

            BeginReadTimeStamp(attributeHelper.GetAttribute(() => footer.TimeStamp), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.TimeStamp = TryCatch <DateTime>(EndReadTimeStamp, machine.CompletionResult);

            BeginReadCreatorApplication(attributeHelper.GetAttribute(() => footer.CreatorApplication), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.CreatorApplication = TryCatch <string>(EndReadCreatorApplication, machine.CompletionResult);

            BeginReadCreatorVersion(attributeHelper.GetAttribute(() => footer.CreatorVersion), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.CreatorVersion = TryCatch <VhdCreatorVersion>(EndReadCreatorVersion, machine.CompletionResult);

            BeginReadCreatorHostOsType(attributeHelper.GetAttribute(() => footer.CreatorHostOsType), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.CreatorHostOsType = TryCatch <HostOsType>(EndReadCreatorHostOsType, machine.CompletionResult);

            BeginReadPhysicalSize(attributeHelper.GetAttribute(() => footer.PhsyicalSize), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.PhsyicalSize = TryCatch <long>(EndReadPhysicalSize, machine.CompletionResult);

            BeginReadVirtualSize(attributeHelper.GetAttribute(() => footer.VirtualSize), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.VirtualSize = TryCatch <long>(EndReadVirtualSize, machine.CompletionResult);

            BeginReadDiskGeometry(attributeHelper.GetAttribute(() => footer.DiskGeometry), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.DiskGeometry = TryCatch <DiskGeometry>(EndReadDiskGeometry, machine.CompletionResult);

            BeginReadDiskType(attributeHelper.GetAttribute(() => footer.DiskType), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.DiskType = TryCatch <DiskType>(EndReadDiskType, machine.CompletionResult);

            BeginReadCheckSum(attributeHelper.GetAttribute(() => footer.CheckSum), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.CheckSum = TryCatch <uint>(EndReadCheckSum, machine.CompletionResult);

            BeginReadUniqueId(attributeHelper.GetAttribute(() => footer.UniqueId), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.UniqueId = TryCatch <Guid>(EndReadUniqueId, machine.CompletionResult);

            BeginReadSavedState(attributeHelper.GetAttribute(() => footer.SavedState), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.SavedState = TryCatch <bool>(EndReadSavedState, machine.CompletionResult);

            BeginReadReserved(attributeHelper.GetAttribute(() => footer.Reserved), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.Reserved = TryCatch <byte[]>(EndReadReserved, machine.CompletionResult);

            BeginReadWholeFooter(attributeHelper.GetAttribute(() => footer.RawData), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            footer.RawData = TryCatch <byte[]>(EndReadWholeFooter, machine.CompletionResult);

            machine.ParameterValue = footer;
        }
Esempio n. 17
0
 public IAsyncResult BeginCreateFooter(AsyncCallback callback, object state)
 {
     return(AsyncMachine <VhdFooter> .BeginAsyncMachine(CreateFooterAsync, callback, state));
 }
 public BlockAllocationTable EndCreate(IAsyncResult result)
 {
     return(AsyncMachine <BlockAllocationTable> .EndAsyncMachine(result));
 }
Esempio n. 19
0
 public IAsyncResult BeginReadCreate(AsyncCallback callback, object state)
 {
     return(AsyncMachine <ParentLocator> .BeginAsyncMachine(CreateParentLocator, callback, state));
 }
Esempio n. 20
0
 private IList <ParentLocator> EndReadParentLocators(IAsyncResult result)
 {
     return(AsyncMachine <IList <ParentLocator> > .EndAsyncMachine(result));
 }
Esempio n. 21
0
 public ParentLocator EndReadCreate(IAsyncResult result)
 {
     return(AsyncMachine <ParentLocator> .EndAsyncMachine(result));
 }
Esempio n. 22
0
 public IAsyncResult BeginReadBoolean(long offset, AsyncCallback callback, object state)
 {
     this.SetPosition(offset);
     return(AsyncMachine.BeginAsyncMachine(FillBuffer, 1, callback, state));
 }
Esempio n. 23
0
 public static byte[] EndReadBytes(IAsyncResult result)
 {
     return(AsyncMachine <byte[]> .EndAsyncMachine(result));
 }
Esempio n. 24
0
 public string EndReadString(IAsyncResult result)
 {
     return(AsyncMachine <string> .EndAsyncMachine(result));
 }
Esempio n. 25
0
 private IEnumerable<CompletionPort> ReadBytesAsync(AsyncMachine<byte[]> machine, long offset, int count)
 {
     StreamHelper.BeginReadBytes(this.reader.BaseStream, offset, count, machine.CompletionCallback, null);
     yield return CompletionPort.SingleOperation;
     byte[] values = StreamHelper.EndReadBytes(machine.CompletionResult);
     machine.ParameterValue = values;
 }
Esempio n. 26
0
 public IAsyncResult BeginReadBytes(long offset, int count, AsyncCallback callback, object state)
 {
     this.SetPosition(offset);
     return(AsyncMachine <byte[]> .BeginAsyncMachine(ReadBytesAsync, offset, count, callback, state));
 }
Esempio n. 27
0
 public IAsyncResult BeginCreateHeader(AsyncCallback callback, object state)
 {
     return(AsyncMachine <VhdHeader> .BeginAsyncMachine(CreateVhdHeader, callback, state));
 }
Esempio n. 28
0
 public bool EndReadBoolean(IAsyncResult result)
 {
     AsyncMachine.EndAsyncMachine(result);
     return(m_buffer[0] != 0);
 }
Esempio n. 29
0
        private static IEnumerable<CompletionPort> ValidateAsync(AsyncMachine<IList<VhdValidationResult>> machine, VhdValidationType validation, Stream vhdStream)
        {
            var result = new List<VhdValidationResult>();

            var fileFactory = new VhdFileFactory();

            fileFactory.BeginCreate(vhdStream, machine.CompletionCallback, null);
            yield return CompletionPort.SingleOperation;

            VhdFile vhdFile = null;
            Exception exception = null;
            try
            {
                vhdFile = fileFactory.EndCreate(machine.CompletionResult);
            }
            catch (VhdParsingException e)
            {
                exception = e;
            }

            machine.ParameterValue = DoValidate(validation, vhdFile, exception);
        }
Esempio n. 30
0
 public VhdHeader EndCreateHeader(IAsyncResult result)
 {
     return(AsyncMachine <VhdHeader> .EndAsyncMachine(result));
 }
Esempio n. 31
0
        private IEnumerable <CompletionPort> CreateVhdHeader(AsyncMachine <VhdHeader> machine)
        {
            if (footer.DiskType != DiskType.Dynamic && footer.DiskType != DiskType.Differencing)
            {
                machine.ParameterValue = null;
                yield break;
            }

            var attributeHelper = new AttributeHelper <VhdHeader>();
            var header          = new VhdHeader();

            BeginReadHeaderCookie(attributeHelper.GetAttribute(() => header.Cookie), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.Cookie = TryCatch <VhdCookie>(EndReadHeaderCookie, machine.CompletionResult);

            BeginReadDataOffset(attributeHelper.GetAttribute(() => header.DataOffset), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.DataOffset = TryCatch <long>(EndReadDataOffset, machine.CompletionResult);

            BeginReadBATOffset(attributeHelper.GetAttribute(() => header.TableOffset), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.TableOffset = TryCatch <long>(EndReadBATOffset, machine.CompletionResult);

            BeginReadHeaderVersion(attributeHelper.GetAttribute(() => header.HeaderVersion), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.HeaderVersion = TryCatch <VhdHeaderVersion>(EndReadHeaderVersion, machine.CompletionResult);

            BeginReadMaxTableEntries(attributeHelper.GetAttribute(() => header.MaxTableEntries), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.MaxTableEntries = TryCatch <uint>(EndReadMaxTableEntries, machine.CompletionResult);

            BeginReadBlockSize(attributeHelper.GetAttribute(() => header.BlockSize), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.BlockSize = TryCatch <uint>(EndReadBlockSize, machine.CompletionResult);

            BeginReadCheckSum(attributeHelper.GetAttribute(() => header.CheckSum), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.CheckSum = TryCatch <uint>(EndReadCheckSum, machine.CompletionResult);

            BeginReadParentUniqueId(attributeHelper.GetAttribute(() => header.ParentUniqueId), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.ParentUniqueId = TryCatch <Guid>(EndReadParentUniqueId, machine.CompletionResult);

            BeginReadParentTimeStamp(attributeHelper.GetAttribute(() => header.ParentTimeStamp), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.ParentTimeStamp = TryCatch <DateTime>(EndReadParentTimeStamp, machine.CompletionResult);

            BeginReadReserved1(attributeHelper.GetAttribute(() => header.Reserverd1), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.Reserverd1 = TryCatch <uint>(EndReadReserved1, machine.CompletionResult);

            BeginReadParentPath(attributeHelper.GetAttribute(() => header.ParentPath), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.ParentPath = TryCatch <string>(EndReadParentPath, machine.CompletionResult);

            BeginReadParentLocators(attributeHelper.GetAttribute(() => header.ParentLocators), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.ParentLocators = TryCatch <IList <ParentLocator> >(EndReadParentLocators, machine.CompletionResult);

            BeginReadRawData(attributeHelper.GetAttribute(() => header.RawData), machine.CompletionCallback, null);
            yield return(CompletionPort.SingleOperation);

            header.RawData = TryCatch <byte[]>(EndReadRawData, machine.CompletionResult);

            machine.ParameterValue = header;
        }
Esempio n. 32
0
 public IAsyncResult BeginReadUInt32(AsyncCallback callback, object state)
 {
     return(AsyncMachine.BeginAsyncMachine(FillBuffer, 4, callback, state));
 }