Example #1
0
//////////////////////////////////////////////////////////////////////////////////////////
// Wait for data to arrive or a timeout to occur.
XsensResultValue Cmt1s::waitForData (const uint32_t maxLength,
							  uint8_t* data, uint32_t* length)
{
	CMT1LOG("L1: waitForData, mto=%u, length=%p\n",m_timeout,length);
	uint32_t timeout = m_timeout;

	uint32_t ln;
	if (length == NULL)
		length = &ln;
	uint32_t eTime = getTimeOfDay(NULL) + timeout;
	uint32_t newLength = 0;

	*length = 0;
	while ((*length < maxLength) && (getTimeOfDay() <= eTime))
	{
		readData(maxLength - *length, data + *length, &newLength);
		*length += newLength;
	}
	CMT1LOG("L1: waitForData result: read %u of %u bytes\n",length[0],maxLength);

	if (length[0] < maxLength)
		return (m_lastResult = XRV_TIMEOUT);
	else
		return (m_lastResult = XRV_OK);
}
Example #2
0
double Timer::getTimeSinceEpoch()
{
    // The timer period (reciprocal of the frequency.)
    static const double timerPeriod = getTimerPeriod();

#if defined(LOVE_LINUX)
    double mt;
    // Check for POSIX timers and monotonic clocks. If not supported, use the gettimeofday fallback.
#if _POSIX_TIMERS > 0 && defined(_POSIX_MONOTONIC_CLOCK) \
&& (defined(CLOCK_MONOTONIC_RAW) || defined(CLOCK_MONOTONIC))
    timespec t;
#ifdef CLOCK_MONOTONIC_RAW
    clockid_t clk_id = CLOCK_MONOTONIC_RAW;
#else
    clockid_t clk_id = CLOCK_MONOTONIC;
#endif
    if (clock_gettime(clk_id, &t) == 0)
        mt = (double) t.tv_sec + (double) t.tv_nsec / 1000000000.0;
    else
#endif
        mt = getTimeOfDay();
    return mt;
#elif defined(LOVE_MACOSX) || defined(LOVE_IOS)
    return (double) mach_absolute_time() * timerPeriod;
#elif defined(LOVE_WINDOWS)
    LARGE_INTEGER microTime;
    QueryPerformanceCounter(&microTime);
    return (double) microTime.QuadPart * timerPeriod;
#endif
}
Example #3
0
TimeStamp timeStampNow(void)
{
	TimeStamp ms;
	time_t s;
	ms = (TimeStamp) getTimeOfDay(NULL,&s);
	ms = (ms % 1000) + (((TimeStamp)s)*1000);

	return ms;
}
/*! \brief Returns the number of milliseconds that has elapsed since the constructor or the restart function were called.
Note that the timer wraps around after 24 hours
*/
uint32_t MillisecondTimer::millisecondsElapsed()
{
	uint32_t tnow = getTimeOfDay();

	if (tnow >= m_tstart)
		return (tnow - m_tstart);
	else // TimeOfDay has wrapped around:
		return (tnow - m_tstart + XSENS_MS_PER_DAY);
}
Example #5
0
void Profiler::positionDone()
{
    Function *f = determineCurrentFunction(currentScriptId.isEmpty() ? -1 : currentScriptId.top());
    if (!f || currentLine.isEmpty())
        return;
    QList<Action>::iterator i = qBinaryFind(f->actions.begin(), f->actions.end(), Action(currentLine.top()));
    if (i == f->actions.end())
        return;

    suseconds_t current = getTimeOfDay();
    Action *action = &f->actions[i - f->actions.begin()];
    action->cost += current - instTime;
    instTime = current;
}
Example #6
0
void Profiler::functionExit(qint64 /*scriptId*/, const QScriptValue &)
{
    positionDone();

    currentFunctionLine.pop();
    currentLine.pop();
    currentScriptId.pop();

    if (!currentFunction.isEmpty())
        currentFunction.pop();
    if (!currentFunction.isEmpty()) {
        useconds_t current = getTimeOfDay();
        currentFunction.top()->cost += current - funTime;
        funTime = current;
        currentFunction.top()->called++;
    }
}
void ServerEnvironment::saveMeta(const std::string &savedir)
{
	std::string path = savedir + "/env_meta.txt";

	// Open file and serialize
	std::ofstream os(path.c_str(), std::ios_base::binary);
	if(os.good() == false)
	{
		dstream<<"WARNING: ServerEnvironment::saveMeta(): Failed to open "
				<<path<<std::endl;
		throw SerializationError("Couldn't save env meta");
	}

	Settings args;
	args.setU64("game_time", m_game_time);
	args.setU64("time_of_day", getTimeOfDay());
	args.writeLines(os);
	os<<"EnvArgsEnd\n";
}
Example #8
0
void Profiler::positionChange(qint64 scriptId, int lineNumber, int /*columnNumber*/)
{
    positionDone();
    if (first) {
        first = false;
        instTime = getTimeOfDay();
    }
    //qDebug() << "step" << scriptId << lineNumber << objects[scriptId].code[lineNumber-1];
    Function *f = determineCurrentFunction(scriptId);
    if (!f) {
        qWarning() << "bug";
        return;
    }
    QList<Action>::iterator i = qBinaryFind(f->actions.begin(), f->actions.end(), Action(lineNumber));
    if (i == f->actions.end())
        f->actions.append(Action(lineNumber));
    currentLine.pop();
    currentLine.push(lineNumber);
}
Example #9
0
// write to a log file/screen/debug-stream
void CMTLOG(const char *str, ...)
{
	#ifdef _LOG_TO_STDOUT
		va_list ptr;
		va_start(ptr,str);
		vprintf(str,ptr);
	#else
	#ifdef _LOG_TO_DBVIEW
		char buf[2048];

		va_list ptr;
		va_start(ptr,str);
		vsprintf(buf,str,ptr);

		OutputDebugString(buf);
	#else
		if (debug_log_valid == 0)
		{
			debug_log_fp = fopen("debug_log_cmt.log","w");
			if (debug_log_fp != NULL)
				debug_log_valid = 1;
			else
				debug_log_valid = -1;
		}
		if (debug_log_valid == 1)
		{
			char buf[2048];

			va_list ptr;
			va_start(ptr,str);
			int32_t sz = vsprintf_s(buf,str,ptr);

			uint32_t nw = getTimeOfDay();
			fprintf(debug_log_fp,"%5u.%03u %s",nw/1000,nw%1000,buf);
			//fwrite(buf,1,sz,debug_log_fp);
			fflush(debug_log_fp);
		}
	#endif
	#endif
}
Example #10
0
BOOL
deleteEntryOnLivedoorClip( char *cookie, const MyClipEx *mp )
{
    BOOL    ret = FALSE;
    char    *request;
    char    *response;
    char    url[MAX_URLLENGTH];
    size_t  sz;

    request = (char *)malloc( MAX_CONTENT_SIZE );
    if ( !request )
        return ( ret );

    sz = MAX_CONTENT_SIZE * 20;
    response = (char *)malloc( sz );
    if ( !response ) {
        free( request );
        return ( ret );
    }
    memset( request,  0x00, MAX_CONTENT_SIZE );

    strcpy( url, "http://clip.livedoor.com/clip/delete" );
    sprintf( request,
             "hash=%s&"
             "postkey=%s&"
             "rand=%.f",
             mp->entryID, mp->remarks,
             getTimeOfDay() );

    setUpReceiveBuffer( response, sz );
    http_postEx( url, "application/x-www-form-urlencoded",
                 request, response, cookie );
    if ( *response )
        ret = TRUE;

    free( response );
    free( request );

    return ( ret );
}
Example #11
0
//////////////////////////////////////////////////////////////////////////////////////////
// Wait for a message to arrive.
XsensResultValue Cmt2s::waitForMessage(
	Message* rcv, const uint8_t msgId, uint32_t timeoutOverride,
	bool acceptErrorMessage)
{
	auto* hdr = (MessageHeader*)m_readBuffer;
	uint16_t pre = 0;
	uint32_t length = 0;
	uint32_t target;
	uint16_t i;
	bool extended;
	bool readsome = (m_readBufferCount > 0);
	uint32_t toRestore = m_toEnd;

	CMT2LOG(
		"L2: waitForMessage x%02x, TO=%u, TOend=%u, TOO=%u\n", (uint32_t)msgId,
		m_timeout, m_toEnd, timeoutOverride);

	// The end-time may be misinterpreted around midnight, where it may be
	// considered
	// expired even if this is not the case. However, this is extremely rare.
	if (m_toEnd == 0)
	{
		if (timeoutOverride != 0)
			m_toEnd = (getTimeOfDay() + (uint32_t)timeoutOverride) %
					  (XSENS_MS_PER_DAY);
		else
			m_toEnd =
				(getTimeOfDay() + (uint32_t)m_timeout) % (XSENS_MS_PER_DAY);
		if (m_toEnd == 0) m_toEnd = 1;
	}

	if (m_readBufferCount == 0)
		m_readBuffer[0] = (uint8_t)~CMT_PREAMBLE;  // create a value that is
	// definitely NOT a preamble

	do
	{
		// find preamble
		while (m_readBuffer[pre] != CMT_PREAMBLE)
		{
			if ((++pre) >= m_readBufferCount)
			{
				m_readBufferCount = 0;
				pre = 0;
				if (m_toEnd >= getTimeOfDay())
				{
					m_lastResult = m_cmt1s.readData(
						CMT_LEN_MSGHEADER, m_readBuffer, &length);
					m_readBufferCount = (uint16_t)length;
					if (m_readBufferCount > 0) readsome = true;
				}
			}
			if (m_toEnd < getTimeOfDay()) break;
		}
		// shift buffer to start
		if (pre)
		{
			m_readBufferCount -= pre;
			for (i = 0; i < m_readBufferCount; ++i)
			{
				m_readBuffer[i] = m_readBuffer[i + pre];
			}
		}

		pre = 1;  // make sure we skip the first item in the next iteration
		// read header
		while (m_readBufferCount < CMT_LEN_MSGHEADERCS &&
			   (m_toEnd >= getTimeOfDay()))
		{
			m_cmt1s.readData(
				CMT_LEN_MSGHEADERCS - m_readBufferCount,
				&m_readBuffer[m_readBufferCount], &length);
			m_readBufferCount += (uint16_t)length;
		}
		if ((m_readBufferCount < CMT_LEN_MSGHEADERCS) &&
			(m_toEnd < getTimeOfDay()))
		{
			CMT2LOG(
				"L2: waitForMessage timeout occurred trying to read header\n");
			break;
		}

		if (hdr->m_length == CMT_EXTLENCODE)
		{
			extended = true;
			while (m_readBufferCount < CMT_LEN_MSGEXTHEADERCS &&
				   (m_toEnd >= getTimeOfDay()))
			{
				m_cmt1s.readData(
					CMT_LEN_MSGEXTHEADERCS - m_readBufferCount,
					&m_readBuffer[m_readBufferCount], &length);
				m_readBufferCount += (uint16_t)length;
			}
			if ((m_readBufferCount < CMT_LEN_MSGEXTHEADERCS) &&
				(m_toEnd < getTimeOfDay()))
			{
				CMT2LOG(
					"L2: waitForMessage timeout occurred trying to read "
					"extended header\n");
				break;
			}
		}
		else
			extended = false;

		// check the reported size
		if (extended &&
			(((uint16_t)hdr->m_datlen.m_extended.m_length.m_high * 256 +
			  (uint16_t)hdr->m_datlen.m_extended.m_length.m_low) >
			 (uint16_t)CMT_MAXDATALEN))
			continue;

		// header seems to be ok, read until end and check checksum
		if (extended)
			target = ((uint16_t)hdr->m_datlen.m_extended.m_length.m_high * 256 +
					  (uint16_t)hdr->m_datlen.m_extended.m_length.m_low) +
					 CMT_LEN_MSGEXTHEADERCS;
		else
			target = hdr->m_length + CMT_LEN_MSGHEADERCS;

		// read the entire message
		while ((m_readBufferCount < target) && (m_toEnd >= getTimeOfDay()))
		{
			m_cmt1s.readData(
				target - m_readBufferCount, &m_readBuffer[m_readBufferCount],
				&length);
			m_readBufferCount += (uint16_t)length;
		}
		if ((m_readBufferCount < target) && (m_toEnd < getTimeOfDay()))
		{
			CMT2LOG("L2: waitForMessage timeout occurred\n");
			break;
		}

		// check the checksum
		// msg=new Message(m_readBuffer,(uint16_t) target, (uint16_t) target);
		if (rcv->loadFromString(m_readBuffer, (uint16_t)target) == XRV_OK)
		{
			CMT2LOG(
				"L2: waitForMessage received msg Id x%02x while expecting "
				"x%02x, msg size=%u\n",
				(uint32_t)rcv->getMessageId(), (uint32_t)msgId, target);
			if (m_onMessageReceived != nullptr)
			{
				auto* bytes = (CmtBinaryData*)malloc(sizeof(CmtBinaryData));
				bytes->m_size = target;
				bytes->m_portNr = m_cmt1s.getPortNr();
				//				bytes->m_type = CMT_CALLBACK_ONMESSAGERECEIVED;
				memcpy(bytes->m_data, m_readBuffer, target);
#ifdef _LOG_CALLBACKS
				CMTLOG(
					"C2: m_onMessageReceived(%d,(%d,%d),%p)\n",
					(int32_t)m_onMessageReceivedInstance,
					(int32_t)bytes->m_size, (int32_t)bytes->m_portNr,
					m_onMessageReceivedParam);
#endif
				m_onMessageReceived(
					m_onMessageReceivedInstance, CMT_CALLBACK_ONMESSAGERECEIVED,
					bytes, m_onMessageReceivedParam);
			}

			m_readBufferCount -= (uint16_t)target;
			if (m_readBufferCount)
			{
				for (i = 0; i < m_readBufferCount; ++i)
				{
					m_readBuffer[i] = m_readBuffer[i + target];
				}
			}

			if ((msgId == 0) || (msgId == rcv->getMessageId()) ||
				(acceptErrorMessage && rcv->getMessageId() == CMT_MID_ERROR))
			{
				m_toEnd = toRestore;
				return m_lastResult = XRV_OK;
			}
		}
		else
		{
			rcv->clear();
			CMT2LOG("L2: waitForMessage load from string failed\n");
		}
	} while (m_toEnd >= getTimeOfDay());

	// a timeout occurred
	if (readsome)
	{
		// check if the current data contains a valid message
		int32_t pos = findValidMessage(m_readBuffer, m_readBufferCount);

		if (pos != -1)
		{
			CMT2LOG("L2: waitForMessage found message in message\n");
			// shift data to start of buffer
			pre = (uint16_t)pos;
			m_readBufferCount -= pre;
			for (i = 0; i < m_readBufferCount; ++i)
			{
				m_readBuffer[i] = m_readBuffer[i + pre];
			}
			waitForMessage(
				rcv, msgId, 0, acceptErrorMessage);  // parse the message
			m_toEnd = toRestore;
			return m_lastResult;  // set by waitForMessage
		}

		m_lastResult = XRV_TIMEOUT;
	}
	else
		m_lastResult = XRV_TIMEOUTNODATA;
	m_toEnd = toRestore;
	return m_lastResult;
}