public unsafe void LockNLoad(ref int j, byte[][] cBufs, byte[][] xBufs, byte[][] oLaps, ISO_PKT_INFO[][] pktsInfo) { cBufs[j] = new byte[CyConst.SINGLE_XFER_LEN]; xBufs[j] = new byte[BufSz]; oLaps[j] = new byte[20]; pktsInfo[j] = new ISO_PKT_INFO[M280DEF.Packet_Xfer]; fixed(byte *tL0 = oLaps[j], tc0 = cBufs[j], tb0 = xBufs[j]) { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tL0; ovLapStatus->hEvent = (IntPtr)PInvoke.CreateEvent(0, 0, 0, 0); int len = BufSz; inEndpoint.BeginDataXfer(ref cBufs[j], ref xBufs[j], ref len, ref oLaps[j]); j++; if (j < QueueSz) { LockNLoad(ref j, cBufs, xBufs, oLaps, pktsInfo); } else { XferData(cBufs, xBufs, oLaps, pktsInfo); } } }
public static extern BOOL ReadFile( HANDLE hFile, byte *lpBuffer, DWORD nNumberOfBytesToRead, DWORD *lpNumberOfBytesRead, OVERLAPPED *lpOverlapped );
public static extern BOOL WriteFile( HANDLE hFile, byte *lpBuffer, DWORD nNumberOfBytesToWrite, DWORD *lpNumberOfBytesWritten, OVERLAPPED *lpOverlapped );
/*Summary * This is a recursive routine for pinning all the buffers used in the transfer in memory. * It will get recursively called QueueSz times. On the QueueSz_th call, it will call * XferData, which will loop, transferring data, until the stop button is clicked. * Then, the recursion will unwind. */ public unsafe void LockNLoad(ref int j, byte[][] cBufs, byte[][] xBufs, byte[][] oLaps, ISO_PKT_INFO[][] pktsInfo) { // Allocate one set of buffers for the queue. Buffered IO method require user to allocate a buffer as a part of command buffer, // the BeginDataXfer does not allocated it. BeginDataXfer will copy the data from the main buffer to the allocated while initializing the commands. cBufs[j] = new byte[CyConst.SINGLE_XFER_LEN + IsoPktBlockSize + ((EndPoint.XferMode == XMODE.BUFFERED) ? BufSz : 0)]; xBufs[j] = new byte[BufSz]; oLaps[j] = new byte[20]; pktsInfo[j] = new ISO_PKT_INFO[PPX]; fixed(byte *tL0 = oLaps[j], tc0 = cBufs[j], tb0 = xBufs[j]) // Pin the buffers in memory { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tL0; ovLapStatus->hEvent = (IntPtr)PInvoke.CreateEvent(0, 0, 0, 0); // Pre-load the queue with a request int len = BufSz; EndPoint.BeginDataXfer(ref cBufs[j], ref xBufs[j], ref len, ref oLaps[j]); j++; if (j < QueueSz) { LockNLoad(ref j, cBufs, xBufs, oLaps, pktsInfo); // Recursive call to pin next buffers in memory } else { XferData(cBufs, xBufs, oLaps, pktsInfo); // All loaded. Let's go! } } }
public unsafe bool PreReadAsysnchonous() { cBufs = new byte[CyConst.SINGLE_XFER_LEN + IsoPktBlockSize + ((InEndpoint.XferMode == XMODE.BUFFERED) ? BufSz : 0)]; xBufs = new byte[BufSz]; oLaps = new byte[20]; pktsInfo = new ISO_PKT_INFO[PPX]; fixed(byte *tL0 = oLaps, tc0 = cBufs, tb0 = xBufs) { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tL0; ovLapStatus->hEvent = (IntPtr)PInvoke.CreateEvent(0, 0, 0, 0); // Pre-load the queue with a request int len = BufSz; if (InEndpoint.BeginDataXfer(ref cBufs, ref xBufs, ref len, ref oLaps) == false) { LogHelper.GetLogger <USB>().Debug("Begin data xfer failure"); } if (!InEndpoint.WaitForXfer(ovLapStatus->hEvent, 500)) { InEndpoint.Abort(); PInvoke.WaitForSingleObject(ovLapStatus->hEvent, 500); } } return(true); }
// Call this one if you want the PacketInfo data passed-back public unsafe bool XferData(ref byte[] buf, ref int len, ref ISO_PKT_INFO[] pktInfos) { byte[] ovLap = new byte[OverlapSignalAllocSize]; fixed(byte *tmp0 = ovLap) { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tmp0; ovLapStatus->hEvent = PInvoke.CreateEvent(0, 0, 0, 0); // This SINGLE_TRANSFER buffer must be allocated at this level. int bufSz = CyConst.SINGLE_XFER_LEN + GetPktBlockSize(len) + ((XferMode == XMODE.DIRECT) ? 0 : len); byte[] cmdBuf = new byte[bufSz]; fixed(byte *tmp1 = cmdBuf, tmp2 = buf) { bool bResult = BeginDataXfer(ref cmdBuf, ref buf, ref len, ref ovLap); bool wResult = WaitForIO(ovLapStatus->hEvent); bool fResult = FinishDataXfer(ref cmdBuf, ref buf, ref len, ref ovLap, ref pktInfos); PInvoke.CloseHandle(ovLapStatus->hEvent); return(wResult && fResult); } } }
private unsafe void LockNLoad(ref int j, byte[][] cBufs, byte[][] xBufs, byte[][] oLaps) { // Allocate one set of buffers for the queue. Buffered IO method require user to allocate a buffer as a part of command buffer, // the BeginDataXfer does not allocated it. BeginDataXfer will copy the data from the main buffer to the allocated while initializing the commands. cBufs[j] = new byte[CyConst.SINGLE_XFER_LEN + BufSz]; xBufs[j] = new byte[BufSz]; oLaps[j] = new byte[CyConst.OverlapSignalAllocSize]; fixed(byte *tL0 = oLaps[j], tc0 = cBufs[j], tb0 = xBufs[j]) // Pin the buffers in memory { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tL0; ovLapStatus->hEvent = (IntPtr)PInvoke.CreateEvent(0, 0, 0, 0); // Pre-load the queue with a request int len = BufSz; bulkInEndPoint.BeginDataXfer(ref cBufs[j], ref xBufs[j], ref len, ref oLaps[j]); j++; if (j < QueueSz) { LockNLoad(ref j, cBufs, xBufs, oLaps); // Recursive call to pin next buffers in memory } else { XferData(cBufs, xBufs, oLaps); // All loaded. Let's go! } } }
private unsafe void LockNLoad(ref int j, byte[][] cBufs, byte[][] xBufs, byte[][] oLaps, ISO_PKT_INFO[][] pktsInfo) { CyIsocEndPoint InEndpt = usbdevice.IsocInEndPt; cBufs[j] = new byte[CyConst.SINGLE_XFER_LEN + bulkendpoint.MaxPktSize + ((bulkendpoint.XferMode == XMODE.BUFFERED) ? buffersize : 0)]; xBufs[j] = new byte[buffersize]; oLaps[j] = new byte[20]; pktsInfo[j] = new ISO_PKT_INFO[M280DEF.Packet_Xfer]; fixed(byte *tL0 = oLaps[j], tc0 = cBufs[j], tb0 = xBufs[j]) { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tL0; ovLapStatus->hEvent = PInvoke.CreateEvent(0, 0, 0, 0); int len = buffersize; bulkendpoint.BeginDataXfer(ref cBufs[j], ref xBufs[j], ref len, ref oLaps[j]); j++; if (j < queuesize) { LockNLoad(ref j, cBufs, xBufs, oLaps, pktsInfo); } else { XferData(cBufs, xBufs, oLaps, pktsInfo); } } }
/*Summary * This is a recursive routine for pinning all the buffers used in the transfer in memory. * It will get recursively called QueueSz times. On the QueueSz_th call, it will call * XferData, which will loop, transferring data. */ public unsafe void LockNLoad(ref int j, byte[][] cBufs, byte[][] xBufs, byte[][] oLaps, ISO_PKT_INFO[][] pktsInfo) { // Allocate one set of buffers for the queue cBufs[j] = new byte[CyConst.SINGLE_XFER_LEN]; xBufs[j] = new byte[BufSz]; oLaps[j] = new byte[20]; //pktsInfo[j] = new ISO_PKT_INFO[PPX]; pktsInfo[j] = new ISO_PKT_INFO[M280DEF.Packet_Xfer]; fixed(byte *tL0 = oLaps[j], tc0 = cBufs[j], tb0 = xBufs[j]) // Pin the buffers in memory { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tL0; ovLapStatus->hEvent = (IntPtr)PInvoke.CreateEvent(0, 0, 0, 0); // Pre-load the queue with a request int len = BufSz; inEndpoint.BeginDataXfer(ref cBufs[j], ref xBufs[j], ref len, ref oLaps[j]); j++; if (j < QueueSz) { LockNLoad(ref j, cBufs, xBufs, oLaps, pktsInfo); // Recursive call to pin next buffers in memory } else { XferData(cBufs, xBufs, oLaps, pktsInfo); // All loaded. Let's go! } } }
// These used by both BULK and INTERRUPT endpoints public unsafe virtual bool XferData(ref byte[] buf, ref int len) { byte[] ovLap = new byte[OverlapSignalAllocSize]; fixed(byte *fixedOvLap = ovLap) { OVERLAPPED *ovLapStatus = (OVERLAPPED *)fixedOvLap; ovLapStatus->hEvent = PInvoke.CreateEvent(0, 0, 0, 0); // This SINGLE_TRANSFER buffer must be allocated at this level. int bufSz = CyConst.SINGLE_XFER_LEN + ((XferMode == XMODE.DIRECT) ? 0 : len); byte[] cmdBuf = new byte[bufSz]; // These nested fixed blocks ensure that the buffers don't move in memory // While we're doing the asynchronous IO - Begin/Wait/Finish fixed(byte *tmp1 = cmdBuf, tmp2 = buf) { bool bResult = BeginDataXfer(ref cmdBuf, ref buf, ref len, ref ovLap); // // This waits for driver to call IoRequestComplete on the IRP // we just sent. // bool wResult = WaitForIO(ovLapStatus->hEvent); bool fResult = FinishDataXfer(ref cmdBuf, ref buf, ref len, ref ovLap); PInvoke.CloseHandle(ovLapStatus->hEvent); return(wResult && fResult); } } }
public static unsafe extern bool DeviceIoControl( SafeHandle hDevice, ControlCode dwIoControlCode, void *lpInBuffer, uint nInBufferSize, void *lpOutBuffer, uint nOutBufferSize, out uint lpBytesReturned, OVERLAPPED *lpOverlapped);
public static extern unsafe bool DeviceIoControl( SafeObjectHandle hDevice, uint dwIoControlCode, IntPtr inBuffer, int nInBufferSize, IntPtr outBuffer, int nOutBufferSize, out int pBytesReturned, OVERLAPPED *lpOverlapped);
/*Summary * Called at the end of recursive method, LockNLoad(). * XferData() implements the infinite transfer loop */ public unsafe void XferData(byte[][] cBufs, byte[][] xBufs, byte[][] oLaps, ISO_PKT_INFO[][] pktsInfo) { int k = 0; int len = 0; int pDataBF = 0; Successes = 0; Failures = 0; XferBytes = 0; for (; bRunning;) { // WaitForXfer fixed(byte *tmpOvlap = oLaps[k]) { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tmpOvlap; if (!inEndpoint.WaitForXfer(ovLapStatus->hEvent, 500)) { inEndpoint.Abort(); PInvoke.WaitForSingleObject(ovLapStatus->hEvent, 500); } } // FinishDataXfer if (inEndpoint.FinishDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k])) { XferBytes += len; Successes++; Array.Copy(xBufs[k], 0, DataBuf[pDataBF], 0, len); pDataBF++; } else { Failures++; } k++; if (k == QueueSz) // Finish { k = 0; Thread.Sleep(1); if (Failures == 0) { // this.Invoke(Img_View); Img_View(); } else { // Scanner busy //MessageBox.Show("Scanner Busy! Please Try again."); } bRunning = false; } } }
/*Summary * This is a recursive routine for pinning all the buffers used in the transfer in memory. * It will get recursively called QueueSz times. On the QueueSz_th call, it will call * XferData, which will loop, transferring data, until the stop button is clicked. * Then, the recursion will unwind. */ public unsafe void LockNLoad(ref int j, byte[][] cBufs, byte[][] xBufs, byte[][] oLaps, ISO_PKT_INFO[][] pktsInfo) { // Allocate one set of buffers for the queue, Buffered IO method require user to allocate a buffer as a part of command buffer, // the BeginDataXfer does not allocated it. BeginDataXfer will copy the data from the main buffer to the allocated while initializing the commands. cBufs[j] = new byte[CyConst.SINGLE_XFER_LEN + IsoPktBlockSize + ((EndPoint.XferMode == XMODE.BUFFERED) ? BufSz : 0)]; xBufs[j] = new byte[BufSz]; //initialize the buffer with initial value 0xA5 for (int iIndex = 0; iIndex < BufSz; iIndex++) { xBufs[j][iIndex] = DefaultBufInitValue; } oLaps[j] = new byte[20]; pktsInfo[j] = new ISO_PKT_INFO[PPX]; fixed(byte *tL0 = oLaps[j], tc0 = cBufs[j], tb0 = xBufs[j]) // Pin the buffers in memory { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tL0; ovLapStatus->hEvent = (IntPtr)PInvoke.CreateEvent(0, 0, 0, 0); // Pre-load the queue with a request int len = BufSz; EndPoint.BeginDataXfer(ref cBufs[j], ref xBufs[j], ref len, ref oLaps[j]); //if (Save == true) //mady //{ // Buffer.BlockCopy(xBufs[j], 0, temp, j * PPX * EndPoint.MaxPktSize, PPX * EndPoint.MaxPktSize); //mady: Take Backup of received data to Temp Buffer //} j++; if (j < QueueSz) { LockNLoad(ref j, cBufs, xBufs, oLaps, pktsInfo); // Recursive call to pin next buffers in memory } else { XferData(cBufs, xBufs, oLaps, pktsInfo); } } }
// Control endpoint uses the BUFFERED xfer method. So that it // doesn't collide with the base class' XferData, we declare it 'new' public new unsafe bool XferData(ref byte[] buf, ref int len) { byte[] ovLap = new byte[sizeof(OVERLAPPED)]; fixed(byte *tmp0 = ovLap) { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tmp0; ovLapStatus->hEvent = PInvoke.CreateEvent(0, 0, 0, 0); bool bResult, wResult, fResult; // Create a temporary buffer that will contain a SINGLE_TRANSFER structure // followed by the actual data. byte[] tmpBuf = new byte[CyConst.SINGLE_XFER_LEN + len]; for (int i = 0; i < len; i++) { tmpBuf[CyConst.SINGLE_XFER_LEN + i] = buf[i]; } GCHandle bufSingleTransfer = GCHandle.Alloc(tmpBuf, GCHandleType.Pinned); GCHandle bufDataAllocation = GCHandle.Alloc(buf, GCHandleType.Pinned); fixed(int *lenTemp = &len) { bResult = BeginDataXfer(ref tmpBuf, ref *lenTemp, ref ovLap); wResult = WaitForIO(ovLapStatus->hEvent); fResult = FinishDataXfer(ref buf, ref tmpBuf, ref *lenTemp, ref ovLap); } PInvoke.CloseHandle(ovLapStatus->hEvent); bufSingleTransfer.Free(); bufDataAllocation.Free(); return(wResult && fResult); } }
public static extern int PostQueuedCompletionStatus([NativeTypeName("HANDLE")] IntPtr CompletionPort, [NativeTypeName("DWORD")] uint dwNumberOfBytesTransferred, [NativeTypeName("ULONG_PTR")] nuint dwCompletionKey, [NativeTypeName("LPOVERLAPPED")] OVERLAPPED *lpOverlapped);
private unsafe void XferData(byte[][] cBufs, byte[][] xBufs, byte[][] oLaps, ISO_PKT_INFO[][] pktsInfo) { int k = 0; int len = 0; int pDataBF = 0; successes = 0; failures = 0; transferbytes = 0; for (; running;) { fixed(byte *tmpOvlap = oLaps[k]) { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tmpOvlap; if (!bulkendpoint.WaitForXfer(ovLapStatus->hEvent, 500)) { bulkendpoint.Abort(); PInvoke.WaitForSingleObject(ovLapStatus->hEvent, 500); } } if (bulkendpoint.FinishDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k])) { transferbytes += len; successes++; Array.Copy(xBufs[k], 0, databuffer[pDataBF], 0, len); pDataBF++; } else { failures++; } // WIN10 UPDATE (VER:1803) --> // Re-submit this buffer into the queue len = buffersize; bulkendpoint.BeginDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k]); // <-- WIN10 UPDATE (VER:1803) k++; if (k == queuesize) { k = 0; Thread.Sleep(1); if (failures == 0) { dispatcher.Invoke(imageview); } else if (successes == 0) { len = 1024 * 4; byte[] buf = new byte[len]; bulkendpoint.Abort(); bulkendpoint.Reset(); bulkendpoint.XferData(ref buf, ref len); Thread.Sleep(100); } else { MessageBox.Show(@"Scanner is busy please wait a few seconds and try again!", "M280", MessageBoxButtons.OK, MessageBoxIcon.Stop); len = 1024 * 4; byte[] buf = new byte[len]; bulkendpoint.Abort(); bulkendpoint.Reset(); bulkendpoint.XferData(ref buf, ref len); Thread.Sleep(100); } Thread.Sleep(100); running = false; } } // WIN10 UPDATE (VER:1803) --> bulkendpoint.Abort(); release = true; // <-- WIN10 UPDATE (VER:1803) }
/*Summary * Called at the end of recursive method, LockNLoad(). * XferData() implements the infinite transfer loop */ public unsafe void XferData(byte[][] cBufs, byte[][] xBufs, byte[][] oLaps, ISO_PKT_INFO[][] pktsInfo) { int k = 0; int len = 0; Successes = 0; Failures = 0; XferBytes = 0; t1 = DateTime.Now; for (; bRunning;) { // WaitForXfer fixed(byte *tmpOvlap = oLaps[k]) { OVERLAPPED *ovLapStatus = (OVERLAPPED *)tmpOvlap; if (!EndPoint.WaitForXfer(ovLapStatus->hEvent, 500)) { EndPoint.Abort(); PInvoke.WaitForSingleObject(ovLapStatus->hEvent, 500); } } if (EndPoint.Attributes == 1) { CyIsocEndPoint isoc = EndPoint as CyIsocEndPoint; // FinishDataXfer if (isoc.FinishDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k], ref pktsInfo[k])) { //XferBytes += len; //Successes++; ISO_PKT_INFO[] pkts = pktsInfo[k]; for (int j = 0; j < PPX; j++) { if (pkts[j].Status == 0) { XferBytes += pkts[j].Length; Successes++; } else { Failures++; } pkts[j].Length = 0; } } else { Failures++; } } else { // FinishDataXfer if (EndPoint.FinishDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k])) { XferBytes += len; Successes++; } else { Failures++; } } // Re-submit this buffer into the queue len = BufSz; EndPoint.BeginDataXfer(ref cBufs[k], ref xBufs[k], ref len, ref oLaps[k]); k++; if (k == QueueSz) // Only update displayed stats once each time through the queue { k = 0; t2 = DateTime.Now; elapsed = t2 - t1; xferRate = (long)(XferBytes / elapsed.TotalMilliseconds); xferRate = xferRate / (int)100 * (int)100; // Call StatusUpdate() in the main thread this.Invoke(updateUI); // For small QueueSz or PPX, the loop is too tight for UI thread to ever get service. // Without this, app hangs in those scenarios. Thread.Sleep(1); } } // End infinite loop // Let's recall all the queued buffer and abort the end point. EndPoint.Abort(); }
public static extern unsafe bool WriteFile( SafeObjectHandle hFile, void *lpBuffer, int nNumberOfBytesToWrite, [Friendly(FriendlyFlags.Out | FriendlyFlags.Optional)] int *lpNumberOfBytesWritten, OVERLAPPED *lpOverlapped);
public static extern unsafe bool CancelIoEx( SafeObjectHandle hFile, OVERLAPPED *lpOverlapped);
public static extern int GetOverlappedResultEx([NativeTypeName("HANDLE")] IntPtr hFile, [NativeTypeName("LPOVERLAPPED")] OVERLAPPED *lpOverlapped, [NativeTypeName("LPDWORD")] uint *lpNumberOfBytesTransferred, [NativeTypeName("DWORD")] uint dwMilliseconds, [NativeTypeName("BOOL")] int bAlertable);
public static extern int CancelIoEx([NativeTypeName("HANDLE")] IntPtr hFile, [NativeTypeName("LPOVERLAPPED")] OVERLAPPED *lpOverlapped);
public static extern int GetOverlappedResult([NativeTypeName("HANDLE")] IntPtr hFile, [NativeTypeName("LPOVERLAPPED")] OVERLAPPED *lpOverlapped, [NativeTypeName("LPDWORD")] uint *lpNumberOfBytesTransferred, [NativeTypeName("BOOL")] int bWait);
static extern unsafe bool PostQueuedCompletionStatus(uint hCompletionPort, uint uiSizeOfArgument, uint *puiUserArg, OVERLAPPED *pOverlapped);
public unsafe static extern bool WaitCommEvent( SafeFileHandle hFile, out EventMask lpEvtMask, OVERLAPPED *lpOverlapped);
public static extern bool WriteFile(IntPtr fFile, byte *lpBuffer, int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, OVERLAPPED *lpOverlapped);
public static extern BOOL CancelIPChangeNotify([NativeTypeName("LPOVERLAPPED")] OVERLAPPED *notifyOverlapped);
public static extern uint NotifyRouteChange([NativeTypeName("PHANDLE")] HANDLE *Handle, [NativeTypeName("LPOVERLAPPED")] OVERLAPPED *overlapped);
public static extern bool GetOverlappedResult( IntPtr hFile, OVERLAPPED *lpOverlapped, ref int lpNumberOfBytesTransferred, [MarshalAs(UnmanagedType.Bool)] bool bWait);
public static extern int DeviceIoControl([NativeTypeName("HANDLE")] IntPtr hDevice, [NativeTypeName("DWORD")] uint dwIoControlCode, [NativeTypeName("LPVOID")] void *lpInBuffer, [NativeTypeName("DWORD")] uint nInBufferSize, [NativeTypeName("LPVOID")] void *lpOutBuffer, [NativeTypeName("DWORD")] uint nOutBufferSize, [NativeTypeName("LPDWORD")] uint *lpBytesReturned, [NativeTypeName("LPOVERLAPPED")] OVERLAPPED *lpOverlapped);