diff --git a/libtransmission/rpc-server.c b/libtransmission/rpc-server.c index 28e966c64..7388b3638 100644 --- a/libtransmission/rpc-server.c +++ b/libtransmission/rpc-server.c @@ -398,8 +398,10 @@ handle_request( struct evhttp_request * req, } static void -startServer( tr_rpc_server * server ) +startServer( void * vserver ) { + tr_rpc_server * server = vserver; + fprintf( stderr, "%s:%d in startServer; current context is %p\n", __FILE__, __LINE__, server->httpd ); if( !server->httpd ) @@ -423,16 +425,25 @@ stopServer( tr_rpc_server * server ) } } +static void +onEnabledChanged( void * vserver ) +{ + tr_rpc_server * server = vserver; + + if( !server->isEnabled ) + stopServer( server ); + else + startServer( server ); +} + + void tr_rpcSetEnabled( tr_rpc_server * server, int isEnabled ) { server->isEnabled = isEnabled != 0; - if( !isEnabled ) - stopServer( server ); - else - startServer( server ); + tr_runInEventThread( server->session, onEnabledChanged, server ); } int @@ -441,6 +452,18 @@ tr_rpcIsEnabled( const tr_rpc_server * server ) return server->isEnabled; } +static void +restartServer( void * vserver ) +{ + tr_rpc_server * server = vserver; + + if( server->isEnabled ) + { + stopServer( server ); + startServer( server ); + } +} + void tr_rpcSetPort( tr_rpc_server * server, uint16_t port ) @@ -450,10 +473,7 @@ tr_rpcSetPort( tr_rpc_server * server, server->port = port; if( server->isEnabled ) - { - stopServer( server ); - startServer( server ); - } + tr_runInEventThread( server->session, restartServer, server ); } } @@ -529,13 +549,10 @@ tr_rpcIsPasswordEnabled( const tr_rpc_server * server ) ***** LIFE CYCLE ****/ -void -tr_rpcClose( tr_rpc_server ** ps ) +static void +closeServer( void * vserver ) { - tr_rpc_server * s = *ps; - - *ps = NULL; - + tr_rpc_server * s = vserver; stopServer( s ); tr_free( s->acl ); tr_free( s->username ); @@ -543,6 +560,13 @@ tr_rpcClose( tr_rpc_server ** ps ) tr_free( s ); } +void +tr_rpcClose( tr_rpc_server ** ps ) +{ + tr_runInEventThread( (*ps)->session, closeServer, *ps ); + *ps = NULL; +} + tr_rpc_server * tr_rpcInit( tr_handle * session, int isEnabled, @@ -562,9 +586,8 @@ tr_rpcInit( tr_handle * session, s->password = tr_strdup( password ); s->isPasswordEnabled = isPasswordEnabled != 0; s->isEnabled = isEnabled != 0; - if( isEnabled ) - startServer( s ); + tr_runInEventThread( session, startServer, s ); return s; } diff --git a/libtransmission/trevent.c b/libtransmission/trevent.c index bc71dbfca..f652c2ae9 100644 --- a/libtransmission/trevent.c +++ b/libtransmission/trevent.c @@ -145,7 +145,7 @@ logFunc( int severity, static void libeventThreadFunc( void * veh ) { - tr_event_handle * eh = (tr_event_handle *) veh; + tr_event_handle * eh = veh; tr_dbg( "Starting libevent thread" ); @@ -154,9 +154,7 @@ libeventThreadFunc( void * veh ) signal( SIGPIPE, SIG_IGN ); #endif - eh->base = event_init( ); eh->h->events = eh; - event_set_log_callback( logFunc ); /* listen to the pipe's read fd */ event_set( &eh->pipeEvent, eh->fds[0], EV_READ | EV_PERSIST, @@ -182,6 +180,7 @@ tr_eventInit( tr_handle * handle ) eh->lock = tr_lockNew( ); pipe( eh->fds ); eh->h = handle; + eh->base = event_init( ); eh->thread = tr_threadNew( libeventThreadFunc, eh ); }