/// <summary>
        ///
        /// </summary>
        /// <param name="segmentId"></param>
        /// <param name="sourceAddress"></param>
        /// <param name="destinationAddress"></param>
        /// <param name="readLength"></param>
        /// <param name="callback"></param>
        /// <param name="asyncResult"></param>
        public override unsafe void ReadAsync(int segmentId, ulong sourceAddress,
                                              IntPtr destinationAddress,
                                              uint readLength,
                                              IOCompletionCallback callback,
                                              IAsyncResult asyncResult)
        {
            ReadCallbackWrapper wrapper = null;
            SectorAlignedMemory memory  = null;
            Stream logHandle            = null;

            try
            {
                memory    = pool.Get((int)readLength);
                logHandle = GetOrAddHandle(segmentId);
                wrapper   = new ReadCallbackWrapper(logHandle, callback, asyncResult, memory, destinationAddress, readLength, 0);
                logHandle.Seek((long)sourceAddress, SeekOrigin.Begin);
                logHandle.BeginRead(memory.buffer, 0, (int)readLength, wrapper.Callback, null);
            }
            catch (IOException e)
            {
                wrapper = new ReadCallbackWrapper(logHandle, callback, asyncResult, memory, destinationAddress, readLength, (uint)(e.HResult & 0x0000FFFF));
                wrapper.Callback(asyncResult);
            }
            catch
            {
                wrapper = new ReadCallbackWrapper(logHandle, callback, asyncResult, memory, destinationAddress, readLength, uint.MaxValue);
                wrapper.Callback(asyncResult);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="segmentId"></param>
        /// <param name="sourceAddress"></param>
        /// <param name="destinationAddress"></param>
        /// <param name="readLength"></param>
        /// <param name="callback"></param>
        /// <param name="asyncResult"></param>
        public override unsafe void ReadAsync(int segmentId, ulong sourceAddress,
                                              IntPtr destinationAddress,
                                              uint readLength,
                                              IOCompletionCallback callback,
                                              IAsyncResult asyncResult)
        {
            var logHandle = GetOrAddHandle(segmentId);
            var memory    = pool.Get((int)readLength);

            logHandle.Seek((long)sourceAddress, SeekOrigin.Begin);
            logHandle.BeginRead(memory.buffer, 0, (int)readLength,
                                new ReadCallbackWrapper(logHandle, callback, asyncResult, memory, destinationAddress, readLength).Callback, null);
        }
Example #3
0
        public unsafe VarLenHeapContainer(ref T obj, IVariableLengthStruct <T> varLenStruct, SectorAlignedBufferPool pool)
        {
            var len = varLenStruct.GetLength(ref obj);

            mem = pool.Get(len);
            Buffer.MemoryCopy(Unsafe.AsPointer(ref obj), mem.GetValidPointer(), len, len);
        }
        /// <summary>
        /// Note: will read potentially more data (based on sector alignment)
        /// </summary>
        /// <param name="device"></param>
        /// <param name="address"></param>
        /// <param name="buffer"></param>
        /// <param name="size"></param>
        private unsafe void ReadInto(IDevice device, ulong address, out byte[] buffer, int size)
        {
            if (bufferPool == null)
            {
                bufferPool = new SectorAlignedBufferPool(1, (int)device.SectorSize);
            }

            long numBytesToRead = size;

            numBytesToRead = ((numBytesToRead + (device.SectorSize - 1)) & ~(device.SectorSize - 1));

            var pbuffer = bufferPool.Get((int)numBytesToRead);

            device.ReadAsync(address, (IntPtr)pbuffer.aligned_pointer,
                             (uint)numBytesToRead, IOCallback, null);
            semaphore.Wait();

            buffer = new byte[numBytesToRead];

            fixed(byte *bufferRaw = buffer)
            Buffer.MemoryCopy(pbuffer.aligned_pointer, bufferRaw, numBytesToRead, numBytesToRead);

            pbuffer.Return();
        }