bool irsdk_waitForDataReady(int timeOut, char *data)
{
#ifdef _MSC_VER
	_ASSERTE(timeOut >= 0);
#endif

	if(isInitialized || irsdk_startup())
	{
		// just to be sure, check before we sleep
		if(irsdk_getNewData(data))
			return true;

		// sleep till signaled
		WaitForSingleObject(hDataValidEvent, timeOut);

		// we woke up, so check for data
		if(irsdk_getNewData(data))
			return true;
		else
			return false;
	}

	// sleep if error
	if(timeOut > 0)
		Sleep(timeOut);

	return false;
}
bool irsdk_getNewData(char *data)
{
	if(isInitialized || irsdk_startup())
	{
#ifdef _MSC_VER
		_ASSERTE(NULL != pHeader);
#endif

		// if sim is not active, then no new data
		if(!(pHeader->status & irsdk_stConnected))
		{
			lastTickCount = INT_MAX;
			return false;
		}

		int latest = 0;
		for(int i=1; i<pHeader->numBuf; i++)
			if(pHeader->varBuf[latest].tickCount < pHeader->varBuf[i].tickCount)
			   latest = i;	

		// if newer than last recieved, than report new data
		if(lastTickCount < pHeader->varBuf[latest].tickCount)
		{
			// if asked to retrieve the data
			if(data)
			{
				// try twice to get the data out
				for(int count = 0; count < 2; count++)
				{
					int curTickCount =  pHeader->varBuf[latest].tickCount;
					memcpy(data, pSharedMem + pHeader->varBuf[latest].bufOffset, pHeader->bufLen);
					if(curTickCount ==  pHeader->varBuf[latest].tickCount)
					{
						lastTickCount = curTickCount;
						lastValidTime = time(NULL);
						return true;
					}
				}
				// if here, the data changed out from under us.
				return false;
			}
			else
			{
				lastTickCount =  pHeader->varBuf[latest].tickCount;
				lastValidTime = time(NULL);
				return true;
			}
		}
		// if older than last recieved, than reset, we probably disconnected
		else if(lastTickCount >  pHeader->varBuf[latest].tickCount)
		{
			lastTickCount =  pHeader->varBuf[latest].tickCount;
			return false;
		}
		// else the same, and nothing changed this tick
	}

	return false;
}
예제 #3
0
bool APIDataReceiver::getNewData(char *data) {
    if(initialized || irsdk_startup())
    {
        // if sim is not active, then no new data
        if(!(header->status & irsdk_stConnected))
        {
            lastTickCount = std::numeric_limits<int>::max();
            return false;
        }

        int latest = 0;
        for(int i = 1; i < header->numBuf; i++)
            if(header->varBuf[latest].tickCount < header->varBuf[i].tickCount)
                latest = i;

        // if newer than last recieved, than report new data
        if(lastTickCount < header->varBuf[latest].tickCount)
        {
            // if asked to retrieve the data
            if(data)
            {
                // try twice to get the data out
                for(int count = 0; count < 2; count++)
                {
                    int curTickCount =  header->varBuf[latest].tickCount;
                    memcpy(data, sharedMem + header->varBuf[latest].bufOffset, static_cast<size_t >(header->bufLen));
                    if(curTickCount ==  header->varBuf[latest].tickCount)
                    {
                        lastTickCount = curTickCount;
                        lastValidTime = time(nullptr);
                        return true;
                    }
                }
                // if here, the data changed out from under us.
                return false;
            }
            else
            {
                lastTickCount =  header->varBuf[latest].tickCount;
                lastValidTime = time(nullptr);
                return true;
            }
        }
            // if older than last recieved, than reset, we probably disconnected
        else if(lastTickCount >  header->varBuf[latest].tickCount)
        {
            lastTickCount =  header->varBuf[latest].tickCount;
            return false;
        }
        // else the same, and nothing changed this tick
    }

    return false;
}