2007-08-18 03:02:32 +00:00
|
|
|
/*
|
2008-01-01 17:20:20 +00:00
|
|
|
* This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
|
2007-08-18 03:02:32 +00:00
|
|
|
*
|
|
|
|
* 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:19:49 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
2007-08-18 03:02:32 +00:00
|
|
|
*/
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-08-18 03:02:32 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2007-09-05 23:01:56 +00:00
|
|
|
#include <stdio.h>
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <signal.h>
|
2007-08-18 03:02:32 +00:00
|
|
|
|
|
|
|
#include <event.h>
|
|
|
|
#include <evhttp.h>
|
|
|
|
|
|
|
|
#include "transmission.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "list.h"
|
2007-08-18 03:02:32 +00:00
|
|
|
#include "platform.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "trevent.h"
|
2007-08-18 03:02:32 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2007-08-20 02:29:36 +00:00
|
|
|
/* #define DEBUG */
|
2007-08-18 03:02:32 +00:00
|
|
|
#ifdef DEBUG
|
2007-08-20 23:37:08 +00:00
|
|
|
#include <stdio.h>
|
2007-08-18 03:02:32 +00:00
|
|
|
#undef tr_dbg
|
|
|
|
#define tr_dbg( a, b... ) fprintf(stderr, a "\n", ##b )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
typedef struct tr_event_handle
|
2007-08-18 03:02:32 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lock * lock;
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_handle * h;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_thread * thread;
|
|
|
|
tr_list * commands;
|
2007-08-20 23:37:08 +00:00
|
|
|
struct event_base * base;
|
2007-09-20 16:32:01 +00:00
|
|
|
struct event pulse;
|
|
|
|
struct timeval pulseInterval;
|
|
|
|
uint8_t die;
|
|
|
|
|
|
|
|
int timerCount;
|
2007-08-18 03:02:32 +00:00
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_event_handle;
|
2007-08-18 03:02:32 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
static int reads = 0;
|
|
|
|
static int writes = 0;
|
|
|
|
#endif
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
enum mode
|
|
|
|
{
|
|
|
|
TR_EV_TIMER_ADD,
|
|
|
|
TR_EV_EXEC
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef int timer_func(void*);
|
|
|
|
|
|
|
|
struct tr_timer
|
|
|
|
{
|
|
|
|
struct event event;
|
|
|
|
struct timeval tv;
|
|
|
|
timer_func * func;
|
|
|
|
void * user_data;
|
|
|
|
struct tr_event_handle * eh;
|
|
|
|
uint8_t inCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct tr_event_command
|
2007-08-18 03:02:32 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
int mode;
|
|
|
|
|
|
|
|
struct tr_timer * timer;
|
|
|
|
|
2007-08-18 03:02:32 +00:00
|
|
|
struct evhttp_connection * evcon;
|
|
|
|
struct evhttp_request * req;
|
2007-09-20 16:32:01 +00:00
|
|
|
enum evhttp_cmd_type evtype;
|
2007-08-18 23:32:07 +00:00
|
|
|
char * uri;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
struct bufferevent * bufev;
|
|
|
|
short enable;
|
|
|
|
short disable;
|
2007-09-05 23:01:56 +00:00
|
|
|
char * buf;
|
|
|
|
size_t buflen;
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void (*func)( void* );
|
|
|
|
void * user_data;
|
|
|
|
};
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
|
|
|
pumpList( int i UNUSED, short s UNUSED, void * veh )
|
|
|
|
{
|
|
|
|
tr_event_handle * eh = veh;
|
2007-10-08 00:56:12 +00:00
|
|
|
int doDie;
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-10-08 00:56:12 +00:00
|
|
|
for( ;; )
|
2007-08-18 03:02:32 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_event_command * cmd;
|
2007-08-18 06:59:20 +00:00
|
|
|
|
2007-10-08 00:56:12 +00:00
|
|
|
doDie = eh->die && !eh->timerCount;
|
|
|
|
if( doDie )
|
|
|
|
break;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/* get the next command */
|
|
|
|
tr_lockLock( eh->lock );
|
|
|
|
cmd = tr_list_pop_front( &eh->commands );
|
|
|
|
tr_lockUnlock( eh->lock );
|
|
|
|
if( cmd == NULL )
|
2007-08-18 03:02:32 +00:00
|
|
|
break;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/* process the command */
|
|
|
|
switch( cmd->mode )
|
|
|
|
{
|
|
|
|
case TR_EV_TIMER_ADD:
|
2008-04-24 19:38:59 +00:00
|
|
|
evtimer_add( &cmd->timer->event, &cmd->timer->tv );
|
2007-09-20 16:32:01 +00:00
|
|
|
++eh->timerCount;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_EV_EXEC:
|
|
|
|
(cmd->func)( cmd->user_data );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( 0 && "unhandled command type!" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free( cmd );
|
|
|
|
}
|
2007-09-05 23:01:56 +00:00
|
|
|
|
2007-10-08 00:56:12 +00:00
|
|
|
if( !doDie )
|
2008-04-24 19:38:59 +00:00
|
|
|
evtimer_add( &eh->pulse, &eh->pulseInterval );
|
2007-09-20 16:32:01 +00:00
|
|
|
else {
|
|
|
|
assert( eh->timerCount == 0 );
|
|
|
|
event_del( &eh->pulse );
|
2007-08-18 03:02:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-19 03:54:27 +00:00
|
|
|
static void
|
|
|
|
logFunc( int severity, const char * message )
|
|
|
|
{
|
2008-03-18 02:23:39 +00:00
|
|
|
if( severity >= _EVENT_LOG_ERR )
|
|
|
|
tr_nerr( "%s", message );
|
|
|
|
else
|
|
|
|
tr_ndbg( "%s", message );
|
2007-08-19 03:54:27 +00:00
|
|
|
}
|
|
|
|
|
2007-08-18 03:02:32 +00:00
|
|
|
static void
|
|
|
|
libeventThreadFunc( void * veh )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_event_handle * eh = (tr_event_handle *) veh;
|
2007-08-20 02:18:38 +00:00
|
|
|
tr_dbg( "Starting libevent thread" );
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#ifndef WIN32
|
|
|
|
/* Don't exit when writing on a broken socket */
|
|
|
|
signal( SIGPIPE, SIG_IGN );
|
|
|
|
#endif
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
eh->base = event_init( );
|
2008-03-18 02:23:39 +00:00
|
|
|
event_set_log_callback( logFunc );
|
2008-04-24 19:38:59 +00:00
|
|
|
evtimer_set( &eh->pulse, pumpList, veh );
|
|
|
|
evtimer_add( &eh->pulse, &eh->pulseInterval );
|
2007-09-20 16:32:01 +00:00
|
|
|
eh->h->events = eh;
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-08-20 23:37:08 +00:00
|
|
|
event_dispatch( );
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-08-20 02:18:38 +00:00
|
|
|
tr_lockFree( eh->lock );
|
2007-08-20 23:37:08 +00:00
|
|
|
event_base_free( eh->base );
|
2007-08-20 02:18:38 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
eh->h->events = NULL;
|
|
|
|
|
|
|
|
tr_free( eh );
|
2007-08-20 02:18:38 +00:00
|
|
|
tr_dbg( "Closing libevent thread" );
|
2007-08-18 03:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_eventInit( tr_handle * handle )
|
2007-08-18 03:02:32 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_event_handle * eh;
|
2007-08-18 03:02:32 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
eh = tr_new0( tr_event_handle, 1 );
|
2007-08-18 03:02:32 +00:00
|
|
|
eh->lock = tr_lockNew( );
|
|
|
|
eh->h = handle;
|
2008-04-24 01:42:53 +00:00
|
|
|
eh->pulseInterval = tr_timevalMsec( 100 );
|
2007-09-05 23:01:56 +00:00
|
|
|
eh->thread = tr_threadNew( libeventThreadFunc, eh, "libeventThreadFunc" );
|
2007-08-18 03:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 20:14:13 +00:00
|
|
|
tr_eventClose( tr_handle * handle )
|
2007-08-18 03:02:32 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_event_handle * eh = handle->events;
|
2007-08-20 02:18:38 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lockLock( eh->lock );
|
2007-09-28 14:27:56 +00:00
|
|
|
tr_list_free( &eh->commands, tr_free );
|
2007-09-20 16:32:01 +00:00
|
|
|
eh->die = TRUE;
|
|
|
|
tr_lockUnlock( eh->lock );
|
2007-08-18 03:02:32 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
static void
|
|
|
|
pushList( struct tr_event_handle * eh, struct tr_event_command * command )
|
2007-08-18 06:59:20 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lockLock( eh->lock );
|
|
|
|
tr_list_append( &eh->commands, command );
|
|
|
|
tr_lockUnlock( eh->lock );
|
2007-08-18 06:59:20 +00:00
|
|
|
}
|
|
|
|
|
2007-11-15 16:43:46 +00:00
|
|
|
int
|
|
|
|
tr_amInEventThread( struct tr_handle * handle )
|
|
|
|
{
|
|
|
|
return tr_amInThread( handle->events->thread );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
static int
|
|
|
|
timerCompareFunc( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const struct tr_event_command * a = va;
|
|
|
|
const struct tr_timer * b = vb;
|
|
|
|
return a->timer == b ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
timerCallback( int fd UNUSED, short event UNUSED, void * vtimer )
|
|
|
|
{
|
|
|
|
int more;
|
|
|
|
struct tr_timer * timer = vtimer;
|
|
|
|
void * del;
|
|
|
|
|
|
|
|
del = tr_list_remove( &timer->eh->commands, timer, timerCompareFunc );
|
|
|
|
|
|
|
|
if( del != NULL ) /* there's a TIMER_DEL command queued for this timer... */
|
|
|
|
more = FALSE;
|
|
|
|
else {
|
|
|
|
timer->inCallback = 1;
|
|
|
|
more = (*timer->func)( timer->user_data );
|
|
|
|
timer->inCallback = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( more )
|
2008-04-24 19:38:59 +00:00
|
|
|
evtimer_add( &timer->event, &timer->tv );
|
2007-09-05 23:01:56 +00:00
|
|
|
else
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_timerFree( &timer );
|
2007-10-01 05:49:23 +00:00
|
|
|
|
|
|
|
tr_free( del );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_timerFree( tr_timer ** ptimer )
|
|
|
|
{
|
|
|
|
tr_timer * timer;
|
|
|
|
|
|
|
|
/* zero out the argument passed in */
|
|
|
|
assert( ptimer );
|
|
|
|
timer = *ptimer;
|
|
|
|
*ptimer = NULL;
|
|
|
|
|
|
|
|
/* destroy the timer directly or via the command queue */
|
|
|
|
if( timer!=NULL && !timer->inCallback ) {
|
2007-11-19 16:30:31 +00:00
|
|
|
void * del;
|
|
|
|
assert( tr_amInEventThread( timer->eh->h ) );
|
|
|
|
del = tr_list_remove( &timer->eh->commands, timer, timerCompareFunc );
|
|
|
|
--timer->eh->timerCount;
|
|
|
|
event_del( &timer->event );
|
|
|
|
tr_free( timer );
|
|
|
|
tr_free( del );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_timer*
|
|
|
|
tr_timerNew( struct tr_handle * handle,
|
|
|
|
timer_func func,
|
|
|
|
void * user_data,
|
2008-04-24 19:38:59 +00:00
|
|
|
uint64_t interval_milliseconds )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
tr_timer * timer = tr_new0( tr_timer, 1 );
|
2008-04-24 19:38:59 +00:00
|
|
|
timer->tv = tr_timevalMsec( interval_milliseconds );
|
2007-09-20 16:32:01 +00:00
|
|
|
timer->func = func;
|
|
|
|
timer->user_data = user_data;
|
|
|
|
timer->eh = handle->events;
|
2008-04-24 19:38:59 +00:00
|
|
|
evtimer_set( &timer->event, timerCallback, timer );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
if( tr_amInThread( handle->events->thread ) ) {
|
2008-04-24 19:38:59 +00:00
|
|
|
evtimer_add( &timer->event, &timer->tv );
|
2007-09-20 16:32:01 +00:00
|
|
|
++handle->events->timerCount;
|
|
|
|
} else {
|
|
|
|
struct tr_event_command * cmd = tr_new0( struct tr_event_command, 1 );
|
|
|
|
cmd->mode = TR_EV_TIMER_ADD;
|
|
|
|
cmd->timer = timer;
|
|
|
|
pushList( handle->events, cmd );
|
|
|
|
}
|
|
|
|
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_runInEventThread( struct tr_handle * handle,
|
|
|
|
void func( void* ),
|
|
|
|
void * user_data )
|
|
|
|
{
|
|
|
|
if( tr_amInThread( handle->events->thread ) )
|
|
|
|
(func)( user_data );
|
|
|
|
else {
|
|
|
|
struct tr_event_command * cmd = tr_new0( struct tr_event_command, 1 );
|
|
|
|
cmd->mode = TR_EV_EXEC;
|
|
|
|
cmd->func = func;
|
|
|
|
cmd->user_data = user_data;
|
|
|
|
pushList( handle->events, cmd );
|
|
|
|
}
|
|
|
|
}
|