public unsafe override int Read(byte[] buffer, int offset, int count) { Throw.If.OutOfRange(buffer, offset, count); uint bytesTransferred; IntPtr @event = NativeMethods.CreateManualResetEventOrThrow(); HandleAcquireIfOpenOrFail(); UpdateSettings(); try { fixed(byte *ptr = buffer) { var overlapped = stackalloc NativeOverlapped[1]; overlapped[0].EventHandle = @event; NativeMethods.OverlappedOperation(_handle, @event, ReadTimeout, _closeEventHandle, NativeMethods.ReadFile(_handle, ptr + offset, count, IntPtr.Zero, overlapped), overlapped, out bytesTransferred); return((int)bytesTransferred); } } finally { HandleRelease(); NativeMethods.CloseHandle(@event); } }
// Buffer needs to be big enough for the largest report, plus a byte // for the Report ID. public unsafe override int Read(byte[] buffer, int offset, int count) { Throw.If.OutOfRange(buffer, offset, count); uint bytesTransferred; IntPtr @event = NativeMethods.CreateManualResetEventOrThrow(); HandleAcquireIfOpenOrFail(); try { lock (_readSync) { int minIn = Device.GetMaxInputReportLength(); if (minIn <= 0) { throw new IOException("Can't read from this device."); } if (_readBuffer == null || _readBuffer.Length < Math.Max(count, minIn)) { Array.Resize(ref _readBuffer, Math.Max(count, minIn)); } fixed(byte *ptr = _readBuffer) { var overlapped = stackalloc NativeOverlapped[1]; overlapped[0].EventHandle = @event; NativeMethods.OverlappedOperation(_handle, @event, ReadTimeout, _closeEventHandle, NativeMethods.ReadFile(_handle, ptr, Math.Max(count, minIn), IntPtr.Zero, overlapped), overlapped, out bytesTransferred); if (count > (int)bytesTransferred) { count = (int)bytesTransferred; } Array.Copy(_readBuffer, 0, buffer, offset, count); return(count); } } } finally { HandleRelease(); NativeMethods.CloseHandle(@event); } }
// Buffer needs to be big enough for the largest report, plus a byte // for the Report ID. public unsafe override int Read(byte[] buffer, int offset, int count) { Throw.If.OutOfRange(buffer, offset, count); uint bytesTransferred; IntPtr @event = NativeMethods.CreateManualResetEventOrThrow(); HandleAcquireIfOpenOrFail(); try { lock (_readSync) { int maxIn = _device.MaxInputReportLength; Array.Resize(ref _readBuffer, maxIn); if (count > maxIn) { count = maxIn; } fixed(byte *ptr = _readBuffer) { var overlapped = stackalloc NativeOverlapped[1]; overlapped[0].EventHandle = @event; NativeMethods.OverlappedOperation(_handle, @event, ReadTimeout, _closeEventHandle, NativeMethods.ReadFile(_handle, ptr, maxIn, IntPtr.Zero, overlapped), overlapped, out bytesTransferred); if (count > (int)bytesTransferred) { count = (int)bytesTransferred; } Array.Copy(_readBuffer, 0, buffer, offset, count); return(count); } } } finally { HandleRelease(); NativeMethods.CloseHandle(@event); } }