2007-08-14 20:45:23 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
|
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2007-08-18 17:05:51 +00:00
|
|
|
*
|
2007-08-18 17:19:49 +00:00
|
|
|
* $Id$
|
2007-08-14 20:45:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <sys/types.h> /* u_char for event.h */
|
|
|
|
#include <event.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2007-08-18 03:02:32 +00:00
|
|
|
#include "trevent.h"
|
2007-08-14 20:45:23 +00:00
|
|
|
#include "timer.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
typedef int tr_timer_func ( void * user_data );
|
|
|
|
typedef void tr_data_free_func( void * user_data );
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct timer_node
|
|
|
|
{
|
2007-08-18 06:59:20 +00:00
|
|
|
tr_handle_t * handle;
|
|
|
|
struct event * event;
|
2007-08-14 20:45:23 +00:00
|
|
|
tr_timer_func * func;
|
|
|
|
void * user_data;
|
|
|
|
tr_data_free_func * free_func;
|
|
|
|
struct timeval tv;
|
2007-09-05 23:01:56 +00:00
|
|
|
uint8_t doFree;
|
2007-08-14 20:45:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_timerFree( struct timer_node ** node )
|
|
|
|
{
|
|
|
|
if( *node )
|
|
|
|
{
|
2007-09-05 23:01:56 +00:00
|
|
|
(*node)->doFree = 1;
|
2007-08-14 20:45:23 +00:00
|
|
|
*node = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
timerCB( int fd UNUSED, short event UNUSED, void * arg )
|
|
|
|
{
|
|
|
|
struct timer_node * node = (struct timer_node *) arg;
|
|
|
|
|
2007-09-05 23:01:56 +00:00
|
|
|
if( !node->doFree )
|
|
|
|
node->doFree = !(node->func)(node->user_data);
|
|
|
|
|
|
|
|
if( node->doFree ) {
|
|
|
|
if( node->free_func != NULL )
|
|
|
|
(node->free_func)( node->user_data );
|
|
|
|
tr_event_del( node->handle, node->event );
|
|
|
|
tr_free( node );
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tr_timer_tag
|
2007-08-18 03:02:32 +00:00
|
|
|
tr_timerNew( tr_handle_t * handle,
|
|
|
|
tr_timer_func func,
|
2007-08-14 20:45:23 +00:00
|
|
|
void * user_data,
|
|
|
|
tr_data_free_func free_func,
|
|
|
|
int timeout_milliseconds )
|
|
|
|
{
|
|
|
|
struct timer_node * node;
|
|
|
|
|
|
|
|
assert( func != NULL );
|
|
|
|
assert( timeout_milliseconds >= 0 );
|
|
|
|
|
|
|
|
node = tr_new( struct timer_node, 1 );
|
2007-08-18 06:59:20 +00:00
|
|
|
node->handle = handle;
|
|
|
|
node->event = tr_new0( struct event, 1 );
|
2007-08-14 20:45:23 +00:00
|
|
|
node->func = func;
|
|
|
|
node->user_data = user_data;
|
|
|
|
node->free_func = free_func;
|
2007-09-05 23:01:56 +00:00
|
|
|
node->doFree = 0;
|
2007-08-23 20:33:01 +00:00
|
|
|
node->tv = timevalMsec ( timeout_milliseconds );
|
2007-08-18 06:59:20 +00:00
|
|
|
timeout_set( node->event, timerCB, node );
|
|
|
|
tr_event_add( handle, node->event, &node->tv );
|
2007-08-14 20:45:23 +00:00
|
|
|
return node;
|
|
|
|
}
|