//------------------------------------------------------------------------------------------ //---------------------------------------------------------------------------------------- protected void DataEventThread() { Byte[] currBuff = new Byte[inputReportSize]; while (dataThreadActive) { if (readRing == null) { return; } if (callNever == false) { if (errCodeR != 0) { Array.Clear(currBuff, 0, inputReportSize); holdDataThreadOpen = true; registeredDataHandler.HandlePIEHidData(currBuff, this, errCodeR); holdDataThreadOpen = false; dataThreadActive = false; } else if (readRing.get(currBuff) == 0) { holdDataThreadOpen = true; registeredDataHandler.HandlePIEHidData(currBuff, this, 0); holdDataThreadOpen = false; } if (readRing.IsEmpty()) { FileIOApiDeclarations.ResetEvent(readEvent); } } // System.Threading.Thread.Sleep(10); FileIOApiDeclarations.WaitForSingleObject(readEvent, 100); } //while return; } //DataEventThread()
protected void ReadThread() { // FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttrUnused = new FileIOApiDeclarations.SECURITY_ATTRIBUTES(); IntPtr overlapEvent = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, ""); FileIOApiDeclarations.OVERLAPPED overlapped = new FileIOApiDeclarations.OVERLAPPED(); overlapped.Offset = 0; //IntPtr.Zero; overlapped.OffsetHigh = 0; overlapped.hEvent = (Int32)overlapEvent; overlapped.Internal = 0; //IntPtr.Zero; overlapped.InternalHigh = 0; //IntPtr.Zero; if (inputReportSize == 0) { errCodeR = 302; errCodeRE = 302; return; } errCodeR = 0; errCodeRE = 0; byte[] buffer = new byte[inputReportSize]; GCHandle gch = GCHandle.Alloc(buffer, GCHandleType.Pinned); //onur March 2009 - pinning is reuired while (readThreadActive) { int dataRead = 0; if (0 == FileIOApiDeclarations.ReadFile(readFileHandle, gch.AddrOfPinnedObject(), inputReportSize, ref dataRead, ref overlapped)) //ref readFileBuffer[0] { int result = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (result != FileIOApiDeclarations.ERROR_IO_PENDING) //|| result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED) { errCodeR = 307; errCodeRE = 307; goto EXit; } else //if (result != .ERROR_IO_PENDING) { // gch.Free(); //onur while (readThreadActive) { result = FileIOApiDeclarations.WaitForSingleObject(overlapEvent, 50); if (FileIOApiDeclarations.WAIT_OBJECT_0 == result) { if (0 == FileIOApiDeclarations.GetOverlappedResult(readFileHandle, ref overlapped, ref dataRead, 0)) { result = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (result == FileIOApiDeclarations.ERROR_INVALID_HANDLE || result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED) { errCodeR = 307; errCodeRE = 307; goto EXit; } } // buffer[0] = 89; goto ReadCompleted; } } //while } //if (result != .ERROR_IO_PENDING)...else continue; } //buffer[0] = 90; ReadCompleted: if (dataRead != inputReportSize) { errCodeR = 310; errCodeRE = 310; goto EXit; } if (suppressDuplicateReports) { int r = readRing.putIfDiff(buffer); if (r == 0) { FileIOApiDeclarations.SetEvent(readEvent); } } else { readRing.put(buffer); FileIOApiDeclarations.SetEvent(readEvent); } } //while EXit: FileIOApiDeclarations.CancelIo(readFileHandle); readFileHandle = null; gch.Free(); //onur return; }
//------------------------------------------------------------------------------------------- /// <summary> /// Write Thread /// </summary> protected void WriteThread() { // FileIOApiDeclarations.SECURITY_ATTRIBUTES securityAttrUnused = new FileIOApiDeclarations.SECURITY_ATTRIBUTES(); IntPtr overlapEvent = FileIOApiDeclarations.CreateEvent(ref securityAttrUnused, 1, 0, ""); FileIOApiDeclarations.OVERLAPPED overlapped = new FileIOApiDeclarations.OVERLAPPED(); overlapped.Offset = 0; //IntPtr.Zero; overlapped.OffsetHigh = 0; overlapped.hEvent = (Int32)overlapEvent; overlapped.Internal = 0; //IntPtr.Zero; overlapped.InternalHigh = 0; //IntPtr.Zero; if (outputReportSize == 0) { return; } byte[] buffer = new byte[outputReportSize]; GCHandle wgch = GCHandle.Alloc(buffer, GCHandleType.Pinned); //onur March 2009 - pinning is reuired int byteCount = 0;; // int loopCount = 0; errCodeW = 0; errCodeWE = 0; while (writeThreadActive) { if (writeRing == null) { errCodeW = 407; errCodeWE = 407; goto Error; } while (writeRing.get((byte[])buffer) == 0) { if (0 == FileIOApiDeclarations.WriteFile( writeFileHandle, wgch.AddrOfPinnedObject(), outputReportSize, ref byteCount, ref overlapped)) { int result = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (result != FileIOApiDeclarations.ERROR_IO_PENDING) // if ((result == FileIOApiDeclarations.ERROR_INVALID_HANDLE) || // (result == FileIOApiDeclarations.ERROR_DEVICE_NOT_CONNECTED)) { if (result == 87) { errCodeW = 412; errCodeWE = 412; goto Error; } errCodeW = 408; errCodeWE = 408; goto Error; } //if (result == else { // loopCount = 0; // while (writeThreadActive) // { result = FileIOApiDeclarations.WaitForSingleObject(overlapEvent, 1000); if (result == FileIOApiDeclarations.WAIT_OBJECT_0) { // errCodeWE=1400+loopCount; goto WriteCompleted; } // loopCount++; // if (loopCount > 10) // { errCodeW = 411; errCodeWE = 411; goto Error; // } /* if (0 == FileIOApiDeclarations.GetOverlappedResult(readFileHandle, ref overlapped, ref byteCount, 0)) * { * result = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); * // if (result == ERROR_INVALID_HANDLE || result == ERROR_DEVICE_NOT_CONNECTED) * if (result != FileIOApiDeclarations.ERROR_IO_INCOMPLETE) * { * errCodeW = 409; * errCodeWE = 409; * * goto Error; * }//if(result == ERROR_INVALID_HANDLE * }//if(!GetOverlappedResult*/ // }//while // goto Error; } //else if(result==ERROR_IO_PENDING){ // continue; } else { if ((long)byteCount != outputReportSize) { errCodeW = 410; errCodeWE = 410; } //iface->errCodeWE=1399; } WriteCompleted :; } //while(get== FileIOApiDeclarations.WaitForSingleObject(writeEvent, 100); FileIOApiDeclarations.ResetEvent(writeEvent); // System.Threading.Thread.Sleep(100); } Error: wgch.Free(); //onur return; }