OverlappedOperation() 공개 정적인 메소드

public static OverlappedOperation ( IntPtr ioHandle, IntPtr eventHandle, int eventTimeout, IntPtr closeEventHandle, bool overlapResult, NativeOverlapped overlapped, uint &bytesTransferred ) : void
ioHandle System.IntPtr
eventHandle System.IntPtr
eventTimeout int
closeEventHandle System.IntPtr
overlapResult bool
overlapped System.Threading.NativeOverlapped
bytesTransferred uint
리턴 void
예제 #1
0
        public unsafe override void Write(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, WriteTimeout, _closeEventHandle,
                                                      NativeMethods.WriteFile(_handle, ptr + offset, count, IntPtr.Zero, overlapped),
                                                      overlapped, out bytesTransferred);
                    if (bytesTransferred != count)
                    {
                        throw new IOException("Write failed.");
                    }
                }
            }
            finally
            {
                HandleRelease();
                NativeMethods.CloseHandle(@event);
            }
        }
예제 #2
0
        public unsafe override void Write(byte[] buffer, int offset, int count)
        {
            Throw.If.OutOfRange(buffer, offset, count); uint bytesTransferred;
            IntPtr @event = NativeMethods.CreateManualResetEventOrThrow();

            HandleAcquireIfOpenOrFail();
            try
            {
                lock (_writeSync)
                {
                    int minOut = Device.GetMaxOutputReportLength();
                    if (minOut <= 0)
                    {
                        throw new IOException("Can't write to this device.");
                    }
                    if (_writeBuffer == null || _writeBuffer.Length < Math.Max(count, minOut))
                    {
                        Array.Resize(ref _writeBuffer, Math.Max(count, minOut));
                    }
                    Array.Copy(buffer, offset, _writeBuffer, 0, count);

                    if (count < minOut)
                    {
                        Array.Clear(_writeBuffer, count, minOut - count);
                        count = minOut;
                    }

                    fixed(byte *ptr = _writeBuffer)
                    {
                        int offset0 = 0;

                        while (count > 0)
                        {
                            var overlapped = stackalloc NativeOverlapped[1];
                            overlapped[0].EventHandle = @event;

                            NativeMethods.OverlappedOperation(_handle, @event, WriteTimeout, _closeEventHandle,
                                                              NativeMethods.WriteFile(_handle, ptr + offset0, Math.Min(minOut, count), IntPtr.Zero, overlapped),
                                                              overlapped, out bytesTransferred);
                            count -= (int)bytesTransferred; offset0 += (int)bytesTransferred;
                        }
                    }
                }
            }
            finally
            {
                HandleRelease();
                NativeMethods.CloseHandle(@event);
            }
        }
예제 #3
0
        // 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);
            }
        }
예제 #4
0
        // 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);
            }
        }
예제 #5
0
        public unsafe override void Write(byte[] buffer, int offset, int count)
        {
            Throw.If.OutOfRange(buffer, offset, count); uint bytesTransferred;
            IntPtr @event = NativeMethods.CreateManualResetEventOrThrow();

            HandleAcquireIfOpenOrFail();
            try
            {
                lock (_writeSync)
                {
                    int maxOut = _device.MaxOutputReportLength;
                    Array.Resize(ref _writeBuffer, maxOut); if (count > maxOut)
                    {
                        count = maxOut;
                    }
                    Array.Copy(buffer, offset, _writeBuffer, 0, count); count = maxOut;

                    fixed(byte *ptr = _writeBuffer)
                    {
                        int offset0 = 0;

                        while (count > 0)
                        {
                            var overlapped = stackalloc NativeOverlapped[1];
                            overlapped[0].EventHandle = @event;

                            NativeMethods.OverlappedOperation(_handle, @event, WriteTimeout, _closeEventHandle,
                                                              NativeMethods.WriteFile(_handle, ptr + offset0, count, IntPtr.Zero, overlapped),
                                                              overlapped, out bytesTransferred);
                            count -= (int)bytesTransferred; offset0 += (int)bytesTransferred;
                        }
                    }
                }
            }
            finally
            {
                HandleRelease();
                NativeMethods.CloseHandle(@event);
            }
        }