예제 #1
0
        public unsafe void ReadAsync(ulong sourceAddress,
                                     IntPtr destinationAddress,
                                     uint readLength,
                                     IAsyncResult asyncResult)
        {
            Overlapped ov = new Overlapped
            {
                AsyncResult = asyncResult,
                OffsetLow   = unchecked ((int)(sourceAddress & 0xFFFFFFFF)),
                OffsetHigh  = unchecked ((int)((sourceAddress >> 32) & 0xFFFFFFFF))
            };

            NativeOverlapped *ovNative = ov.UnsafePack(null, IntPtr.Zero);

            /* Invoking the Native method ReadFile provided by Kernel32.dll
             * library. Returns false, if request failed or accepted for async
             * operation. Returns true, if success synchronously.
             */
            bool result = Native32.ReadFile(logHandle,
                                            destinationAddress,
                                            readLength,
                                            out uint bytesRead,
                                            ovNative);

            if (!result)
            {
                int error = Marshal.GetLastWin32Error();

                /* Just handle the case when it is not ERROR_IO_PENDING
                 * If ERROR_IO_PENDING, then it is accepted for async execution
                 */
                if (error != Native32.ERROR_IO_PENDING)
                {
                    Overlapped.Unpack(ovNative);
                    Overlapped.Free(ovNative);
                    throw new Exception("Error reading from log file: " + error);
                }
            }
            else
            {
                //executed synchronously, so process callback
                //callback(0, bytesRead, ovNative);
            }
        }
예제 #2
0
        public unsafe void ReadAsync(ulong sourceAddress,
                                     IntPtr destinationAddress,
                                     uint readLength,
                                     IOCompletionCallback callback,
                                     IAsyncResult asyncResult)
        {
            Overlapped        ov       = new Overlapped(0, 0, IntPtr.Zero, asyncResult);
            NativeOverlapped *ovNative = ov.UnsafePack(callback, IntPtr.Zero);

            ovNative->OffsetLow  = unchecked ((int)((ulong)sourceAddress & 0xFFFFFFFF));
            ovNative->OffsetHigh = unchecked ((int)(((ulong)sourceAddress >> 32) & 0xFFFFFFFF));

            uint bytesRead = default(uint);
            bool result    = Native32.ReadFile(logHandle,
                                               destinationAddress,
                                               readLength,
                                               out bytesRead,
                                               ovNative);

            if (!result)
            {
                int error = Marshal.GetLastWin32Error();
                if (error != Native32.ERROR_IO_PENDING)
                {
                    Overlapped.Unpack(ovNative);
                    Overlapped.Free(ovNative);

                    // NOTE: alignedDestinationAddress needs to be freed by whoever catches the exception
                    throw new Exception("Error reading from log file: " + error);
                }
            }
            else
            {
                // On synchronous completion, issue callback directly
                callback(0, bytesRead, ovNative);
            }
        }