(trunk libT) simplify the evtimer / struct timeval code a little

This commit is contained in:
Charles Kerr 2009-06-15 03:24:40 +00:00
parent 0380906aa6
commit 1be7f51b3d
7 changed files with 23 additions and 29 deletions

View File

@ -1185,7 +1185,6 @@ tr_handshakeNew( tr_peerIo * io,
handshakeDoneCB doneCB,
void * doneUserData )
{
struct timeval tv;
tr_handshake * handshake;
handshake = tr_new0( tr_handshake, 1 );
@ -1196,9 +1195,8 @@ tr_handshakeNew( tr_peerIo * io,
handshake->doneUserData = doneUserData;
handshake->session = tr_peerIoGetSession( io );
tr_timevalSet( &tv, HANDSHAKE_TIMEOUT_SEC, 0 );
evtimer_set( &handshake->timeout_timer, handshakeTimeout, handshake );
evtimer_add( &handshake->timeout_timer, &tv );
tr_timerAdd( &handshake->timeout_timer, HANDSHAKE_TIMEOUT_SEC, 0 );
tr_peerIoRef( io ); /* balanced by the unref in tr_handshakeFree */
tr_peerIoSetIOFuncs( handshake->io, canRead, NULL, gotError, handshake );

View File

@ -2116,13 +2116,11 @@ sendPex( tr_peermsgs * msgs )
static void
pexPulse( int foo UNUSED, short bar UNUSED, void * vmsgs )
{
struct timeval tv;
struct tr_peermsgs * msgs = vmsgs;
sendPex( msgs );
tr_timevalSet( &tv, PEX_INTERVAL_SECS, 0 );
evtimer_add( &msgs->pexTimer, &tv );
tr_timerAdd( &msgs->pexTimer, PEX_INTERVAL_SECS, 0 );
}
/**
@ -2137,7 +2135,6 @@ tr_peerMsgsNew( struct tr_torrent * torrent,
tr_publisher_tag * setme )
{
tr_peermsgs * m;
struct timeval tv;
assert( peer );
assert( peer->io );
@ -2160,9 +2157,8 @@ tr_peerMsgsNew( struct tr_torrent * torrent,
m->peerAskedFor = REQUEST_LIST_INIT;
m->clientAskedFor = REQUEST_LIST_INIT;
m->clientWillAskFor = REQUEST_LIST_INIT;
tr_timevalSet( &tv, PEX_INTERVAL_SECS, 0 );
evtimer_set( &m->pexTimer, pexPulse, m );
evtimer_add( &m->pexTimer, &tv );
tr_timerAdd( &m->pexTimer, PEX_INTERVAL_SECS, 0 );
peer->msgs = m;
*setme = tr_publisherSubscribe( &m->publisher, func, userData );

View File

@ -91,8 +91,8 @@ natPulse( tr_shared * s, tr_bool doPortCheck )
static void
onTimer( int fd UNUSED, short what UNUSED, void * vshared )
{
int sec=0, msec=0;
tr_shared * s = vshared;
struct timeval interval;
assert( s );
assert( s->timer );
@ -108,22 +108,21 @@ onTimer( int fd UNUSED, short what UNUSED, void * vshared )
/* if we're mapped, everything is fine... check back in 20 minutes
* to renew the port forwarding if it's expired */
s->doPortCheck = TRUE;
tr_timevalSet( &interval, 60*20, 0 );
sec = 60 * 20;
break;
case TR_PORT_ERROR:
/* some kind of an error. wait 60 seconds and retry */
tr_timevalSet( &interval, 60, 0 );
sec = 60;
break;
default:
/* in progress. pulse frequently. */
tr_timevalSet( &interval, 0, 333000 );
msec = 333000;
break;
}
assert( tr_isTimeval( &interval ) );
evtimer_add( s->timer, &interval );
tr_timerAdd( s->timer, sec, msec );
}
/***
@ -142,12 +141,9 @@ tr_sharedInit( tr_session * session, tr_bool isEnabled )
if( isEnabled )
{
struct timeval timeval;
s->timer = tr_new0( struct event, 1 );
evtimer_set( s->timer, onTimer, s );
tr_timevalSet( &timeval, 0, 333000 );
evtimer_add( s->timer, &timeval );
tr_timerAdd( s->timer, 0, 333000 );
}
return s;

View File

@ -1062,14 +1062,12 @@ setAltTimer( tr_session * session )
{
const time_t now = time( NULL );
struct tm tm;
struct timeval tv;
assert( tr_isSession( session ) );
assert( session->altTimer != NULL );
tr_localtime_r( &now, &tm );
tr_timevalSet( &tv, 60-tm.tm_sec, 0 );
evtimer_add( session->altTimer, &tv );
tr_timerAdd( session->altTimer, 60-tm.tm_sec, 0 );
}
/* this is called once a minute to:

View File

@ -102,7 +102,6 @@ int
tr_dhtInit(tr_session *ss)
{
struct sockaddr_in sin;
struct timeval tv;
tr_benc benc;
int rc;
tr_bool have_id = FALSE;
@ -168,10 +167,8 @@ tr_dhtInit(tr_session *ss)
tr_threadNew( dht_bootstrap, cl );
}
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 );
tr_timerAdd( &dht_event, 0, tr_cryptoWeakRandInt( 1000000 ) );
return 1;
@ -360,7 +357,6 @@ static void
event_callback(int s, short type, void *ignore UNUSED )
{
time_t tosleep;
struct timeval tv;
if( dht_periodic(s, type == EV_READ, &tosleep, callback, NULL) < 0 ) {
if(errno == EINTR) {
@ -375,8 +371,7 @@ event_callback(int s, short type, void *ignore UNUSED )
/* Being slightly late is fine,
and has the added benefit of adding some jitter. */
tr_timevalSet( &tv, tosleep, tr_cryptoWeakRandInt( 1000000 ) );
event_add( &dht_event, &tv );
tr_timerAdd( &dht_event, tosleep, tr_cryptoWeakRandInt( 1000000 ) );
}
void

View File

@ -483,6 +483,13 @@ tr_timevalSet( struct timeval * setme, int seconds, int microseconds )
assert( tr_isTimeval( setme ) );
}
void
tr_timerAdd( struct event * timer, int seconds, int milliseconds )
{
struct timeval tv;
tr_timevalSet( &tv, seconds, milliseconds );
event_add( timer, &tv );
}
/**
***

View File

@ -244,6 +244,10 @@ void tr_timevalMsec( uint64_t milliseconds, struct timeval * setme );
void tr_timevalSet( struct timeval * setme, int seconds, int microseconds );
struct event;
void tr_timerAdd( struct event * timer, int seconds, int milliseconds );
/** @brief return the current date in milliseconds */
uint64_t tr_date( void );