/* if we're mapped, everything is fine... check back in 20 minutes
* to renew the port forwarding if it's expired */
s->doPortCheck = TRUE;
- interval.tv_sec = 60*20;
+ tr_timevalSet( &interval, 60*20, 0 );
break;
case TR_PORT_ERROR:
/* some kind of an error. wait 60 seconds and retry */
- interval.tv_sec = 60;
+ tr_timevalSet( &interval, 60, 0 );
break;
default:
/* in progress. pulse frequently. */
- interval.tv_sec = 0;
- interval.tv_usec = 333000;
+ tr_timevalSet( &interval, 0, 333000 );
break;
}
+ assert( tr_isTimeval( &interval ) );
evtimer_add( s->timer, &interval );
}
if( isEnabled )
{
struct timeval timeval;
- timeval.tv_sec = 0;
- timeval.tv_usec = 333000;
+
s->timer = tr_new0( struct event, 1 );
evtimer_set( s->timer, onTimer, s );
+ tr_timevalSet( &timeval, 0, 333000 );
evtimer_add( s->timer, &timeval );
}
assert( session->altTimer != NULL );
tr_localtime_r( &now, &tm );
- tv.tv_sec = 60 - tm.tm_sec;
- tv.tv_usec = 0;
+ tr_timevalSet( &tv, 60-tm.tm_sec, 0 );
evtimer_add( session->altTimer, &tv );
}
if(status == TR_DHT_STOPPED || status >= TR_DHT_FIREWALLED)
break;
tr_dhtAddNode(cl->session, &addr, port, 1);
- tv.tv_sec = 2 + tr_cryptoWeakRandInt( 5 );
- tv.tv_usec = tr_cryptoWeakRandInt( 1000000 );
- select(0, NULL, NULL, NULL, &tv);
+ tr_timevalSet( &tv, 2 + tr_cryptoWeakRandInt( 5 ), tr_cryptoWeakRandInt( 1000000 ) );
+ select( 0, NULL, NULL, NULL, &tv );
}
tr_free( cl->nodes );
tr_free( closure );
tr_threadNew( dht_bootstrap, cl );
}
- tv.tv_sec = 0;
- tv.tv_usec = tr_cryptoWeakRandInt( 1000000 );
+ tr_timevalSet( &tv, 0, tr_cryptoWeakRandInt( 1000000 ) );
event_set( &dht_event, dht_socket, EV_READ, event_callback, NULL );
+ assert( tr_isTimeval( &tv ) );
event_add( &dht_event, &tv );
return 1;
/* Being slightly late is fine,
and has the added benefit of adding some jitter. */
- tv.tv_sec = tosleep;
- tv.tv_usec = tr_cryptoWeakRandInt( 1000000 );
- event_add(&dht_event, &tv);
+ tr_timevalSet( &tv, tosleep, tr_cryptoWeakRandInt( 1000000 ) );
+ event_add( &dht_event, &tv );
}
void
more = ( *timer->func )( timer->user_data );
timer->inCallback = 0;
- if( more )
- evtimer_add( &timer->event, &timer->tv );
- else
+ if( !more )
tr_timerFree( &timer );
+ else {
+ assert( tr_isTimeval( &timer->tv ) );
+ evtimer_add( &timer->event, &timer->tv );
+ }
}
void
***
**/
+tr_bool
+tr_isTimeval( const struct timeval * tv )
+{
+ return tv && ( tv->tv_sec >= 0 )
+ && ( tv->tv_usec >= 0 )
+ && ( tv->tv_usec < 1000000 );
+}
+
void
tr_timevalMsec( uint64_t milliseconds, struct timeval * setme )
{
assert( setme != NULL );
setme->tv_sec = microseconds / 1000000;
setme->tv_usec = microseconds % 1000000;
+ assert( tr_isTimeval( setme ) );
}
+void
+tr_timevalSet( struct timeval * setme, int seconds, int microseconds )
+{
+ setme->tv_sec = seconds;
+ setme->tv_usec = microseconds;
+ assert( tr_isTimeval( setme ) );
+}
+
+
+/**
+***
+**/
+
uint8_t *
tr_loadFile( const char * path,
size_t * size )
struct timeval;
-void tr_timevalMsec( uint64_t milliseconds, struct timeval * setme );
+tr_bool tr_isTimeval( const struct timeval * tv );
+
+void tr_timevalMsec( uint64_t milliseconds, struct timeval * setme );
+
+void tr_timevalSet( struct timeval * setme, int seconds, int microseconds );
/** @brief return the current date in milliseconds */