Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packet lost in Hid device read thread - windows #5

Open
adixmasz opened this issue Aug 31, 2018 · 0 comments
Open

Packet lost in Hid device read thread - windows #5

adixmasz opened this issue Aug 31, 2018 · 0 comments

Comments

@adixmasz
Copy link

In function:

  • void HidDevice::HidDeviceReaderThread::onStartHandler()
    Data will be lost if arrive beetween CloseHandle() and ReadFile() call.
void HidDevice::HidDeviceReaderThread::onStartHandler()
{
    if( this->device != NULL )
    {
        std::string tempBuf;

        #ifdef OS_WINDOWS

            WaitForSingleObject( this->runningMutex, INFINITE);
            this->running = true;
            ReleaseMutex(this->runningMutex);

            DWORD       bytesRead;
            OVERLAPPED  readOL;

            while(this->running)
            {
                WaitForSingleObject( this->runningMutex, INFINITE);
                if( this->device->isInitialized() and this->device->isOpened() )
                {
                    memset(&(readOL), 0, sizeof(readOL));
                    readOL.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
                    bytesRead     = 0;
                    tempBuf       = "";
                    tempBuf.resize(this->device->internalReadBufferSize, 0);

                    ResetEvent(readOL.hEvent);

                    if( !ReadFile(this->device->devHandle, &(tempBuf[0]), tempBuf.size(), &bytesRead, &readOL) )
                    {
                        if (GetLastError() != ERROR_IO_PENDING)
                        {
                            CancelIo(this->device->devHandle);
                            CloseHandle(readOL.hEvent);
                            ReleaseMutex(this->runningMutex);
                            msleep(1);
                            continue;
                        }
                    }

                    if( WaitForSingleObject(readOL.hEvent, POLLING_TIME_MS) != WAIT_OBJECT_0 )
                    {
                        CancelIo(this->device->devHandle);
                        CloseHandle(readOL.hEvent);
                        ReleaseMutex(this->runningMutex);
                        msleep(DEVICE_READ_INTERVAL_MS);
                        continue;
                    }

                    if( GetOverlappedResult(this->device->devHandle, &readOL, &bytesRead, TRUE) )
                    {
                        if( bytesRead > 0 ) this->device->readFifoBuffer.push(tempBuf);
                    }

                    CloseHandle(readOL.hEvent);
                }
                ReleaseMutex(this->runningMutex);
                msleep(DEVICE_READ_INTERVAL_MS);
            }
#endif

Work around (works for me):

void HidDevice::HidDeviceReaderThread::onStartHandler()
{
    if( this->device != NULL )
    {
        std::string tempBuf;

        #ifdef OS_WINDOWS
            static bool isReadRoutineInit = false;
            WaitForSingleObject( this->runningMutex, INFINITE);
            this->running = true;
            ReleaseMutex(this->runningMutex);

            DWORD       bytesRead;
            OVERLAPPED  readOL;

            memset(&(readOL), 0, sizeof(readOL));
            readOL.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
            bytesRead     = 0;
            tempBuf       = "";
            tempBuf.resize(this->device->internalReadBufferSize, 0);

            while(this->running)
            {
                WaitForSingleObject( this->runningMutex, INFINITE);
                if( this->device->isInitialized() and this->device->isOpened() )
                {
                    if(isReadRoutineInit == false)
                    {
                        isReadRoutineInit = true;
                        ReadFile(this->device->devHandle, &(tempBuf[0]), tempBuf.size(), &bytesRead, &readOL);
                        ResetEvent(readOL.hEvent);
                    }
                    else if (GetLastError() != ERROR_IO_PENDING)
                    {
                        CancelIo(this->device->devHandle);
                        ReadFile(this->device->devHandle, &(tempBuf[0]), tempBuf.size(), &bytesRead, &readOL);
                        ResetEvent(readOL.hEvent);
                    }
                    else if( !WaitForSingleObject(readOL.hEvent, POLLING_TIME_MS) != WAIT_OBJECT_0 )
                    {
                        if( GetOverlappedResult(this->device->devHandle, &readOL, &bytesRead, TRUE) )
                        {
                            if( bytesRead > 0 ) 
                                this->device->readFifoBuffer.push(tempBuf);

                            CancelIo(this->device->devHandle);
                            ReadFile(this->device->devHandle, &(tempBuf[0]), tempBuf.size(), &bytesRead, &readOL);
                            ResetEvent(readOL.hEvent);
                        }
                    }
                }
                else
                {
                    if(isReadRoutineInit == true)
                        isReadRoutineInit = false;
                }
                ReleaseMutex(this->runningMutex);
                msleep(DEVICE_READ_INTERVAL_MS);
            }

            CloseHandle(readOL.hEvent);
        #endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant